jdbc获取clob图片_jdbc方式读取oracle的clob字段实例
可能大家也都習(xí)慣了spring和hibernate對(duì)CLOB字段的處理,在spring中配置clob的oracle處理句柄,在hibernate中配置映射類型,然后就可以很輕松的以String 的形式處理大字段。
今天我做了個(gè)需求,需要以jdbc的方式從mysql導(dǎo)一些備份數(shù)據(jù)到oracle正式庫(kù),就查了一些資料,最后寫(xiě)了個(gè)例子:
1:首先:寫(xiě)個(gè)連接數(shù)據(jù)庫(kù)的類,里面有返回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:數(shù)據(jù)庫(kù)名字”,"name","password");
}
if(flag.equals("2"))
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/數(shù)據(jù)庫(kù)名?user=用戶名&password=密碼&useUnicode=true&characterEncoding=GBK");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
2:執(zhí)行插入操作
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();? //插入時(shí)將大字段設(shè)為空
this.updateOne(con,rs.getInt(1),rs.getString(2));??//?這里調(diào)用然后更新這個(gè)大字段
}
rs.close();? //關(guān)閉相關(guān)連接
pstmt.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滾出現(xiàn)異常!");
e1.printStackTrace();
}
}
}
3:該方法實(shí)現(xiàn)對(duì)應(yīng)大字段記錄的更新
public void updateOne(Connection con,int id, String content) {
String str = "select t.content from table? t?where t.id=" + id+ "? for update";
try {
//?注意:存取操作開(kāi)始前,必須用setAutoCommit(false)取消自動(dòng)提交,否則Oracle將拋出“讀取違反順序”的錯(cuò)誤。
con.setAutoCommit(false);
stmt = con.createStatement();
ResultSet?? rs_clob = stmt.executeQuery(str);
while (?rs_clob .next()) {
/*?取出clob數(shù)據(jù)*/
oracle.sql.CLOB clob = (oracle.sql.CLOB)??rs_clob .getClob(1);
/* 向clob中寫(xiě)入數(shù)據(jù)*/
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("回滾出現(xiàn)異常!");
e1.printStackTrace();
}
}
}
現(xiàn)在就完成了一行記錄的更新。
4:讀clob字段以String 的形式返回(當(dāng)然也可以將讀到的內(nèi)容寫(xiě)入文件,大家改一下就可以了)
/**
* 讀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對(duì)象
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);//寫(xiě)入文件
out.close();*/
con.setAutoCommit(true);
con.close();
}catch(Exception e)
{
System.out.println("出現(xiàn)異常");
e.printStackTrace();
try
{
con.rollback();
}
catch (Exception e1)
{
System.out.println("回滾出現(xiàn)異常!");
e1.printStackTrace();
}
}
return content;
}
總結(jié)
以上是生活随笔為你收集整理的jdbc获取clob图片_jdbc方式读取oracle的clob字段实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 解密TTY
- 下一篇: ABAP 销售订单BAPI创建批导程序