Oracle插入大数据、获取大数据

插入数据库

CLOB tempClob = null;
try {
  Connection conn = getConnection();
  PreparedStatement = = conn.prepareStatement("UPDATE PROGRAM_HISTORY SET DETAILS = ? WHERE ID = 12");
  tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
  tempClob.open(CLOB.MODE_READWRITE);
  Writer tempClobWriter = tempClob.getCharacterOutputStream();
  tempClobWriter.write(clobData);
  tempClobWriter.flush();
  tempClobWriter.close();
  tempClob.close();

 pStmt.setClob(1, tempClob);
 pStmt.execute();

} catch (Exception ex) { // Trap errors
  System.out.println(" Error Inserting Clob : "+ex.toString());
  ex.printStackTrace();
}finally{
  if(tempClob != null ) tempClob.freeTemporary();
  opstmt.close();
  conn.close();

}

 获取

Oracle rs.getClob("")转String

oracle数据中定义为clob类型的字段,如果用rs.getString()方法获取,值一直为nul,无法取出

用rs.getClob()取出来为Clob型

需把Clob型转为String类型

第一种方法:

rs = pstmt.executeQuery();
            while(rs.next()){
                Reader reader = rs.getClob("afterStepScript")
                        .getCharacterStream();
                BufferedReader r = new BufferedReader(reader);
                StringBuffer sb = new StringBuffer();
                String line;
                try {
                    while ((line = r.readLine()) != null) {

                        sb.append(line);
                        //sb.append("\r\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                String afterStepScript=sb.toString();

 第二种方法

Clob clob=rs.getClob("insideScript");
String insideScript="";
if(clob!=null){
   insideScript=clob.getSubString((long)1, (int)clob.length());
}

 

posted on 2013-03-21 09:26  seaven  阅读(891)  评论(0编辑  收藏  举报

导航