PostgreSQL- getColumnName 不工作,返回别名
PostgreSQL- getColumnName is not working, returning alias Name
我正在尝试从下面的查询中获取列名称,
SELECT
category as c1,
forecast_2016,
category,
rowcount,
item_number,
rowcount,
category,
avg_demand_2014_2015,
category,
avg_spend_2014_2015,
avg_demand_2014_2015,
avg_spend_2014_2015,
demand_2015
FROM
ag_instrument_portfolio_master LIMIT 1
Postgres 版本为 9.3,Java 版本为 1.7,java 实现如下。
stmt = con.createStatement();
rs = stmt.executeQuery(query.toString());
ResultSetMetaData columnsMetadata = rs.getMetaData();
int i = 0;
while (i < columnsMetadata.getColumnCount()) {
i++;
System.out.println("Name: " + columnsMetadata.getColumnName(i));
System.out.println("Label: " + columnsMetadata.getColumnLabel(i));
}
输出为
Name: c1
Label: c1
但是,预期是
Name: category
Label: c1
根据 pgsql-jdbc
邮件列表 here 中的评论,您看到的似乎是 PostgreSQL JDBC 驱动程序的 "as designed" 行为:
This is a limitation of the information the driver gets back from the
server, it only returns the 'label' which the driver then uses for both
columnname and label.
与 JDBC 的许多其他方面一样,给定功能的行为通常会因特定 JDBC 驱动程序的实施而异。
我正在尝试从下面的查询中获取列名称,
SELECT
category as c1,
forecast_2016,
category,
rowcount,
item_number,
rowcount,
category,
avg_demand_2014_2015,
category,
avg_spend_2014_2015,
avg_demand_2014_2015,
avg_spend_2014_2015,
demand_2015
FROM
ag_instrument_portfolio_master LIMIT 1
Postgres 版本为 9.3,Java 版本为 1.7,java 实现如下。
stmt = con.createStatement();
rs = stmt.executeQuery(query.toString());
ResultSetMetaData columnsMetadata = rs.getMetaData();
int i = 0;
while (i < columnsMetadata.getColumnCount()) {
i++;
System.out.println("Name: " + columnsMetadata.getColumnName(i));
System.out.println("Label: " + columnsMetadata.getColumnLabel(i));
}
输出为
Name: c1
Label: c1
但是,预期是
Name: category
Label: c1
根据 pgsql-jdbc
邮件列表 here 中的评论,您看到的似乎是 PostgreSQL JDBC 驱动程序的 "as designed" 行为:
This is a limitation of the information the driver gets back from the server, it only returns the 'label' which the driver then uses for both columnname and label.
与 JDBC 的许多其他方面一样,给定功能的行为通常会因特定 JDBC 驱动程序的实施而异。