using CLOB in java throwing exception

2019-07-26 10:14发布

问题:

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");}

this is throwing an exception

java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createClob()Ljava/sql/Clob;

whats the problem here? and How can I solve it?

回答1:

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();

This also works with an INSERT statement. No need to create any intermediate clob (or blob) objects.

Note that I changed the wrong index 8 to the correct index 2 to match the placeholders in the UPDATE statement.

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 jdbc clob