开发者

rs.getMetaData().getColumnName(i) with aliased columns on mysql

开发者 https://www.devze.com 2023-03-21 05:02 出处:网络
when I have a query result for something like: select col as newName from table; 开发者_如何学运维and I then do (in java) :

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消