我用jbuilder链接oracle数据库,想更新某个表的clob字段,总是报错!
请大家帮忙分析一下!!!
完整的代码如下:
package com.temp;
import java.sql.*;
public class test{
public test(){} public static Connection getConnection1() throws SQLException {
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}catch(Exception e){}
String url = "jdbc:oracle:thin:@192.168.0.1:1521:ora1";
String user = "admin";
String password = "admin";
return java.sql.DriverManager.getConnection(url,user,password);
} public static void main(String[] args) throws Exception{
Connection con=null;
Connection access=null;
PreparedStatement pst=null;
ResultSet rs=null;
try{
String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=d:/123.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
access= java.sql.DriverManager.getConnection(url,"","");
con=getConnection1();
con.setAutoCommit(false);
pst=access.prepareStatement("select newsid,content from newsinfo order by newsid");
rs=pst.executeQuery();
int k=0;String content="";int newsid=0;
while(rs.next()){
newsid=rs.getInt("newsid");
content=rs.getString("content");
PreparedStatement pst1=null;
ResultSet rs1=null;
pst1 = con.prepareStatement("select c_content from newsinfo where id=?");
pst1.setInt(1,newsid);
rs1 = pst1.executeQuery();
if (!rs1.next())System.out.println("读取数据");
oracle.sql.CLOB clobjj = (oracle.sql.CLOB) rs1.getClob("c_content");
clobjj.putString(1, content);
pst1.clearParameters();
pst1 = con.prepareStatement("update newsinfo set c_content=? where id=?");
pst1.setClob(1, clobjj);
pst1.setInt(2, newsid);
k+=pst1.executeUpdate();
System.out.println(k);
rs1.close();
pst1.close();
System.out.println(newsid);
}
con.commit();
rs.close();
pst.close();
System.out.println("完毕.");
}
catch (Exception e) {
con.rollback();
System.out.println(e);
e.printStackTrace(System.out);
}
finally {
if (!con.isClosed()){
con.setAutoCommit(true);
con.close();
}
if(!access.isClosed()){
access.close();
}
}
}}
请大家帮忙分析一下!!!
完整的代码如下:
package com.temp;
import java.sql.*;
public class test{
public test(){} public static Connection getConnection1() throws SQLException {
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}catch(Exception e){}
String url = "jdbc:oracle:thin:@192.168.0.1:1521:ora1";
String user = "admin";
String password = "admin";
return java.sql.DriverManager.getConnection(url,user,password);
} public static void main(String[] args) throws Exception{
Connection con=null;
Connection access=null;
PreparedStatement pst=null;
ResultSet rs=null;
try{
String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=d:/123.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
access= java.sql.DriverManager.getConnection(url,"","");
con=getConnection1();
con.setAutoCommit(false);
pst=access.prepareStatement("select newsid,content from newsinfo order by newsid");
rs=pst.executeQuery();
int k=0;String content="";int newsid=0;
while(rs.next()){
newsid=rs.getInt("newsid");
content=rs.getString("content");
PreparedStatement pst1=null;
ResultSet rs1=null;
pst1 = con.prepareStatement("select c_content from newsinfo where id=?");
pst1.setInt(1,newsid);
rs1 = pst1.executeQuery();
if (!rs1.next())System.out.println("读取数据");
oracle.sql.CLOB clobjj = (oracle.sql.CLOB) rs1.getClob("c_content");
clobjj.putString(1, content);
pst1.clearParameters();
pst1 = con.prepareStatement("update newsinfo set c_content=? where id=?");
pst1.setClob(1, clobjj);
pst1.setInt(2, newsid);
k+=pst1.executeUpdate();
System.out.println(k);
rs1.close();
pst1.close();
System.out.println(newsid);
}
con.commit();
rs.close();
pst.close();
System.out.println("完毕.");
}
catch (Exception e) {
con.rollback();
System.out.println(e);
e.printStackTrace(System.out);
}
finally {
if (!con.isClosed()){
con.setAutoCommit(true);
con.close();
}
if(!access.isClosed()){
access.close();
}
}
}}
------------------------------------------------------------------------------
java.sql.SQLException: ORA-22920: 未锁定含有 LOB 值的行java.sql.SQLException: ORA-22920: 未锁定含有 LOB 值的行 at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.v8TTILob.receiveReply(v8TTILob.java:989)
at oracle.jdbc.ttc7.v8TTIClob.write(v8TTIClob.java:329)
at oracle.jdbc.ttc7.TTC7Protocol.lobWrite(TTC7Protocol.java:2334)
at oracle.sql.LobDBAccessImpl.putChars(LobDBAccessImpl.java:658)
at oracle.sql.CLOB.putChars(CLOB.java:542)
at oracle.sql.CLOB.putString(CLOB.java:627)
at com.temp.test.main(test.java:40)
是不是没有提交,然后又对字段进行操作了~
谢谢提醒,是忘记for update了。
应该在pst1 = con.prepareStatement("select c_content from newsinfo where id=? for update");