views:

560

answers:

1

Is there some hack to create a wrapper for java.sql.Connection which works with JDBC 3 and 4 (Sun added a couple of methods and new types to the interface for JDBC 4) without resorting to something which patches the source at compile time?

My use case is that I need to generate a library which works with Java 5 and 6 and I'd really like to avoid creating two versions of it.

+1  A: 

Haven't tried it but it might work.

Create your class to implement the Java 6 version. It will have problem with the new classes in java.sql (NClob, SQLXML, SQLClientInfoException). Assuming you do not use these classes (as you are working with Java 5 as well), create a dummy implementation of them and put it in a separate jar. In the Java 5 deployment, refer to this jar with the -Xbootclasspath command line variable so they will be loaded correctly.

Notice that your wrapper will need to know whether it is running on Java 5 or 6 (in order to delegate properly), so you will probably want to have all the Java 6 features handled in a separate class, instantiated in run time (see the state design pattern)

David Rabinowitz