插入資料庫
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());}