您的位置:首页 > 其它

String怎么样可以转换成clob类型

2009-05-21 17:35 393 查看
这是代码

String kpiPrc=ParamUtils.getParameter(request, "kpi_prc", true);
StringBuffer kpiProc=new StringBuffer();
kpiProc.append(request.getParameter("kpi_proc".toString());
oracle.sql.CLOB clob=null;
try {
conn = ds.getConnection();
stmt=conn.createStatement();
conn.setAutoCommit(false);
sql="select kpi_proc from kpi_procedures where kpi_prc='"+kpiPrc+"'";
rs=stmt.executeQuery(sql);
while(rs.next()){
//取出CLOB对象
clob=(oracle.sql.CLOB)rs.getClob("kpi_proc");
}
BufferedWriter o=new BufferedWriter(clob.getCharacterOutputStream());
StringReader strReader=new StringReader(kpiProc.toString());
BufferedReader in=new BufferedReader(strReader);
int c=0;
while((c=in.read())!=-1){
o.write(c);
}
in.close();
o.close();
stmt.close();
stmt = null;
conn.close(); // Return to connection pool
conn = null; // Make sure we don't close it twice
} catch (SQLException e) {
err=1;
System.out.println(e.toString());
} finally {
// Always make sure result sets and statements are closed,
// and the connection is returned to the pool
if (rs != null){
try { rs.close(); } catch (SQLException e) { out.println(e.toString()); }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { out.println(e.toString()); }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { out.println(e.toString()); }
conn = null;
}
}

报错:row containing the LOB value is not locked

-----------------------------------------------------------------

用下面的方法可以将JAVA的STRING 转化成CLOB类型,不过好像仅限于ORACLE,其他的数据库上我没有试过。
private CLOB getCLOB( String clobData,Connection conn )
throws Exception {
CLOB tempClob = null; try {
// create a new temporary CLOB

tempClob = CLOB.createTemporary(getNativeConnection(conn) , false,
CLOB.DURATION_SESSION ); // Open the temporary CLOB in readwrite mode to enable writing
tempClob.open( CLOB.MODE_READWRITE );
// Get the output stream to write
Writer tempClobWriter = tempClob.getCharacterOutputStream( ); // Write the data into the temporary CLOB
tempClobWriter.write( clobData ); // Flush and close the stream
tempClobWriter.flush( );
tempClobWriter.close( ); // Close the temporary CLOB
tempClob.close( ); } catch ( Exception exp ) {
// Free CLOB object
throw exp;
//do something
}
return tempClob;
}

如果使用连接池来获得数据库连接,有可能需要将数据库连接进行一下转化,使用以下代码:
private static Connection getNativeConnection(Connection con) throws SQLException { if (con instanceof DelegatingConnection) {
Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate(); // For some reason, the innermost delegate can be null: not for a
// Statement''''s Connection but for the Connection handle returned by the pool.
// We''''ll fall back to the MetaData''''s Connection in this case, which is
// a native unwrapped Connection with Commons DBCP 1.1. return (nativeCon != null ? nativeCon : con.getMetaData().getConnection());
}
return con;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: