2016-02-11 48 views
0

我在嘗試連接到數據庫java的mysql的jdbc無法連接使用密碼

java.sql.SQLException: Access denied for user 'addressapp'@'localhost' (using password: YES) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) 
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694) 
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215) 
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255) 
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286) 
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085) 
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795) 
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) 
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400) 
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327) 
at java.sql.DriverManager.getConnection(DriverManager.java:664) 
at java.sql.DriverManager.getConnection(DriverManager.java:247) 
at ch.makery.address.mysql.MysqlConnection.test(MysqlConnection.java:29) 
at ch.makery.address.MainApp.start(MainApp.java:37) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:745) 



static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/test"; 

// Database credentials 
static final String USER = "addressapp"; 
static final String PASS = "addressapp"; 

public void test() { 
Connection conn = null; 
Statement stmt = null; 
try{ 
    //STEP 2: Register JDBC driver 
    Class.forName("com.mysql.jdbc.Driver"); 

    //STEP 3: Open a connection 
    System.out.println("Connecting to database..."); 
    conn = DriverManager.getConnection(DB_URL,USER,PASS); 
} 
    //STEP 6: Clean-up environment 
    rs.close(); 
    stmt.close(); 
    conn.close(); 
}catch(SQLException se){ 
    //Handle errors for JDBC 
    se.printStackTrace(); 
}catch(Exception e){ 
    //Handle errors for Class.forName 
    e.printStackTrace(); 
}finally{ 
    //finally block used to close resources 
    try{ 
     if(stmt!=null) 
      stmt.close(); 
    }catch(SQLException se2){ 
    }// nothing we can do 
    try{ 
     if(conn!=null) 
      conn.close(); 
    }catch(SQLException se){ 
     se.printStackTrace(); 
    }//end finally try 
}//end try 

問題是,當這個錯誤

  1. 我可以連接使用MySQL工作臺使用該用戶的憑據
  2. 到DB
  3. 如果沒有密碼,我可以使用JDBC連接到數據庫(例如,用戶root或將用戶的addressapp密碼更改爲空白
  4. I ca n不是連接使用使用JDBC
  5. 任何密碼時DB

回答

1

問題是我在我的分貝女巫「匿名」的用戶沒有密碼。刪除它後,問題解決了

0

嘗試

Properties connProps = new Properties(); 
connProps.put ("user", USER); 
connProps.put ("password", PASS); 

Connection theConnection = DriverManager.getConnection (DB_URL, connProps); 
+0

沒有工作 –