Bug #18554 ResultSetMetaData.getColumnName() returns garbage chars w/ long column names
Submitted: 27 Mar 2006 23:49 Modified: 5 Apr 2006 22:14
Reporter: Brad Jackson
Status: Closed
Category:Connector/J Severity:S2 (Serious)
Version:3.1.12 OS:Microsoft Windows (Windows XP)
Assigned to: Mark Matthews Target Version:

[27 Mar 2006 23:49] Brad Jackson
Description:
I wrote a query with a large case statement that was several hundred characters long and
didn't have an "as fieldname" to use a column alias. The code that builds the SQL and
calls ResultSet.getDouble(fieldName) started throwing an exception that said the column
wasn't found. I wrote some test code that calls ResultSetMetaData.getColumnName() and the
name of the field had two garbage characters in front of the case statement and the field
name was truncated. The problem only happens when the field name is over 250 characters.

I am using the Connector/J 3.1.12 jar and 4.1.15 server on Windows XP. I cannot tell if
this is a client or server bug.

How to repeat:
Here is test code to reproduce the problem.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class BugTest {

	public static void main(String args[]) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs   = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost/dbname", "username",
"password");
			stmt = con.createStatement();

			String colName =
"a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a1234567891";
			rs = stmt.executeQuery("select curtime() as " + colName);
			ResultSetMetaData meta = rs.getMetaData();

			System.out.println("Original column name: " + colName);
			System.out.println("Original column name length: " + colName.length());
			System.out.println("Meta data column name: " + meta.getColumnName(1));
			System.out.println("Meta data column length: " + meta.getColumnName(1).length());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)   rs.close();
				if (stmt != null) stmt.close();
				if (con != null)  con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
[28 Mar 2006 18:51] Tonci Grgin
Thanks for your bug report. Does this help:
http://dev.mysql.com/doc/refman/4.1/en/legal-names.html
[28 Mar 2006 19:46] Brad Jackson
I have worked around the problem by using a column alias for my large case statement, but
there is still a bug with the two garbage characters (such as รป and a null) that are
prepended to the meta data column name when it is over 250 characters long.
[3 Apr 2006 20:22] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/4421