Bug #13277 ResultSetMetaData invalid when using RETURN_GENERATED_KEYS
Submitted: 16 Sep 2005 18:20 Modified: 16 Sep 2005 20:14
Reporter: k self Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.1.10 OS:Microsoft Windows (Windows XP)
Assigned to: Mark Matthews CPU Architecture:Any

[16 Sep 2005 18:20] k self
Description:
When using the function:

prepareStatement("SQL STATEMENT",Statement.RETURN_GENERATED_KEYS);
...
and obtaining the ResultSetMetaData object from the generated keys resultset (using the getGeneratedKeys() and the getMetaData() functions), the following
functions (and possibly others) result in an error:

getColumnDisplaySize()
isDefinitelyWritable()
isReadOnly()
isWritable()

The error generated is:

java.lang.NullPointerException
	at com.mysql.jdbc.Field.getMaxBytesPerCharacter(Field.java:437)
	at com.mysql.jdbc.ResultSetMetaData.getColumnDisplaySize(ResultSetMetaData.java:307)
	at hindsight.Test.main(Test.java:22)

This error is repeatable, and an example program that generates this problem is shown in the 'how to repeat' section below (with changes to the SQL statement and database name and login information).

How to repeat:
import java.sql.*;

public class Test 
{
 public static void main(String[] args) throws Exception 
 {
  try
  {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/hindsight?user=hindsight&password=hindsight");

            PreparedStatement stmt  = conn.prepareStatement("insert into TradeRange(tradeID,startVolume,endVolume,variablePrice,fixedPrice,useFixed) values(1,200,250,10,10,1);",Statement.RETURN_GENERATED_KEYS);
   stmt.executeUpdate();

   ResultSet lrs = stmt.getGeneratedKeys();

   if(lrs.next()) 
      {
    ResultSetMetaData lrsMeta = lrs.getMetaData();
    int liDSize = lrsMeta.getColumnDisplaySize(1);
    
    System.out.println(liDSize);
      }
            lrs.close();
            conn.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

Suggested fix:
There are no work arounds that have been found.
[16 Sep 2005 20:14] Mark Matthews
Fixed in source repo for 3.1.11 and 5.0.0. See http://downloads.mysql.com/snapshots.php#connector-j after 00:00 GMT Sept 17th for a nightly build which contains a fix for this issue.

Thanks for the bug report.