Jun 04

If you are pulling a value from a database ResultSet it is somethimes not possible to check the value returned to determine if the value in the database was null. In these cases you can use the wasNull() method to check.
The wasNull() method will return true if the value in the database was NULL for the last value retrieved from ResultSet.

Here is an example of its usage.

ResultSet rs = statement.executeQuery();
int value = rs.getInt(1);
if (rs.wasNull(1))
{
   // Database contains NULL
}

written by objects \\ tags: , ,

Apr 01

When using the mysql jdbc drivers to run queries with that return large result sets you are very likely to get an OutOfMemoryError. Reason for this is that by default the mysql jdbc driver implementation tries to store the entire result set in memory. This is for performance reasons, with the assumption that the majority of result sets are relatively small and will fit in memory (which is fair enough).

If you do have a large result set then this is just not going to work and you will either get an OutOfMemoryError, or at best your application footprint drastically increases and slows to a crawl. Solution is to stream the result set, which is done by using a read only, forward only statement and setting the fetch size to Integer.MIN_VALUE.

Statement statement = connection.createStatement(
    java.sql.ResultSet.TYPE_FORWARD_ONLY,
    java.sql.ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(Integer.MIN_VALUE);

Downside to this is that you can’t use the connection for anything else until your streaming result set has been closed.

written by objects \\ tags: , , ,

Feb 04

Depending on your jdbc driver version the following code can fail to run.

File file = new File("image.jpg");
InputStream in = new FileInputStream(file);
preparedStatment.setBinaryStream(1, in, file.length());

The error you will see is as follows.

java.lang.AbstractMethodError: com.mysql.jdbc.ServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V

To fix this problem you need to change the call to setBinaryStream so the last parameter is passed as an integer instead of a long.

File file = new File("image.jpg");
InputStream in = new FileInputStream(file);
preparedStatment.setBinaryStream(1, in, (int) file.length());

written by objects \\ tags: , , ,