Wrong column names in ResultSetMetaData

From: "Mike Martin" <mmartin(at)vieo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Wrong column names in ResultSetMetaData
Date: 2004-07-28 23:43:44
Message-ID: ce9dna$pne$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

With the new V3 driver the column names in ResultSetMetaData
don't reflect the aliases used in the SQL. I.e. if I do:

SELECT name as name_alias FROM ...

the metadata says I have a result column called "name" instead
of "name_alias".

Client: PostgreSQL 7.5devel JDBC2 with SSL (build 304)
Server: PostgreSQL 7.4.2 on i386-redhat-linux-gnu, compiled by GCC 2.96

The code below works with the 7.4 driver but fails with 7.5.
Is this more likely to be in the driver or in the server-side
V3 code?

Mike

AliasTest.java
-------------------

import java.sql.*;
import java.util.*;

public class AliasTest
{
public static void main(String args[]) throws Exception
{
if (args.length != 3)
throw new IllegalArgumentException("usage: java AliasTest
<database_spec> <username> <password>");

Class.forName("org.postgresql.Driver");

Connection conn = DriverManager.getConnection("jdbc:postgresql:" +
args[0], args[1], args[2]);
Statement stmt = conn.createStatement();

try { stmt.execute("DROP TABLE alias_test"); } catch (SQLException
e) {}
stmt.execute("CREATE TABLE alias_test ( name varchar(64) not null
primary key )");
stmt.executeUpdate("INSERT INTO alias_test ( name ) VALUES (
'abc' )");
stmt.close();

PreparedStatement ps = conn.prepareStatement("SELECT name as
name_alias FROM alias_test");
ResultSet rs = ps.executeQuery();

if (!rs.next()) throw new Exception("No row?");

List expectedColumnNames = Arrays.asList(new String[] {
"name_alias" });
List actualColumnNames = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();

int colCount = rsmd.getColumnCount();

for (int colNum = 1; colNum <= colCount; ++colNum)
actualColumnNames.add(rsmd.getColumnName(colNum));

if (!expectedColumnNames.equals(actualColumnNames))
throw new Exception("Wrong column names in result set metadata,
expected " +
expectedColumnNames + ", got " + actualColumnNames);

rs.close();
ps.close();
conn.close();
}
}

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Mike Martin 2004-07-29 01:24:00 Re: Wrong column names in ResultSetMetaData
Previous Message dgr 2004-07-28 10:32:34 Re: SSL Connection Problems