Bug #23310 ResultSetMetaData.getColumnName() returns null
Submitted: 15 Oct 2006 20:44 Modified: 17 Oct 2006 10:54
Reporter: Alec Gonzales Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.0.3 OS:Windows (XP)
Assigned to: CPU Architecture:Any

[15 Oct 2006 20:44] Alec Gonzales
Description:
ResultSetMetaData.getColumnName() function sometimes returns null instead of the proper column name.

from my experience, it returns null when querying from dual and using count() funciton in select statement.

How to repeat:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

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

    ResultSet rs = null;
    PreparedStatement ps = null;
		
	try{
	  ps = conn.prepareStatement("SELECT to_days('2006-10-15') day 	FROM dual");
	  rs = ps.executeQuery();
	  
	  // get result details
	  ResultSetMetaData rsmd = rs.getMetaData();
	  int numColumns = rsmd.getColumnCount();
	  String[] columnNames = new String[numColumns];
	  for (int i=0;i<numColumns;i++){
	    columnNames[i] = rsmd.getColumnName(i+1);
  	    System.out.println("column name: " + rsmd.getColumnName(i+1));
	  }
	}finally {
	  if (rs != null) {
	    try {rs.close();} 
	    catch (SQLException sqlEx) {}
	    rs = null;
	  }
      if (ps != null) {
        try {ps.close();}
        catch (SQLException sqlEx) {}
	    ps = null;
	  }
	  conn.close();
	}
  }
}

Suggested fix:
ResultSetMetaData.getColumnName() should still return the column name regardless of what table is queried.
[16 Oct 2006 2:40] Mark Matthews
Please test with a nightly snapshot build of 5.0 (from http://downloads.mysql.com/snapshots.php), which has fixes related to result set metadata to deal with missing metadata from the server.
[17 Oct 2006 10:54] Alec Gonzales
the bug's fixed with the mysql-connector-java-5.0-nightly-20061016-bin version.

kudos