rs.getMetaData().getColumnName(i) with aliased columns on mysql
when I have a query result for something like:
select col as newName from table;
开发者_如何学运维and I then do (in java) :
rs.getMetaData().getColumnName(i)
it returns the name of the column instead of "newName"...
if however I do
select concat(col,'') as newName from table;
it returns the expected "newName"
is there a way to get the "newName" without messing with tho sql query?
This is all mysql, java, tomcat 6.
Try using getColumnLabel()
instead:
rs.getMetaData().getColumnLabel(i);
FYI, I did a test and rs.getMetaData().getColumnName(i)
worked for me - ie it gave me the alias, not the column name, but perhaps you are using an older version of the JDBC driver and/or mysql database.
精彩评论