[英]How to get a column by alias/label from Java's ResultSet?
Suppose I have a SP which does SELECT a as b FROM example_table
at the end. 假设我有一个SP,
SELECT a as b FROM example_table
在最后SELECT a as b FROM example_table
中SELECT a as b FROM example_table
。
When I do 当我做
// Some magical code goes here
ResultSet rs = callableStatement.executeQuery();
while(rs.next()) {
System.out.printLn(rs.getString("b"));
}
// Some magical code goes here
I get an error telling me that the column does not exist, however when I change my code for rs.getString("a")
, It will execute correctly. 我收到一条错误消息,通知我该列不存在,但是当我更改
rs.getString("a")
代码时,它将正确执行。 This seems to be because the getString(String)
fetches for the column by its name, not by its alias/label. 这似乎是因为
getString(String)
通过其名称而不是通过其别名/标签来获取列。
How can I tell the ResultSet to fetch the column by label/alias, not by its name. 如何告诉ResultSet通过标签/别名(而不是名称)来获取列。
@Alper was on the money. @Alper赚了钱。
They changed the driver and added a connection property GET_COLUMN_LABEL_FOR_NAME that you need to set to "true". 他们更改了驱动程序,并添加了需要设置为“ true”的连接属性GET_COLUMN_LABEL_FOR_NAME。 I thought it was only for resultSetMetaData, but apparently not so much...
我以为它只用于resultSetMetaData,但显然不多。
It's bizarre, but try it. 这很奇怪,但请尝试一下。
I don't know if it is related but in MySQL there is a similar problem which you can avert by adding ?useOldAliasMetadataBehavior=true
to the end of the connection string. 我不知道它是否相关,但是在MySQL中,您可以通过在连接字符串的末尾添加
?useOldAliasMetadataBehavior=true
来避免类似的问题。 Check this link for details 检查此链接以获取详细信息
The link of an official explanation for the same case : Here it says, until the version 5.0.x the default value was set to true
but after the version 5.1 the default value was set false
. 相同情况的官方解释链接 :此处表示直到5.0.x版本,默认值都设置为
true
而在5.1版本之后,默认值设置为false
。 You can also check the other parameters for the configuration string here 您还可以在此处检查配置字符串的其他参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.