在Java中使用CLOB引发异常 [英] using CLOB in java throwing exception
问题描述
我正在将Clob数据插入mysql数据库...这是我的代码
I am inserting clob data into mysql database...here is my code
Clob cl=dbCon.createClob();
cl.setString(1,userAbout);
dbCon.setAutoCommit(false);
PreparedStatement insertClob=dbCon.prepareStatement("UPDATE user_data SET user_about=? WHERE user_id=?");
insertClob.setClob(1,cl);
insertClob.setInt(2,userId);
int count= insertClob.executeUpdate();
if(count==1){dbCon.commit();dbCon.close();out.write("success");}
else{dbCon.rollback();dbCon.close();out.print("error");}
这引发了异常
java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createClob()Ljava/sql/Clob;
这是什么问题?以及我该如何解决?
whats the problem here? and How can I solve it?
推荐答案
无论如何您都不需要createClob()
.我发现使用setCharacterStream()更加稳定(并且所有JDBC驱动程序都提供了更好的支持).
You don't need createClob()
anyway. I find using setCharacterStream() to be much more stable (and much better supported by all JDBC drivers).
StringReader reader = new StringReader(userAbout);
PreparedStatement insertClob = dbCon.prepareStatement("UPDATE user_data SET user_about=? WHERE user_id=?");
insertClob.setCharacterStream(1, reader, userAbout.length());
insertClob.setInt(2,userId);
int count= insertClob.executeUpdate();
这也可以与INSERT
语句一起使用.无需创建任何中间clob(或blob)对象.
This also works with an INSERT
statement. No need to create any intermediate clob (or blob) objects.
请注意,我将错误的索引8
更改为正确的索引2
,以匹配UPDATE
语句中的占位符.
Note that I changed the wrong index 8
to the correct index 2
to match the placeholders in the UPDATE
statement.
对于CLOB列,许多现代驱动程序也可以处理简单" setString()
.值得尝试-可以减少更多代码.
Many modern drivers also handle a "simple" setString()
just as well for CLOB columns. It's worth trying out - would reduce the code even more.
这篇关于在Java中使用CLOB引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!