Bug #18554 ResultSetMetaData.getColumnName() returns garbage chars w/ long column names
Submitted: 27 Mar 2006 21:49 Modified: 5 Apr 2006 20:14
Reporter: Brad Jackson Email Updates:
Status: Closed Impact on me:
Category:Connector / J Severity:S2 (Serious)
Version:3.1.12 OS:Windows (Windows XP)
Assigned to: Mark Matthews CPU Architecture:Any

[27 Mar 2006 21:49] Brad Jackson
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 {
			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) {
		} finally {
			try {
				if (rs != null)   rs.close();
				if (stmt != null) stmt.close();
				if (con != null)  con.close();
			} catch (SQLException e) {
[28 Mar 2006 16:51] Tonci Grgin
Thanks for your bug report. Does this help:
[28 Mar 2006 17: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 18: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: