今天我做了個需求,需要以jdbc的方式從mysql導一些備份數據到oracle正式庫,就查了一些資料,最后寫了個例子:
1:首先:寫個連接數據庫的類,里面有返回mysq, oracle連接的方法
public Connection getConn(String flag){
Connection con=null;
try
{
if(flag.equals("1"))
{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con = DriverManager.getConnection(“jdbc:oracle:thin:@IP:1521:數據庫名字”,"name","password");
}
if(flag.equals("2"))
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/數據庫名?user=用戶名&password=密碼&useUnicode=true&characterEncoding=GBK");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
2:執行插入操作
public void setData() {
conn = new Conn();
try {
String sqlfrom = "select p.id,p.content from table p order by p.id ";
String sqlinsert = "insert into table values(?,?)";
con = conn.getConn("2");
stmt = con.createStatement(); //從mysql取出大字段
rs = stmt.executeQuery(sqlfrom);
con = conn.getConn("1");
PreparedStatement pstmt = con.prepareStatement(sqlinsert); //向oracle中插入大字段
int i = 0;
while (rs.next()) {
pstmt.setInt(1, rs.getInt(1));
pstmt.setClob(2, oracle.sql.CLOB.empty_lob());
pstmt.executeUpdate(); //插入時將大字段設為空
this.updateOne(con,rs.getInt(1),rs.getString(2)); // 這里調用然后更新這個大字段
}
rs.close(); //關閉相關連接
pstmt.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滾出現異常!");
e1.printStackTrace();
}
}
}
3:該方法實現對應大字段記錄的更新
public void updateOne(Connection con,int id, String content) {
String str = "select t.content from table t where t.id=" + id+ " for update";
try {
// 注意:存取操作開始前,必須用setAutoCommit(false)取消自動提交,否則Oracle將拋出“讀取違反順序”的錯誤。
con.setAutoCommit(false);
stmt = con.createStatement();
ResultSet rs_clob = stmt.executeQuery(str);
while ( rs_clob .next()) {
/* 取出clob數據*/
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs_clob .getClob(1);
/* 向clob中寫入數據*/
clob.putString(1, content);
}
stmt.close();
con.commit();
con.setAutoCommit(true);
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滾出現異常!");
e1.printStackTrace();
}
}
}
4:讀clob字段以String 的形式返回(當然也可以將讀到的內容寫入文件,大家改一下就可以了)
/**
* 讀clob字段
* @param con
* @param id
* @return
*/
public String readClob(Connection con,int id)
{
String content="";
try
{
con.setAutoCommit(false);
stmt=con.createStatement();
ResultSet rs_clob=stmt.executeQuery("select t.content from table t where t.id="+id);
oracle.sql.CLOB contents=null;
while (rs_clob.next())
{ // 取出CLOB對象
contents= (oracle.sql.CLOB) rs_clob.getClob(1);
}
BufferedReader a = new BufferedReader(contents.getCharacterStream()); //以字符流的方式讀入BufferedReader
String str = "";
while ((str = a.readLine()) != null) {
content = content.concat(str); //最后以String的形式得到
}
con.commit();
/*
BufferedWriter out = new BufferedWriter(new FileWriter("e:/test.txt"));
out.write(content);//寫入文件
out.close();*/
con.setAutoCommit(true);
con.close();
}catch(Exception e)
{
System.out.println("出現異常");
e.printStackTrace();
try
{
con.rollback();
}
catch (Exception e1)
{
System.out.println("回滾出現異常!");
e1.printStackTrace();
}
}
return content;
}