Oracle的CLOB大数据字段类型

public boolean save(Article article){ boolean result = true; Connection conn = ConnectionUntils.getInstance(); String sql = "insert into temp values(?,?,empty_clob())"; //锁住该列,防止并发写入时候该字段同时被多次写入造成错误 String sqlClob = "select temp_clob from temp t where t.name=? for update"; PreparedStatement pst =null; ResultSet rs = null; Writer writer = null; try { conn.setAutoCommit(false);//设置不自动提交,开启事务 pst = conn.prepareStatement(sql); pst.setString(1,article.getName()); pst.setString(2,article.getAge()); pst.executeUpdate(); pst= conn.prepareStatement(sqlClob); pst.setInt(1, article.getId()); rs = pst.executeQuery(); CLOB clob = null; if(rs.next()){ try { clob = (CLOB) rs.getClob(1); writer = clob.getCharacterOutputStream(); //拿到clob的字符输入流 writer.write(article.getContent()); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } conn.commit(); } catch (SQLException e) { result = false; try { conn.rollback();//当commit或者rollback后会自动释放该列的锁定 } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { conn.setAutoCommit(true);//还原 ConnectionUntils.close(rs, pst, conn); } return result; } 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/852e3ec639bf32097527719357680069.html