简体   繁体   English

如何在Java 5和6中使JDBC驱动程序工作?

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

Java 6 comes with JDBC 4, which is not backward compatible with JDBC shipped with previous versions of Java. Java 6附带JDBC 4,它与以前版本的Java附带的JDBC不向后兼容。

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. 我们有一个JDBC驱动程序,它必须同时支持Java 5和Java 6.如果我在驱动程序中实现新接口,它在Java 5中不起作用,因为接口也使用新类。 So we have 2 versions of the driver. 所以我们有2个版本的驱动程序。 Is there a way to have one jar for both Java 5 and 6? 有没有办法让Java 5和6都有一个jar?

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. 如果您实现JDBC 3.0驱动程序,并且您的客户端在Java 6中运行它,那么如果它们调用任何新的JDBC 4.0方法,它们将会出错。 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) 使用java 5编译(如果使用java 6编译并在java 5中运行,则会出现版本不匹配错误)
  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) 实现您的普通JDBC 3.0驱动程序,但也包括新JDBC 4.0方法的实现(注意:这将导致编译错误,因为java 5没有定义像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) 在一个单独的项目中创建新的JDBC 4.0接口(如java.sql.NClob)的精确副本(所以现在有2个项目:原始项目和这个项目)
  4. compile both projects together, and make a separate jar per project: 将两个项目一起编译,并为每个项目创建一个单独的jar:
    • your JDBC driver implementation (keep this jar) 你的JDBC驱动程序实现(保持这个jar)
    • the new JDBC 4.0 interfaces (discard this jar) 新的JDBC 4.0接口(丢弃此jar)

We can discard the JDBC 4.0 interfaces jar because: 我们可以丢弃JDBC 4.0接口jar,因为:

  • in Java 6, those new JDBC 4.0 interfaces are defined 在Java 6中,定义了那些新的JDBC 4.0接口
  • in Java 5, the JDBC 3.0 interfaces don't define the new methods (like Connection.createNClob()), so your users won't call them 在Java 5中,JDBC 3.0接口没有定义新方法(如Connection.createNClob()),因此您的用户不会调用它们

The only potential problem could be with the java 5 classloader. 唯一可能的问题可能是java 5类加载器。 It may fail if it can't find the new JDBC 4.0 interfaces (like java.sql.NClob). 如果找不到新的JDBC 4.0接口(如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. 我不认为这会在Sun JVM中发生,除非你调用一个返回其中一个新接口的方法,所以你应该很好。 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. 如果我错了,这是一个问题,那么在Java 5中运行时,只需将丢弃的jar(定义新的JDBC 4.0接口的jar)放在类路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM