调用DriverManager.getConnection()时出现意外的java.lang.NoClassDefFoundError

时间:2013-07-05 19:53:27

标签: mysql jdbc

我正在编写一个Java应用程序,需要通过JDBC将一些数据插入MySQL数据库。这是相关的代码:

public JDBCDecoder() {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Loaded MySQL JDBC driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Exception attempting to load MySQL JDBC driver");
    }

    String url = "jdbc:mysql://localhost/db";

    Properties props = new Properties();
    props.put("user", "root");
    props.put("password", "root");

    try {
        conn = DriverManager.getConnection(url, props);
        conn.setAutoCommit(false);
    } catch (SQLException e) {
        Throwables.propagate(e);
    }
    ....

}

这是我在尝试运行代码后得到的错误堆栈跟踪:

java.lang.NoClassDefFoundError: Could not initialize class oracle.jdbc.OracleDriver
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at java.sql.DriverManager.getCallerClass(DriverManager.java:477)
    at java.sql.DriverManager.getConnection(DriverManager.java:576)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at exportclient.JDBCExportClient$JDBCDecoder.<init>(JDBCExportClient.java:179)
    at exportclient.JDBCExportClient.constructExportDecoder(JDBCExportClient.java:604)
    at export.processors.GuestProcessor$1.run(GuestProcessor.java:113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at utils.CoreUtils$1$1.run(CoreUtils.java:259)
    at java.lang.Thread.run(Thread.java:680)

这对我来说似乎很奇怪,因为:1)我没有尝试连接到Oracle数据库; 2)实际上我的classpath中有一个ojdbc6.jar(包含oracle.jdbc.OracleDriver)。所以我完全不知道为什么会发生这种错误。

任何建议将不胜感激。提前谢谢!