java.sql.SQLEXCEPTION:拒绝访问用户&root;' @' localhhost' (使用密码:是)

时间:2015-11-09 12:51:16

标签: java

public static Connection ConnectDB() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms_db","root","golu");
        return con;
    } catch(ClassNotFoundException | SQLException e) {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }      
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException  
  at Login.txtPasswordKeyPressed(Login.java:204)  
  at Login.access$300(Login.java:14)  
  at Login$4.keyPressed(Login.java:74)  
  at java.awt.Component.processKeyEvent(Component.java:6493)  
  at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)  
  at java.awt.Component.processEvent(Component.java:6312)  
  at java.awt.Container.processEvent(Container.java:2236)  
  at java.awt.Component.dispatchEventImpl(Component.java:4891)  
  at java.awt.Container.dispatchEventImpl(Container.java:2294)  
  at java.awt.Component.dispatchEvent(Component.java:4713)  
  at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
  at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
  at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
  at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
  at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
  at java.awt.Component.dispatchEventImpl(Component.java:4762)  
  at java.awt.Container.dispatchEventImpl(Container.java:2294)  
  at java.awt.Window.dispatchEventImpl(Window.java:2750)  
  at java.awt.Component.dispatchEvent(Component.java:4713)  
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)  
  at java.awt.EventQueue.access$500(EventQueue.java:97)  
  at java.awt.EventQueue$3.run(EventQueue.java:709)  
  at java.awt.EventQueue$3.run(EventQueue.java:703)  
  at java.security.AccessController.doPrivileged(Native Method)  
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
  at java.awt.EventQueue$4.run(EventQueue.java:731)  
  at java.awt.EventQueue$4.run(EventQueue.java:729)  
  at java.security.AccessController.doPrivileged(Native Method)  
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)  
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)  
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的用户名是" admin" ,密码是" 12345" 。 请有人帮我解决这些错误。

3 个答案:

答案 0 :(得分:0)

你传递'root'就像用户名和'golu'一样传递它是正确的吗?因为在你的帖子中你说用户名和密码是其他的。

答案 1 :(得分:0)

您为mysql数据库传递了错误的用户名和密码以获取jdbc连接。

 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms_db","<valid-mysql-username>","<valid-mysql-password>");

答案 2 :(得分:0)

确保您已在项目中加入the library of JDBC driver

并改变它:

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/<database name>","<user>","<password>");