5
votes

I'm using Postgresql 8.4 and my application is trying to connect to the database. I've registered the driver:

DriverManager.registerDriver(new org.postgresql.Driver());

and then trying the connection:

db = DriverManager.getConnection(database_url);

(btw, my jdbc string is something like: jdbc:postgresql://localhost:5432/myschema?user=myuser&password=mypassword)

I've tried various version of the jdbc driver and getting two type of errors:

with jdbc3:

Exception in thread "main" java.lang.AbstractMethodError: org.postgresql.jdbc3.Jdbc3Connection.getSchema()Ljava/lang/String;

with jdbc4:

java.sql.SQLFeatureNotSupportedException: Il metodo ½org.postgresql.jdbc4.Jdbc4Connection.getSchema()╗ non Þ stato ancora implementato.

that means: method org.postgresql.jdbc4.Jdbc4Connection.getSchema() not implemented yet.

I'm missing something but I don't know what..

------ SOLVED ---------

The problem were not in the connection String or the Driver version, the problem were in the code directly above the getConnection() method:

db = DriverManager.getConnection(database_url);
LOGGER.info("Connected to : " + db.getCatalog() + " - " + db.getSchema());

It seems postgresql driver doesn't have getSchema method, as the java console were often trying to say to me..

1
What is the version of the driver you're using? The filename of the jar contains the version number - fvu
I've used: 8.4-702.jdbc3, 8.4-702.jdbc4, 9.2-1002.jdbc3, 9.2-1002.jdbc4, 9.2-1003.jdbc3, 9.2-1003.jdbc4, 9.3-1102.jdbc3, 9.3-1102.jdbc4 and maybe a couple of other versions - Stefano Vercellino
Please show the full code of your test. Also, what's your JVM version? - Craig Ringer
Short version after discussion on comments - same error with Class.forName. Using JDK 1.7. So I think your classpath has multiple incompatible PgJDBC versions (possibly nested in other jars?) or something else is horribly broken. I'd need to see a standalone test case to help you further. - Craig Ringer
Now I'm using 9.3-1102-jdbc41, but I think that now that I've removed the problematic method ( db.getSchema() ) it could work also with other versions. java version 7. - Stefano Vercellino

1 Answers

5
votes

The Connection.getSchema() version was added in Java 7 / JDBC 4.1. This means that it is not necessarily available in a JDBC 3 or 4 driver (although if an implementation exists, it will get called).

If you use a JDBC 3 (Java 4/5) driver or a JDBC 4 (Java 6) driver in Java 7 or higher it is entirely possible that you receive a java.lang.AbstractMethodError when calling getSchema if it does not exist in the implementation. Java provides a form of forward compatibility for classes implementing an interface.

If new methods are added to an interface, classes that do not have these methods and were - for example - compiled against an older version of the interface, can still be loaded and used provided the new methods are not called. Missing methods will be stubbed by code that simply throws an AbstractMethodError. On the other hand: if a method getSchema had been implemented and the signature was compatible that method would now be accessible through the interface, even though the method did not exist in the interface at compile time.

In March 2011, the driver was updated so it could be compiled on Java 7 (JDBC 4.1), this happened by stubbing the new JDBC 4.1 methods with an implementation that throws a java.sql.SQLFeatureNotSupportedException, including the implementation of Connection.getSchema. This code is still in the current PostgreSQL JDBC driver version 9.3-1102. Technically a JDBC-compliant driver is not allowed to throw SQLFeatureNotSupportedException unless the API documentation or JDBC specification explicitly allows it (which it doesn't for getSchema).

However the current code on github does provide an implementation since April this year. You might want to consider compiling your own version, or ask on the pgsql-jdbc mailinglist if there are recent snapshots available (the snapshots link on http://jdbc.postgresql.org/ shows rather old versions).