When I use postgresql, I found the following code:
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from t"); String tableName = rs.getMetaData().getTableName(1); System.out.println(tableName);
It prints an empty line.
So, I checked the source code and found that the org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData#getTableName
always returns an empty string.
Source:
public abstract class AbstractJdbc2ResultSetMetaData implements PGResultSetMetaData { public String getTableName(int column) throws SQLException { return ""; } }
You can see that it simply returns a ""
.
I found a discussion about this, see: http://archives.postgresql.org/pgsql-jdbc/2009-12/msg00100.php
They think "rs.getMetaData.getTableName (col)" should return an alias in the query not the name of the base table. But it’s hard to implement, so it’s better to leave it blank.
They also gave a method to get the table name, use:
PGResultSetMetaData.getBaseTableName()
Example:
ResultSet rs = stmt.executeQuery("select * from x");
Now it can print the correct table name.
I do not know that the postgresql implementation is correct, but returning the name of the base table is much more useful than an empty string, and most other databases provide the base name of the table instead of the empty string.
I have a problem using the play2 anorm framework with postgesql: Anonymous Anonymous 2 does not work on postgresql , but it works well in other databases.
What do you think is the correct implementation of the postgresql jdbc driver? Return empty string, base table name, or something else?