Home:ALL Converter>PostgreSQL- getColumnName is not working, returning alias Name

PostgreSQL- getColumnName is not working, returning alias Name

Ask Time:2016-07-16T04:25:02         Author:Haneep CR

Json Formatter

I'm trying to get the column name from below query,

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 version is 9.3 and Java version 1.7, java implementation is below.

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));
}

The output is

Name: c1
Label: c1

But, expected is

Name: category
Label: c1

Author:Haneep CR,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38404534/postgresql-getcolumnname-is-not-working-returning-alias-name
yy