*** pgjdbc.xml.old 2009-06-20 17:19:40.000000000 +0200 --- pgjdbc.xml 2010-07-20 22:15:26.543010752 +0200 *************** *** 3187,3192 **** --- 3187,3255 ---- + + PGResultSetMetaData interface + + If a column is aliased in the query, the standard ResultSetMetaData interface can not determine the schema and table name for this column. + So for consistency reasons, getColumnName and getSchemaName always returns a zero-length string (""). + + + To determine the names of the underlying columns, table and schema of the ResultSet, you have to use the PGResultSetMetaData interface. + This interface gives the names of the columns, table and schema of the underlying base column, table or schema name if it can determine it. Note that is not possible when the column is an operation (see example below). + + + Using ResultSetMetaData and PGResultSetMetaData + + Statement stmt = null; + ResultSet resultSet = null; + + // Query + String sql = "SELECT city as town, temp_lo + 273 , temp_hi FROM weather"; + try { + stmt = connexion.createStatement(); + resultSet = stmt.executeQuery(sql); + + // Metadata + ResultSetMetaData rsm; + PGResultSetMetaData pgRsm; + + rsm = resultSet.getMetaData(); + pgRsm = (PGResultSetMetaData)rsm; + + System.out.println("1rst column : "); + System.out.println("column name : " + rsm.getColumnName(1) + "; base column : " + pgRsm.getBaseColumnName(1)); + System.out.println("table name : " + rsm.getTableName(1) + "; base table : " + pgRsm.getBaseTableName(1)); + System.out.println("schema name : " + rsm.getSchemaName(1) + "; base schema : " + pgRsm.getBaseSchemaName(1)); + System.out.println("2nd column : "); + System.out.println("column name : " + rsm.getColumnName(2) + "; base column : " + pgRsm.getBaseColumnName(2)); + System.out.println("table name : " + rsm.getTableName(2) + "; base table : " + pgRsm.getBaseTableName(2)); + System.out.println("schema name : " + rsm.getSchemaName(2) + "; base schema: " + pgRsm.getBaseSchemaName(2)); + System.out.println("3rd column : "); + System.out.println("column name : " + rsm.getColumnName(3) + "; base column : " + pgRsm.getBaseColumnName(3)); + System.out.println("table name : " + rsm.getTableName(3) + "; base table : " + pgRsm.getBaseTableName(3)); + System.out.println("schema name : " + rsm.getSchemaName(3) + "; base schema: " + pgRsm.getBaseSchemaName(3)); + + + + Which will produce the following output : + + + 1rst column : + column name : town; base column : city + table name : ; base table : weather + schema name : ; base schema : public + 2nd column : + column name : ?column?; base column : + table name : ; base table : + schema name : ; base schema: + 3rd column : + column name : temp_hi; base column : temp_hi + table name : ; base table : weather + schema name : ; base schema: public + + + + Further Reading