Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make JDBC Driver Work in Java 5 & 6?

Tags:

java

jdbc

Java 6 comes with JDBC 4, which is not backward compatible with JDBC shipped with previous versions of Java.

We have a JDBC driver which must support both Java 5 and Java 6. If I implement the new interfaces in the driver, it doesn't work in Java 5 because the interfaces also uses new classes. So we have 2 versions of the driver. Is there a way to have one jar for both Java 5 and 6?

like image 476
ZZ Coder Avatar asked Feb 28 '23 11:02

ZZ Coder


1 Answers

If you implement a JDBC 3.0 driver, and your clients run it in Java 6, they'll get errors if they call any of the new JDBC 4.0 methods. This is rare, as those new methods aren't used very often, but if you want to be fool-proof, the below solution (which is a hack) should do the trick:

  1. compile with java 5 (if you compile with java 6 and run in java 5, you'll get a version mismatch error)
  2. Implement your normal JDBC 3.0 driver, but also include implementations for the new JDBC 4.0 methods (note: this will cause compile errors because java 5 doesn't define types like java.sql.NClob)
  3. create an exact replica of the new JDBC 4.0 interfaces (like java.sql.NClob) in a separate project (so now there's 2 projects: the original one and this one)
  4. compile both projects together, and make a separate jar per project:
    • your JDBC driver implementation (keep this jar)
    • the new JDBC 4.0 interfaces (discard this jar)

We can discard the JDBC 4.0 interfaces jar because:

  • in Java 6, those new JDBC 4.0 interfaces are defined
  • in Java 5, the JDBC 3.0 interfaces don't define the new methods (like Connection.createNClob()), so your users won't call them

The only potential problem could be with the java 5 classloader. It may fail if it can't find the new JDBC 4.0 interfaces (like java.sql.NClob). I don't think this will happen in the Sun JVM unless you call a method which returns one of those new interfaces, so you should be good. If I'm wrong and this is an issue, just put the discarded jar (the one that defined the new JDBC 4.0 interfaces) on your classpath when running in Java 5.

like image 119
Brad Cupit Avatar answered Mar 12 '23 18:03

Brad Cupit