当前位置: 技术问答>java相关
用jsp写Oracle里的clob型字段的问题,求救!!!
来源: 互联网 发布时间:2015-07-01
本文导语: 我想从网页的textarea元素提交一个大的字符串,然后用jsp(或javabean)写到clob型字段中,不知该如何做,不知那位高人这样做过,能否共享一下代码? 我用存储过程试过,结果存储非常慢,不知用上述方法是否能够提...
我想从网页的textarea元素提交一个大的字符串,然后用jsp(或javabean)写到clob型字段中,不知该如何做,不知那位高人这样做过,能否共享一下代码?
我用存储过程试过,结果存储非常慢,不知用上述方法是否能够提高效率?
我用存储过程试过,结果存储非常慢,不知用上述方法是否能够提高效率?
|
用oracle jdbc 2.0来实现
写入
先做insert,但不操作Clob字段,返回一个刚才插入的纪录的ID
然后再跟心这个ID的纪录,把CLOB写进去
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT CLOBTYPECOL "+
"FROM TABLE "+
"WHERE ID='"+ID+"' FOR UPDATE");
if (rs.next()) {
Clob l_clob = rs.getClob(1);
((oracle.sql.CLOB)l_clob).plsql_write(((oracle.sql.CLOB)l_clob).plsql_length()+1,strTOInsert);
}
stmt.close();
读取:
Clob clo = rs.getClob("dada");
String str = clo.getSubString(1,(int)clo.length());
|
public String setClob(String sqlStr,String clobName,String info){
String result=null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
java.io.Writer writer;
char[] data=new char[info.length()];
try{
JdbcConnect myConn=new JdbcConnect();
conn=myConn.getConn();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sqlStr,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery();
if (rs.next())
{
try{
Clob clob = rs.getClob(clobName);
writer=((CLOB)clob).getCharacterOutputStream();
info.getChars(0,info.length(),data,0);
writer.write(data);
writer.flush();
writer.close();
conn.commit();
}catch(Exception e)
{
conn.rollback();
result=e.toString();
System.out.println(e.toString());
}
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
System.out.println("建立Statement时错误:"+e.toString());
result="建立Statement时错误:"+e.toString();
}
return result;
}
String result=null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
java.io.Writer writer;
char[] data=new char[info.length()];
try{
JdbcConnect myConn=new JdbcConnect();
conn=myConn.getConn();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sqlStr,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery();
if (rs.next())
{
try{
Clob clob = rs.getClob(clobName);
writer=((CLOB)clob).getCharacterOutputStream();
info.getChars(0,info.length(),data,0);
writer.write(data);
writer.flush();
writer.close();
conn.commit();
}catch(Exception e)
{
conn.rollback();
result=e.toString();
System.out.println(e.toString());
}
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
System.out.println("建立Statement时错误:"+e.toString());
result="建立Statement时错误:"+e.toString();
}
return result;
}
|
我是用PreparedStatement的setObject(int parameterIndex, Object x)方法做的,把你读入的string当作一个object放到这个字段,然后读的时候也用getobject方法读出来再转成string。速度慢可能是因为网页上传的大量文本经http传输的慢。