java操作Oracle的CLOB类型
2014-10-24 10:58
405 查看
今天同事问我是否用过oracle中的CLOB类型,说实话-没听过。
百度了解了下,CLOB是oracle中用于存储文字的大字段类型、当需要存入大数据量(大于4000)时,varchar2不够用,可以使用clob。
Oracle中SQL语句中的两个单引号之间的字符数不能大于4000的限制。'" +
data + "' data在sql语句之间,当data的值大于4000个字节时就会报错。
ORACLE - 往有CLOB类型的表中插数据
1.其他字段照常插入,CLOB类型字段先插入"empty_clob()"值
( )。
2.把自动提交设为false,否则,再次查找时就不能正确更新
2.然后使用带有“for
update”的查询语句锁定更新行。
3.最后实例化输出流并对CLOB类型字段数据进行插入操作。
tips:
1.CLOB类型的空对象为empty_clob();
2.如果在插入前没有将自动提交设为false会报
“fetch out of sequence”;
3.如果不加for
update会报:“row containing the LOB value is not locked”;
关键代码:
ORACLE
- 查询带CLOB类型的数据,并把CLOB数据转为String类型
关键代码:
关键代码:
String notice_content=ClobToString(rs.getClob("str"));
关键代码:
百度了解了下,CLOB是oracle中用于存储文字的大字段类型、当需要存入大数据量(大于4000)时,varchar2不够用,可以使用clob。
Oracle中SQL语句中的两个单引号之间的字符数不能大于4000的限制。'" +
data + "' data在sql语句之间,当data的值大于4000个字节时就会报错。
ORACLE - 往有CLOB类型的表中插数据
1.其他字段照常插入,CLOB类型字段先插入"empty_clob()"值
( )。
2.把自动提交设为false,否则,再次查找时就不能正确更新
2.然后使用带有“for
update”的查询语句锁定更新行。
3.最后实例化输出流并对CLOB类型字段数据进行插入操作。
tips:
1.CLOB类型的空对象为empty_clob();
2.如果在插入前没有将自动提交设为false会报
“fetch out of sequence”;
3.如果不加for
update会报:“row containing the LOB value is not locked”;
关键代码:
StringBuffer sqlstr = new StringBuffer(); sqlstr.append("INSERT INTO notice t (t.notice_id,t.notice_type,t.notice_title," + "t.notice_status,t.sender,t.eff_date,t.exp_date,t.creater,t.create_time," + "t.notice_content) VALUES ("); sqlstr.append(+notice.getNoticeId() + ","); sqlstr.append("'" + notice.getNoticeType() + "',"); sqlstr.append("'" + notice.getNoticeTitle() + "',"); sqlstr.append("'" + notice.getNoticeStatus() + "',"); sqlstr.append("'" + notice.getSender() + "',"); sqlstr.append("sysdate,"); sqlstr.append("to_date('" + theForm.getEndDate2() + " 23:59:59','yyyy-MM-dd HH24:mi:ss'),"); sqlstr.append("'" + notice.getCreater() + "',"); sqlstr.append("sysdate,"); sqlstr.append("empty_clob()");//插入空值 sqlstr.append(")"); String sql = sqlstr.toString(); Class.forName(driverClassName); Connection con = DriverManager.getConnection(url, username, password); con.setAutoCommit(false);//设置不自动提交 PreparedStatement pstmt = con.prepareStatement(sql); String content = notice.getNoticeContent().replace('\'', '\"'); try { pstmt.execute(); con.commit();//插入 pstmt = con.prepareStatement("select notice_content from notice where notice_id="+notice.getNoticeId()+" for update");//查找刚刚插入的那条记录 for update ResultSet res = pstmt.executeQuery(); CLOB clob = null; while(res.next()){ clob = (oracle.sql.CLOB)res.getClob(1); clob.putString(1, content);//content为前台提交过来的公告内容,大数据量 } pstmt = con.prepareStatement("update notice set notice_content = ? where notice_id=?");//修改notice_content字段的内容 pstmt.setClob(1, clob); pstmt.setLong(2, notice.getNoticeId()); pstmt.executeUpdate(); con.commit(); if (nps != null && nps.size() > 0) { noticePartnerDao.saveOrUpdateAll(nps); } } catch (Exception e) { info.setCode("1"); info.setFlag(false); info.setInfo("发布公告失败!"); System.out.println(e.getMessage()); System.out.println(e.getStackTrace()); FileLog.errorLog(e, "发布公告失败"); return info; } finally { if(pstmt!=null) { pstmt.close(); } if(con!=null) { con.close(); } }
ORACLE
- 查询带CLOB类型的数据,并把CLOB数据转为String类型
关键代码:
StringBuffer sqlstr = new StringBuffer(); sqlstr.append("INSERT INTO notice t (t.notice_id,t.notice_type,t.notice_title," + "t.notice_status,t.sender,t.eff_date,t.exp_date,t.creater,t.create_time," + "t.notice_content) VALUES ("); sqlstr.append(+notice.getNoticeId() + ","); sqlstr.append("'" + notice.getNoticeType() + "',"); sqlstr.append("'" + notice.getNoticeTitle() + "',"); sqlstr.append("'" + notice.getNoticeStatus() + "',"); sqlstr.append("'" + notice.getSender() + "',"); sqlstr.append("sysdate,"); sqlstr.append("to_date('" + theForm.getEndDate2() + " 23:59:59','yyyy-MM-dd HH24:mi:ss'),"); sqlstr.append("'" + notice.getCreater() + "',"); sqlstr.append("sysdate,"); sqlstr.append("empty_clob()");//插入空值 sqlstr.append(")"); String sql = sqlstr.toString(); Class.forName(driverClassName); Connection con = DriverManager.getConnection(url, username, password); con.setAutoCommit(false);//设置不自动提交 PreparedStatement pstmt = con.prepareStatement(sql); String content = notice.getNoticeContent().replace('\'', '\"'); try { pstmt.execute(); con.commit();//插入 pstmt = con.prepareStatement("select notice_content from notice where notice_id="+notice.getNoticeId()+" for update");//查找刚刚插入的那条记录 for update ResultSet res = pstmt.executeQuery(); CLOB clob = null; while(res.next()){ clob = (oracle.sql.CLOB)res.getClob(1); clob.putString(1, content);//content为前台提交过来的公告内容,大数据量 } pstmt = con.prepareStatement("update notice set notice_content = ? where notice_id=?");//修改notice_content字段的内容 pstmt.setClob(1, clob); pstmt.setLong(2, notice.getNoticeId()); pstmt.executeUpdate(); con.commit(); if (nps != null && nps.size() > 0) { noticePartnerDao.saveOrUpdateAll(nps); } } catch (Exception e) { info.setCode("1"); info.setFlag(false); info.setInfo("发布公告失败!"); System.out.println(e.getMessage()); System.out.println(e.getStackTrace()); FileLog.errorLog(e, "发布公告失败"); return info; } finally { if(pstmt!=null) { pstmt.close(); } if(con!=null) { con.close(); } }
关键代码:
//oracle.sql.Clob类型转换成String类型
public String ClobToString(Clob clob) { String reString = ""; Reader is = null; try { is = clob.getCharacterStream(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 得到流 BufferedReader br = new BufferedReader(is); String s = null; try { s = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuffer sb = new StringBuffer(); while (s != null) { //执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING sb.append(s); try { s = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } reString = sb.toString(); return reString; }
String notice_content=ClobToString(rs.getClob("str"));
关键代码:
StringBuffer sqlstr = new StringBuffer(); sqlstr.append("INSERT INTO notice t (t.notice_id,t.notice_type,t.notice_title," + "t.notice_status,t.sender,t.eff_date,t.exp_date,t.creater,t.create_time," + "t.notice_content) VALUES ("); sqlstr.append(+notice.getNoticeId() + ","); sqlstr.append("'" + notice.getNoticeType() + "',"); sqlstr.append("'" + notice.getNoticeTitle() + "',"); sqlstr.append("'" + notice.getNoticeStatus() + "',"); sqlstr.append("'" + notice.getSender() + "',"); sqlstr.append("sysdate,"); sqlstr.append("to_date('" + theForm.getEndDate2() + " 23:59:59','yyyy-MM-dd HH24:mi:ss'),"); sqlstr.append("'" + notice.getCreater() + "',"); sqlstr.append("sysdate,"); sqlstr.append("empty_clob()");//插入空值 sqlstr.append(")"); String sql = sqlstr.toString(); Class.forName(driverClassName); Connection con = DriverManager.getConnection(url, username, password); con.setAutoCommit(false);//设置不自动提交 PreparedStatement pstmt = con.prepareStatement(sql); String content = notice.getNoticeContent().replace('\'', '\"'); try { pstmt.execute(); con.commit();//插入 pstmt = con.prepareStatement("select notice_content from notice where notice_id="+notice.getNoticeId()+" for update");//查找刚刚插入的那条记录 for update ResultSet res = pstmt.executeQuery(); CLOB clob = null; while(res.next()){ clob = (oracle.sql.CLOB)res.getClob(1); clob.putString(1, content);//content为前台提交过来的公告内容,大数据量 } pstmt = con.prepareStatement("update notice set notice_content = ? where notice_id=?");//修改notice_content字段的内容 pstmt.setClob(1, clob); pstmt.setLong(2, notice.getNoticeId()); pstmt.executeUpdate(); con.commit(); if (nps != null && nps.size() > 0) { noticePartnerDao.saveOrUpdateAll(nps); } } catch (Exception e) { info.setCode("1"); info.setFlag(false); info.setInfo("发布公告失败!"); System.out.println(e.getMessage()); System.out.println(e.getStackTrace()); FileLog.errorLog(e, "发布公告失败"); return info; } finally { if(pstmt!=null) { pstmt.close(); } if(con!=null) { con.close(); } }
相关文章推荐
- java 关于操作oracle的clob类型(inster ,update,select)的完整例程
- java操作 oracle 的 blob和clob类型
- java操作oracle存取clob类型数据
- oracle使用java操作clob类型的字段
- java 关于操作oracle的clob类型
- JAVA对Oracle中BLOB、CLOB类型字段的操作说明
- 环境:JAVA操作Oracle中BLOB、CLOB
- 使用java + oracle插入clob类型的数据
- JAVA操作Oracle中CLOB字段
- java语言操作Oracle数据库中的CLOB数据类型
- J2EE操作Oracle的clob类型字段
- 【转】oracle对大对象类型操作:blob,clob,nclob,bfile
- 使用java + oracle插入clob类型的数据
- hibernate在SSH框架操作ORACLE Clob字段类型的读写改
- 使用java + oracle插入clob类型的数据
- 【摘抄】oracle对大对象类型操作:blob,clob,nclob,bfile
- JAVA 操作 Oracle Clob,Blob
- java语言操作Oracle数据库中的CLOB数据类型
- Oracle中对clob类型字段的操作
- Java 存储和读取 oracle CLOB 类型字段