This discussion is archived
6 Replies Latest reply: Sep 7, 2012 2:37 PM by rp0428 RSS

getColumnName() returns aliases not the original column name

user11179792 Newbie
Currently Being Moderated
It looks like I am getting the aliases as specified by the AS clause not the original column name from java.sql.ResultSetMetadata.getColumnName(). Is this the expected behavior of JDBC 4.0?
  • 1. Re: getColumnName() returns aliases not the original column name
    rp0428 Guru
    Currently Being Moderated
    >
    It looks like I am getting the aliases as specified by the AS clause not the original column name from java.sql.ResultSetMetadata.getColumnName(). Is this the expected behavior of JDBC 4.0?
    >
    Yes - that has always been expected behavior.
            stmt = con.prepareStatement("SELECT empno, empno as eno1, empno as eno2 FROM EMP");
            rs = stmt.executeQuery();
    
            ResultSetMetaData rsmd = rs.getMetaData();
             System.out.println(rsmd.getColumnName(1));
             System.out.println(rsmd.getColumnName(2));
             System.out.println(rsmd.getColumnName(3));
    
    EMPNO
    ENO1
    ENO2
  • 2. Re: getColumnName() returns aliases not the original column name
    user11179792 Newbie
    Currently Being Moderated
    Thank you for the reply!

    We have a requirement to get the actual column name instead of the alias. I was wondering if there is a way to do that.
  • 3. Re: getColumnName() returns aliases not the original column name
    DrClap Expert
    Currently Being Moderated
    When you say "the actual column name" that suggests you are not aware that it's possible for the subject of the alias to be an arbitrary expression. It doesn't have to be a single column name.
  • 4. Re: getColumnName() returns aliases not the original column name
    user11179792 Newbie
    Currently Being Moderated
    Thanks for the reply. I am aware of that. But in our use case, the alias is only associated with a table column. Based on the java doc, getColumnName returns the designated column's name and getColumnLabel returns the designated column's suggested title for use in printouts and displays. I am trying to understand the difference.
  • 5. Re: getColumnName() returns aliases not the original column name
    user11179792 Newbie
    Currently Being Moderated
    Thanks for the reply. I am aware of that. But in our use case, the alias is only associated with a table column (e.g. SELECT column AS aliasName from table). Based on the java doc, getColumnName returns the designated column's name and getColumnLabel returns the designated column's suggested title for use in printouts and displays. I am trying to understand the difference between the two as they both return the alias.
  • 6. Re: getColumnName() returns aliases not the original column name
    rp0428 Guru
    Currently Being Moderated
    >
    Thanks for the reply. I am aware of that. But in our use case, the alias is only associated with a table column (e.g. SELECT column AS aliasName from table). Based on the java doc, getColumnName returns the designated column's name and getColumnLabel returns the designated column's suggested title for use in printouts and displays. I am trying to understand the difference between the two as they both return the alias.
    >
    The difference between the two will be database driver specific. For Oracle there is no difference. For IBM there can and will be differences. This doc shows what a mess it is if you are using IBM since you can have both an AS clause and a Label.
    http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp?topic=%2Fcom.ibm.db2.luw.wn.doc%2Fdoc%2Fi0052606.html

    There is nothing in the JDBC spec (even 4.0) that specifies what getColumnName should return but the Java API docs (e.g. 1.6) do 'suggest' what getColumnLabel will return.
    >
    Gets the designated column's suggested title for use in printouts and displays. The suggested title is usually specified by the SQL AS clause. If a SQL AS is not specified, the value returned from getColumnLabel will be the same as the value returned by the getColumnName method.
    >
    But, as you found, for Oracle this returns the AS text but so does the getColumnName. So for Oracle the two will always return the same value.

    There are no workarounds or planned changes to this as far as I know.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points