我是Netbeans的新手。我正在尝试通过Netbeans和mysql workbench在Store Management上创建一个新项目作为我的数据库。但我在连接数据库时遇到问题。这里给出了我的代码:
enter code here
package MyDb.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DbConnection {
private static Connection conn=null;
static{
Throwable e = null;
try {
Class.forName("com.mysql.jdbc.Driver");
try {
System.out.println("Connecting Database!!");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/GanpatiDB","root","");
System.out.println("");
} catch (SQLException ex) {
throw new RuntimeException("Cannot connect the database!", e);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, e);
System.out.println("Terminating application...check database");
System.exit(0);
}
finally {
System.out.println("Closing the connection.");
}
}
private static Object connection;
public DbConnection(){
}
public static Connection getConnection(){
return conn;
}
public static void closeConnection() throws Exception{
conn.close();
}
}
调试项目后,控制台将结果显示为
建立成功(总时间:0秒) 但它仍然显示如下错误:
Jan 14, 2014 1:47:13 PM MyDb.utils.DbConnection <clinit>
Terminating application...check database
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'GanpatiDB[root on Default schema]'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:233)
at MyDb.utils.DbConnection.<clinit>(DbConnection.java:20)
at desktopapplication1.NewJFrame.<init>(NewJFrame.java:27)
at desktopapplication1.NewJFrame$2.run(NewJFrame.java:199)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:0)
构建显示成功是有充分理由的:您正在捕获实际错误并自己制作日志语句:
catch (ClassNotFoundException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, e);
System.out.println("Terminating application...check database");
System.exit(0);
}
因为它正在捕获错误,所以代码按预期执行(如果您希望它显示为失败,则应将System.exit
的参数更改为-1)。
您捕获的错误是ClassNotFoundException
,Java似乎无法找到您尝试使用Class.forName("com.mysql.jdbc.Driver");
找到的MySQL驱动程序。如果你查看日志,根本没有提到驱动程序,这似乎支持这一理论。