使用JDBC时是否出现MissingResourceException?

时间:2012-09-19 10:45:25

标签: java mysql exception jdbc

之前我曾经多次使用JDBC没有任何问题,但由于某些原因,在新服务器上我没有使用它。代码在我的主机上编译并运行良好,并且所有库(包括JDBC)都应该编译成我运行的单个JAR。

基本代码:

try
{
    Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
    System.err.println("Cannot load driver...");
    e.printStackTrace();
}

try
{
    connection = DriverManager.getConnection("jdbc:mysql://localhost/dbname?user=dbuser&password=dbpass");
    statement = connection.createStatement();
}
catch (SQLException e)
{
    System.err.println("Cannot connect to database...");
    e.printStackTrace();
}

错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.mysql.jdbc.Util.stackTraceToString(Util.java:355)
    at com.mysql.jdbc.Util.<clinit>(Util.java:120)
    at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:764)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:222)
    at com.test.Server.<init>(Server.java:78)
    at com.test.Server.main(Server.java:49)
Caused by: java.lang.RuntimeException: Can't load resource bundle due to underlying exception java.util.MissingResourceException: Can't find bundle for base name com.mysql.jdbc.LocalizedErrorMessages, locale en_GB
    at com.mysql.jdbc.Messages.<clinit>(Messages.java:61)
    ... 8 more
Caused by: java.util.MissingResourceException: Can't find bundle for base name com.mysql.jdbc.LocalizedErrorMessages, locale en_GB
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1539)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1278)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:733)
    at com.mysql.jdbc.Messages.<clinit>(Messages.java:59)
    ... 8 more

我之前从未遇到过这种错误,谷歌根本没有帮助。我已经检查了数据库凭据,端口号等。非常感谢您解决它的任何帮助!

1 个答案:

答案 0 :(得分:-3)

替换..

connection = DriverManager.getConnection("jdbc:mysql://localhost/dbname?user=dbuser&password=dbpass");

与..bcoz默认端口为3306 for mysql.If更改为

DriverManager.getConnection("jdbc:mysql://localhost:3306/minebench", "username", "password"); 

并添加&#34; mysql-connector-java-5.1.0-bin.jar&#34;。 然后尝试一下。