前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java之基础操作Oracle CLOB

Java之基础操作Oracle CLOB

原创
作者头像
IT工作者
发布2022-03-29 14:44:57
7950
发布2022-03-29 14:44:57
举报
文章被收录于专栏:程序技术知识
代码语言:javascript
复制
//取CLOB中的内容(不包含图片)
  public String query(String strQuery) throws SQLException{
  if (con==null || con.isClosed()) {
  pool = ConnectionPool.getInstance();
  con = pool.getConnection();
  }
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery(strQuery);
  String line = "" ;
  String content = "";
  while (rs.next()){
  Clob clob = rs.getClob("content"); //和提取一般对象一样
  // InputStream is = rs.getAsciiStream(1); //特殊的,对于与得到Clob的流
  // BufferedReader br = new BufferedReader(new InputStreamReader(is));
  //
  // while(null != (line = br.readLine())){
  // //System.out.println(line);
  // content += line;
  // }
  // is.close();
  long longLen = clob.length();
  content = clob.getSubString(1L, (int) longLen);
  }
  rs.close();
  st.close();
  con.close();
  return content;
  }
  //写入CLOB
  public Boolean update(DynaActionForm listForm) throws SQLException, IOException {
  // TODO 自动生成方法存根
  String content = (String) listForm.get("content");
  try {
  if (con==null || con.isClosed()) {
  pool = ConnectionPool.getInstance();
  con = pool.getConnection();
  }
  con.setAutoCommit(false);
  //插入一个空CLOB
  //String insertSql = "insert into Client_PUBBLOB(id,content) values('1',empty_clob())";
  //查询插入的空CLOB
  String selectSql = "select content from Client_PUBBLOB where id = '1' for update";
  //PreparedStatement stmt = con.prepareStatement(insertSql);
  //stmt.executeUpdate();
  //stmt.close();
  //lock this line
  PreparedStatement pstmt = con.prepareStatement(selectSql);
  String emptyClobSql="update Client_PubBLOB set content=empty_clob() where id=1";
  PreparedStatement pstmt2=con.prepareStatement(emptyClobSql);
  pstmt2.executeUpdate();
  pstmt2.close();
  ResultSet rs = pstmt.executeQuery();
  if(rs.next()){
  oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);
  //System.out.println("要修改的content:" + content);
  //为CLOB写信息
  String sql1="update Client_PubBLOB set content=? where id=1";
  clob.putChars(1, content.toCharArray());
  // String s = clob.getSubString((long)1,(int)clob.length()-1);
  // System.out.println("当前的clob内容是: " + s);
  pstmt=con.prepareStatement(sql1);
  pstmt.setClob(1,clob);
  pstmt.executeUpdate();
  // oracle.sql.CLOB clob2 = (oracle.sql.CLOB)rs.getClob(1);
  // String s = clob.getSubString((long)1,(int)clob.length()-1);
  // System.out.println(s);
  }
  con.commit();
  pstmt.close();
  } catch (SQLException e) {
  con.rollback();
  } finally {
  con.setAutoCommit(true);
  con.close();
  }
  return listForm;
  }
  //不过貌似也可以直接写String进CLOB中,比如
  String data= dataInfo.getDetail();
  cs.setString(1, newsInfo.getSubject());
  cs.setString(2, newsInfo.getAuthor());
  cs.setString(3, newsdetail);

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档