一、前言
java通过ojdbc6.jar客户端驱动包连接oracle数据库,提交oracle.sql.CLOB的putString/clob.putChars的api方法报错CLOB字符大型对象的数据类型字段保存,报"
java.sql.SQLException: ORA-22920: 未锁定含有 LOB 值的行" 错误异常,详情日志如下>>
java.sql.SQLException: ORA-22920: 未锁定含有 LOB 值的行@b@@b@ at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)@b@ at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)@b@ at oracle.jdbc.ttc7.v8TTILob.receiveReply(v8TTILob.java:989)@b@ at oracle.jdbc.ttc7.v8TTIClob.write(v8TTIClob.java:329)@b@ at oracle.jdbc.ttc7.TTC7Protocol.lobWrite(TTC7Protocol.java:2334)@b@ at oracle.sql.LobDBAccessImpl.putChars(LobDBAccessImpl.java:658)@b@ at oracle.sql.CLOB.putChars(CLOB.java:542)@b@ at oracle.sql.CLOB.putString(CLOB.java:627)@b@ at com.xwood.jars.util.DBUtilExt.updateCLOB(DBUtilExt.java:41)@b@ at com.xwood.jars.luoku.pms.PmsJarContentFolderHandler.updateContext(PmsJarContentFolderHandler.java:306)@b@ at com.xwood.jars.luoku.pms.PmsJarContentFolderHandler.addContent(PmsJarContentFolderHandler.java:363)@b@ at com.xwood.jars.luoku.pms.PmsJarContentFolderHandler.persist(PmsJarContentFolderHandler.java:297)@b@ at com.xwood.jars.luoku.pms.PmsJarContentFolderHandler.handlerFile(PmsJarContentFolderHandler.java:78)@b@ at com.xwood.jars.luoku.pms.JarParseHandler.run(JarParseHandler.java:46)@b@ at com.xwood.jars.persist.PmsJarAtomExecuteor.jobWBSEntry(PmsJarAtomExecuteor.java:34)@b@ at com.xwood.jars.common.Root5sTimerMain$1.run(Root5sTimerMain.java:53)@b@ at java.util.TimerThread.mainLoop(Timer.java:555)@b@ at java.util.TimerThread.run(Timer.java:505)
二、解决方法
1、原报错CLOB大字段方法如下
public static int updateCLOB(String selectSql,String updateSql,int clobPostion,String clobValue){@b@ int retV=0;@b@ PreparedStatement ps = null;@b@ ResultSet rs = null;@b@ Statement st=null;@b@ try { @b@ ps = conn.prepareStatement(updateSql);@b@ ps.setClob(clobPostion, oracle.sql.CLOB.empty_lob()); @b@ retV=ps.executeUpdate();@b@ st=conn.createStatement();@b@ rs = st.executeQuery(selectSql); @b@ if(rs.next()) { @b@ oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob(clobPostion); @b@ if(clob!=null){@b@ clob.putString(1,clobValue);@b@ }@b@ }@b@ st.close();@b@ conn.commit();@b@ }catch (Exception e) {@b@ try {@b@ conn.rollback();@b@ } catch (SQLException e1) {@b@ e1.printStackTrace();@b@ }@b@ retV=0;@b@ e.printStackTrace();@b@ }finally{@b@ try {@b@ if(ps!=null){@b@ ps.close();@b@ }@b@ } catch (Exception ee) {@b@ } @b@ @b@ }@b@ return retV;@b@ }
调用用例如下
String selectSql="select content from TINFORMATION where object_id='"+objectId+"' ";@b@String updateSql="update TINFORMATION set content=? where object_id='"+objectId+"' "; @b@DBUtilExt.updateCLOB(selectSql, updateSql, 1, StringUtil.filterSstrToX(jarTreeContext));
2、修改后方法如下
public static void updateExt(String sql, Object[] params,String clobContext){ @b@ PreparedStatement pst = null;@b@ try {@b@ pst = conn.prepareStatement(sql);@b@ if (params != null)@b@ for (int i = 0; i < params.length; ++i){@b@ pst.setObject(i + 1, params[i]);@b@ } @b@ StringReader reader = new StringReader(clobContext); @b@ pst.setCharacterStream(1,reader,clobContext.length());@b@ pst.executeUpdate();@b@ conn.commit();@b@ }catch (Exception e) {@b@ try {@b@ conn.rollback();@b@ } catch (SQLException e1) {@b@ e1.printStackTrace();@b@ } @b@ e.printStackTrace();@b@ }finally{@b@ try {@b@ if(pst!=null){@b@ pst.close();@b@ }@b@ } catch (Exception ee) {@b@ } @b@ @b@ } @b@ @b@}
调用用例如下
insertObjSQL = " insert into tinformation(object_id,title,tab_name,content_type,keywords,zhaiyao,cdn_url,maven_url,jar_group_id,jar_art_id,jar_version,source_url,content)"@b@ + "values('"@b@ + objId@b@ + "',"@b@ + "'"@b@ + jardata.getJar_name()@b@ + "','info','0','"@b@ + kwords@b@ + "','"@b@ + zhaiyao@b@ + "',"@b@ + "'"@b@ + jardata.getDefault_url()@b@ + "','"@b@ + jardata.getMaven_url()@b@ + "','"@b@ + jardata.getGroup_name()@b@ + "','"@b@ + jardata.getArtifact_id()@b@ + "','"@b@ + jardata.getJar_version()@b@ + "','"@b@ + jardata.getSourceUrl() + "',?)";@b@ @b@DBUtilExt.updateExt(insertObjSQL, new Object[] {}, clobContext);