Tuesday, March 2, 2010

Storing binary stuff in database with tomcat

When storing binary data in a database, usually you do the following:

PreparedStatement pStmt= conn.prepareStatement("insert into FileData (FileData, BinData) values (?,?)");
pStmt.setString(1, fileDataID);
pStmt.setBinaryStream(2, fInfo.getInputStream(), fInfo.getSize());
pStmt.executeUpdate();


That works fine, but sometimes you can get this error when using it
with tomcat connection pooling:


java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V


The reason for this is, that you passed the last parameter as a long, instead of a int.

This is the fix for it:
PreparedStatement pStmt= conn.prepareStatement("insert into FileData (FileData, BinData) values (?,?)");
pStmt.setString(1, fileDataID);
pStmt.setBinaryStream(2, fInfo.getInputStream(), (int)fInfo.getSize());
pStmt.executeUpdate();


Took half a day to figure out the problem.

No comments:

Post a Comment