简体   繁体   English

如何确定JDBC驱动程序是否为Type 4?

[英]How can I determine whether a JDBC driver is Type 4?

We have an application that runs on Java 1.6 and has the ability to talk to RDBMS systems via custom plugins. 我们有一个在Java 1.6上运行的应用程序,它能够通过自定义插件与RDBMS系统通信。 We don't care what type of database a customer has, but we did decide to require a Type 4 driver, for a variety of reasons. 我们不关心客户的数据库类型,但出于各种原因,我们确实决定要求使用Type 4驱动程序。

I want to programmatically decide whether a driver is Type 4 so that I can reject it right away rather than spend time debugging problems later only to find out that the driver is not Type 4. Since I don't know what the vendor is, I can't just look at the version number. 我想以编程方式确定驱动程序是否为Type 4,以便我可以立即拒绝它,而不是花时间调试问题以后才发现驱动程序不是Type 4.因为我不知道供应商是什么,我不能只看版本号。 I've looked at Driver.jdbcCompliant() but this is not a necessary condition for the driver being Type 4. I've also looked at creating a Connection and then doing reflection on it to see if it implements certain methods like isValid(), but again I realized this does not necessarily guarantee the driver is Type 4. Any suggestions? 我看过Driver.jdbcCompliant(),但这不是驱动程序是Type 4的必要条件。我还看了创建一个Connection然后对它进行反射,看看它是否实现了某些方法,如isValid() ,但我再次意识到这并不一定能保证驱动程序是Type 4.有什么建议吗?

Edit: Seems like I was talking about something different anyway! 编辑:似乎我正在谈论不同的东西!

Get the DatabaseMetaData via Connection.getMetaData() and check that getJDBCMajorVersion() >= 4. You may want to catch NoSuchMethodException when calling that method, since only a JDBC 3.0+ driver will have it. 通过Connection.getMetaData()获取DatabaseMetaData并检查getJDBCMajorVersion() > = 4.您可能希望在调用该方法时捕获NoSuchMethodException ,因为只有JDBC 3.0+驱动程序才会拥有它。

Edit: Hmm, this seems like it should be right but... Oracle doesn't seem to implement it correctly, since I get 11.2 for the JDBC version. 编辑:嗯,这似乎应该是正确的但是...... Oracle似乎没有正确实现它,因为我获得了11.2的JDBC版本。 That's the same thing I get for the driver version. 这与驱动程序版本相同。 Thanks Oracle! 谢谢甲骨文!

If you can't count on the methods specifically designed for this, it seems to me that your only option is to call methods like createClob() on Connection and catch NoSuchMethodException . 如果您不能指望专门为此设计的方法,在我看来,您唯一的选择是在Connection上调用createClob()等方法并捕获NoSuchMethodException That or add special handling for specific vendors. 那或为特定供应商添加特殊处理。

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

相关问题 的DriverManager.getConnection(); 如何确定用户名/密码是否错误或驱动程序无法连接到数据库? - DriverManager.getConnection(); How can I determine whether the username/password is wrong or the Driver is not able to connect to the database? 如何确定要用于JDBC的驱动程序 - How to determine which driver to use for JDBC 如何确定我是否在 Spring 的事务中? - How can I determine whether I am in a transaction in Spring? 如何确定setMaxRows限制了返回的行数(JDBC) - How can I determine that setMaxRows limited the number of rows returned (JDBC) 如何确定文件是否在USB大容量存储设备上? - How can I determine whether a file is on a USB mass storage device? 如何通过反射确定Java类是否是抽象的 - How can I determine whether a Java class is abstract by reflection 如何确定变量是否具有作用域和生存期? - How can I determine whether a variable has a scope and lifetime or not? 如何判断安装的 Java 是否支持模块? - How can I determine whether the installed Java supports modules or not? 如何确定给定JDBC连接的数据库类型? - How to determine database type for a given JDBC connection? 我无法加载 MySQL 的 JDBC 驱动程序 - I can't load the JDBC driver for MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM