GetSchema in PostgreSQL JDBC driver throws java.lang.AbstractMethodError or java.sql.SQLFeatureNotSupportedException

I am using Postgresql 8.4 and my application is trying to connect to the database. I have registered the driver:

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

      

and then try the connection:

db = DriverManager.getConnection(database_url);

      

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

I tried different version of the jdbc driver and got two types 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.

      

this means: the org.postgresql.jdbc4.Jdbc4Connection.getSchema () method has not been implemented yet.

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

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

The problem is not with the string or driver version, the problem was in the code directly above the method getConnection()

:

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

      

The postgresql driver seems to have no method getSchema

as the java console has often tried to tell me.

+3


source to share


1 answer


Java 7 / JDBC 4.1 added Connection.getSchema()

. This means it is not necessarily available in JDBC Driver 3 or 4 (although if an implementation exists, it will be called).

If you are using the JDBC 3 (Java 4/5) or JDBC 4 (Java 6) driver in Java 7 or higher, it is quite possible what you get java.lang.AbstractMethodError

when you call getSchema

if it doesn't exist in the implementation. Java provides a form of forward compatibility for classes that implement an interface.

If new methods are added to the interface, classes that do not have these methods and that, for example, were compiled with an older version of the interface, can still be loaded and used, provided the new methods are not called. Missing methods will be clogged with code that just throws AbstractMethodError

. On the other hand: if the method was implementedgetSchema

and the signature was compatible, this method will now be available through the interface, although this method did not exist in the interface at compile time.



In March 2011 the driver was updated so it could be compiled in Java 7 (JDBC 4.1), this happened by launching the new JDBC 4.1 with an implementation that generates java.sql.SQLFeatureNotSupportedException

, including the implementationConnection.getSchema

. This code is still in the current PostgreSQL 9.3-1102 JDBC driver. Technically for a JDBC compliant driver, it is not allowed to throw SQLFeatureNotSupportedException

unless the API documentation or JDBC specification is explicitly allowed (which is not for getSchema

).

However, the current code on github provides implementation as of April this year . You might want to consider compiling your own version, or ask the pgsql-jdbc mailing list if the latest snapshots are available (the snapshot link at http://jdbc.postgresql.org/ shows fairly old versions).

+4


source







All Articles