JDBC:连接到数据库时出现ExceptionInInitializerError

时间:2016-04-26 17:43:06

标签: java jdbc

我尝试使用下面的代码连接到数据库但是我在控制台输出上得到了java.lang.ExceptionInInitializerError。任何问题来自哪里?随意编译以下代码以验证

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class RetrieveData
{
    static final String DB_URL = "jdbc:mysql://127.0.0.1/mysql";
    static final String DB_USER = "root";
    static final String DB_PASS = "";

    static ResultSet result = null;
    static Connection conn = null;
    static Statement statement = null;

    public static void main(String args[])
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            new com.mysql.jdbc.Driver();
        }
        catch(Exception e)
        {
            System.out.println("Sorry the jdbc lib was not found");
        }

        System.out.println("Connecting to the selected database...");

        try
        {
            conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
        }
        catch(SQLException e)
        {
            System.out.println("Error connecting to the selected database");
        }

        System.out.println("Connected to database successfully");

    }
}

异常追踪:

java.lang.ExceptionInInitializerError
    at com.mysql.jdbc.Util.stackTraceToString(Util.java:357)
    at com.mysql.jdbc.Util.<clinit>(Util.java:108)
    at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:726)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:318)
    at java.sql.DriverManager.getConnection(DriverManager.java:179)
    at java.sql.DriverManager.getConnection(DriverManager.java:213)
    at RetrieveData.main(RetrieveData.java:33)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.aide.ui.build.java.RunJavaActivity$1.run(SourceFile:108)
    at java.lang.Thread.run(Thread.java:848)
Caused by: java.lang.RuntimeException: Can't load resource bundle due to underlying exception java.util.MissingResourceException: Can't find resource for bundle 'com.mysql.jdbc.LocalizedErrorMessages_en_US', key ''
    at com.mysql.jdbc.Messages.<clinit>(Messages.java:54)
... 11 more
Caused by: java.util.MissingResourceException: Can't find resource for bundle 'com.mysql.jdbc.LocalizedErrorMessages_en_US', key ''
    at java.util.ResourceBundle.missingResourceException(ResourceBundle.java:238)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:230)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:139)
    at com.mysql.jdbc.Messages.<clinit>(Messages.java:52)
    ... 11 more

0 个答案:

没有答案