免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2511 | 回复: 0
打印 上一主题 下一主题

java读写clob [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-06 21:33 |只看该作者 |倒序浏览
java读写clob



从数据库中读取clob值

  1. public String clob2String(Clob clob)
  2. {

  3. StringBuffer retValue=new StringBuffer();
  4. if(clob!=null)
  5. {
  6. try {
  7. Reader reader=clob.getCharacterStream();
  8. BufferedReader in=new BufferedReader(reader);
  9. String s=in.readLine();
  10. while(s!=null)
  11. {
  12. retValue.append(s);
  13. s=in.readLine();
  14. }
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }catch (IOException e) {
  18. }
  19. }
  20. return retValue.toString();
  21. }

  22. 将string存储到数据库
  23. public void writeClob(String s)
  24. {
  25. Connection conn=null;
  26. ResultSet rs=null;
  27. try {
  28. java.sql.Statement stmt=conn.createStatement();
  29. boolean flag=conn.getAutoCommit();
  30. conn.setAutoCommit(false);
  31. String sql="update tableName set clobCloumn=empty_Clob()";
  32. String updateSql="select clobCloumn from tableName for update";
  33. stmt.executeUpdate(sql);
  34. rs=stmt.executeQuery(updateSql);
  35. while(rs.next())
  36. {
  37. Writer writer=rs.getClob("clobCloumn").setCharacterStream(1);
  38. writer.write(s);
  39. writer.flush();
  40. writer.close();
  41. }

  42. conn.commit();
  43. conn.setAutoCommit(flag);
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. try {
  48. conn.rollback();
  49. } catch (SQLException e1) {
  50. e1.printStackTrace();
  51. }
  52. e.printStackTrace();
  53. }
  54. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP