Create a wrapper for java.sql.Connection that works with JDBC 3 and 4

隐身守侯 提交于 2019-12-22 10:30:14

问题


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:


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)



来源:https://stackoverflow.com/questions/1069544/create-a-wrapper-for-java-sql-connection-that-works-with-jdbc-3-and-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!