import java.sql.*;
import java.util.Properties;

/**
 * Test case to produce the following NullPointerException
 * when using mysql jdbc driver version 5.0.5:<br/>
 * java.lang.NullPointerException<br/>
 * &nbsp;&nbsp;at com.mysql.jdbc.Connection.initializeResultsMetadataFromCache(Connection.java:5882)<br/>
 * &nbsp;&nbsp;at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:853)<br/>
 * &nbsp;&nbsp;at ....<br/>
 */
public final class TestMysqlJdbc505Bug
{
  private static final String MYURL =
    "jdbc:mysql://localhost:3306/test";

  public static void main(String[] args)
  {
    Properties props = new Properties();
    Connection conn = null;
    PreparedStatement ps = null;
    try
    {
      DriverManager.registerDriver((Driver)
        Class.forName("com.mysql.jdbc.Driver").newInstance());
      props.put("user", "root");
      props.put("password", "");
      props.put("cachePrepStmts", "true");
      props.put("useServerPrepStmts", "false");
      props.put("cacheResultSetMetadata", "true");
      conn = DriverManager.getConnection(MYURL, props);
      ps = conn.prepareStatement( "select 1" );
      // it fails in the below line when using 5.0.5 driver, 5.0.4 is ok.
      ps.execute();
      // ps.executeQuery instead of ps.execute runs correctly, does not fail.
      // sun's jdk 1.4.2_13 and 1.5.0_10 makes no difference.
      // when cacheResultSetMetaData set to false, ps.execute does not fail.
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (ps != null) ps.close();
        if (conn != null) conn.close();
      }
      catch (SQLException e)
      {
        System.err.println( e );
      }
    }
  }
}
