db2 的 ExceptionInInitializerError

发布于 2024-11-09 03:54:29 字数 1163 浏览 0 评论 0原文

我有最简单的 JDBC 程序来从 DB2(或其他配置的)实例读取数据。当我尝试从 Eclipse 或从命令行运行它时,连接失败,抛出 SQL 异常

 Class.forName(jdbcDriverClassName).newInstance();

class.forName 失败,使用 com.ibm.db2.jcc.DB2Driver。我将 Eclipse 配置为指向 DBVisualizer 使用的同一驱动程序(既作为启动库又用于项目)。该程序使用与 DBVisualizer 相同的连接字符串。 DBVisualizer 没有任何问题。

带完整堆栈的输出(请注意驱动程序的 FQN 不为空):

                   V-- !null driver name --V
Attempting to load com.ibm.db2.jcc.DB2Driver
ERROR:java.lang.ExceptionInInitializerError: null
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.ibm.db2.jcc.DB2Driver.class$(DB2Driver.java:58)
at com.ibm.db2.jcc.DB2Driver.<clinit>(DB2Driver.java:61)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.onlineretailer.ecomm.dumporder.DumpOrder.main(DumpOrder.java:79)
Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.ib.i(ib.java:490)
at com.ibm.db2.jcc.am.ib.<clinit>(ib.java:420)
... 7 more

我还尝试将 .newInstance() 添加到 class.forName() 的末尾,并且它继续执行相同的行为。

感谢大家的时间和投入!

I have the simplest JDBC program to read data from a DB2 (or other configured) instance. When I attempt to run it from Eclipse OR from the command line, the connect fails, throwing a SQLexception

 Class.forName(jdbcDriverClassName).newInstance();

The class.forName fails, using com.ibm.db2.jcc.DB2Driver. I configured Eclipse to point to the same driver that DBVisualizer uses (both as a boot library and for the project). The program uses the same connect string that DBVisualizer uses. DBVisualizer isn't having any trouble.

Ouptut w/full stack (note the FQN for the driver is not null):

                   V-- !null driver name --V
Attempting to load com.ibm.db2.jcc.DB2Driver
ERROR:java.lang.ExceptionInInitializerError: null
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.ibm.db2.jcc.DB2Driver.class$(DB2Driver.java:58)
at com.ibm.db2.jcc.DB2Driver.<clinit>(DB2Driver.java:61)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.onlineretailer.ecomm.dumporder.DumpOrder.main(DumpOrder.java:79)
Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.ib.i(ib.java:490)
at com.ibm.db2.jcc.am.ib.<clinit>(ib.java:420)
... 7 more

I also tried adding the .newInstance() to the end of the class.forName() and it continues to execute the same behavior.

Thanks to everyone for your time and input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望喜 2024-11-16 03:54:29
SQLException:没有找到适合 jdbc:db2:... 的驱动程序

此异常只有 2 个可能的原因:

  1. JDBC 驱动程序未知,DriverManager。即 JDBC 驱动程序未通过 DriverManager#registerDriver()

  2. 对于使用 DriverManager 注册的任何 JDBC 驱动程序,JDBC URL 都是未知的。即 Driver#acceptsURL() 尚未为任何已注册的驱动程序返回 true

您的 JDBC 驱动程序类名完全没问题。您的 JDBC URL 完全没问题。

精心设计的 JDBC 驱动程序会在 static {} 初始化块中向 DriverManager 注册自身,该初始化块在 Class#forName()< 上执行/代码>。但是,旧版本的 IBM DB2 JDBC 驱动程序将其自身注册到构造函数中。对于那些损坏的驱动程序,您需要调用 newInstance() 之后也是如此。

Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();

另请参阅:


更新根据您的问题更新,newInstance()做到了这个技巧。然而,它导致了一个问题:

Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.ib.i(ib.java:490)
at com.ibm.db2.jcc.am.ib.<clinit>(ib.java:420)

事实证明这是 DB2 JDBC 驱动程序本身的另一个错误。升级它。

SQLException: No suitable driver found for jdbc:db2:...

This exception has only 2 possible causes:

  1. The JDBC driver is unknown with DriverManager. I.e. the JDBC driver has not registered itself properly by DriverManager#registerDriver().

  2. The JDBC URL is unknown for any of the JDBC drivers registered with DriverManager. I.e. the Driver#acceptsURL() hasn't returned true for any of the registered drivers.

Your JDBC driver class name is perfectly fine. Your JDBC URL is perfectly fine.

A well designed JDBC driver registers itself with DriverManager in a static {} initializer block which get executed upon Class#forName(). However, older versions of the IBM DB2 JDBC driver registers itself in the constructor instead. For those broken drivers you need to call newInstance() afterwards as well.

Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();

See also:


Update as per your question update, the newInstance() did the trick. It however leads to a new problem:

Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.ib.i(ib.java:490)
at com.ibm.db2.jcc.am.ib.<clinit>(ib.java:420)

It turns out to be another bug in the DB2 JDBC Driver itself. Upgrade it.

七禾 2024-11-16 03:54:29

您还需要使用 Class.forName() 在 jvm 中加载驱动程序
您需要加载以下驱动程序

com.ibm.db2.jcc.DB2Driver 

这是它的完整外观

String databaseURL = "jdbc:derby:net://localhost:1527/sample";
// Load DB2 Driver for JDBC class
Class.forName("com.ibm.db2.jcc.DB2Driver");
// Set user and password properties
Properties properties = new Properties();
properties.put("user", "APP");
properties.put("password", "APP");
properties.put("retreiveMessagesFromServerOnGetMessage", "true");
// Get a connection
Connection conn = DriverManager.getConnection(databaseURL, properties); 

参考

You need to load driver too in your jvm , using Class.forName()
You need to load following driver

com.ibm.db2.jcc.DB2Driver 

Here is how it fully look like

String databaseURL = "jdbc:derby:net://localhost:1527/sample";
// Load DB2 Driver for JDBC class
Class.forName("com.ibm.db2.jcc.DB2Driver");
// Set user and password properties
Properties properties = new Properties();
properties.put("user", "APP");
properties.put("password", "APP");
properties.put("retreiveMessagesFromServerOnGetMessage", "true");
// Get a connection
Connection conn = DriverManager.getConnection(databaseURL, properties); 

Reference

狼性发作 2024-11-16 03:54:29

“没有合适的驱动程序”通常意味着连接 URL 对于您正在使用的 JAR 不正确。

"No suitable driver" usually means that the connection URL isn't correct for the JAR you're using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文