rs.getMetaData().getColumnName(i) 与 mysql 上的别名列
当我有类似以下内容的查询结果时:
select col as newName from table;
然后我执行(在java中):
rs.getMetaData().getColumnName(i)
它返回列的名称而不是“newName”...
如果我这样做,
select concat(col,'') as newName from table;
它会返回预期的“newName”
有没有办法获取“newName”而不扰乱 sql 查询?
这都是mysql、java、tomcat 6。
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.
尝试使用
getColumnLabel()
代替:仅供参考,我做了一个测试,
rs.getMetaData().getColumnName(i)
为我工作 - 即它给了我 别名,不是列名,但也许您正在使用旧版本的 JDBC 驱动程序和/或 mysql 数据库。Try using
getColumnLabel()
instead: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.