如何使JDBC驱动程序在Java 5& 6? [英] How to Make JDBC Driver Work in Java 5 & 6?

查看:87
本文介绍了如何使JDBC驱动程序在Java 5& 6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 6附带JDBC 4,它与以前版本的Java一起提供的JDBC不向后兼容。

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

我们有一个必须支持Java 5的JDBC驱动程序如果我在驱动程序中实现新接口,它在Java 5中不起作用,因为接口也使用新类。所以我们有两个版本的驱动程序。有没有办法让Java 5和6都有一个jar?

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?

推荐答案

如果你实现了一个JDBC 3.0驱动程序,那么你的客户端在Java 6中运行它们,如果它们调用任何新的JDBC 4.0方法,它们将会出错。这种情况很少见,因为这些新方法并不经常使用,但是如果你想要万无一失,那么下面的解决方案(这是一个黑客行为)应该可以解决这个问题:

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. 使用java 5编译(如果使用java 6编译并在java 5中运行,则会出现版本不匹配错误)

  2. 实现您的普通的JDBC 3.0驱动程序,但也包括新的JDBC 4.0方法的实现(注意:这将导致编译错误,因为java 5没有定义像java.sql.NClob这样的类型)

  3. 创建在一个单独的项目中新的JDBC 4.0接口(如java.sql.NClob)的精确副本(所以现在有2个项目:原始项目和这个项目)

  4. 将两个项目一起编译,并为每个项目创建一个单独的jar:


    • 您的JDBC驱动程序实现(保留此jar)

    • 新JDBC 4.0接口(丢弃这个jar)

  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)

我们可以丢弃JDBC 4.0接口jar因为:

We can discard the JDBC 4.0 interfaces jar because:


  • ,在Java 5中定义了那些新的JDBC 4.0接口

  • ,JDBC 3.0接口没有定义新的方法(比如Connection.createNClob()),所以你的用户不会打电话给他们

唯一的潜在问题可能是java 5类加载器。如果找不到新的JDBC 4.0接口(如java.sql.NClob),它可能会失败。我不认为这会在Sun JVM中发生,除非你调用一个返回其中一个新接口的方法,所以你应该很好。如果我错了,这是一个问题,那么在Java 5中运行时,只需将丢弃的jar(定义新JDBC 4.0接口的jar)放在类路径上。

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.

这篇关于如何使JDBC驱动程序在Java 5& 6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆