现在的位置: 首页 > 综合 > 正文

java读取Oracle中Clob字段

2014年01月24日 ⁄ 综合 ⁄ 共 1269字 ⁄ 字号 评论关闭

-----------从ORACLE读取Clob字段------------

这是在grails中书写的。各位借鉴的时候需要修改少许。

一。得到连接

  def dbDriver = "oracle.jdbc.driver.OracleDriver";
  def url =  "jdbc:oracle:thin:@xxxxxx";
  def username = "foi";
  def password = "foi";
  Connection conn = null;
  Class.forName(dbDriver);
  DriverManager.setLoginTimeout(30);
  conn = DriverManager.getConnection(url, username, password);

 

二。写查询语句

def sql="""select n.id,
         n.create_date,
         n.create_id,
         (select a.USER_REAL_NAME from auth_user a where id=n.create_id) as create_name,
         n.publish_time,
         n.scan_times,
         n.title,
         n.info_body
    from notice n where n.record_state=0 and n.id=1;

 

三。预编译

def pstmt = conn.prepareStatement(sql);
  ResultSet result = pstmt.executeQuery();
  def resultList=[];
  java.sql.Clob clob=null;
  StringBuilder sb=new StringBuilder();
  if (result.next()) {
   resultList[0]=result.getString("create_name");
   resultList[1]=result.getString("publish_time");
   resultList[2]=result.getString("title");
   clob = result.getClob("info_body");
   Reader is = clob.getCharacterStream();// 得到流
   BufferedReader br = new BufferedReader(is);
   String s = br.readLine();
   while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
   sb.append(s);
   s = br.readLine();
   }
   resultList[3]=sb.toString();
   resultList[4]=result.getInt("scan_times");
  }

抱歉!评论已关闭.