2012-08-15 278 views
2

我對我的項目有麻煩,它涉及到JDBCMysql連接。java.sql.SQLException:拒絕訪問用戶'root'@'localhost'(使用密碼:否)

當我做到以下幾點:

private Statement m_statement = null;    // statement 
private Connection m_connection = null;    // connection 

/** 
* Constructor #1 of the Database class 
* @throws ClassNotFoundException 
*/ 
public Database() throws ClassNotFoundException 
{ 
    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     MysqlDataSource ds = new MysqlConnectionPoolDataSource(); 
     ds.setServerName("localhost"); 
     ds.setPort(3306); 
     ds.setUser("root"); 
     ds.setPassword(""); 
     this.m_connection = ds.getConnection(); 
     this.m_statement = this.m_connection.createStatement(); 
    } 

    catch (SQLException s) 
    { 
     System.out.println("SQL statement is not executed! 2!"); 
    } 

} 

當我嘗試使用上面的代碼連接,連接不能正常工作,這裏是堆棧跟蹤:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919) 
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334) 
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:443) 
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:141) 
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:111) 
    at db.Database.<init>(Database.java:99) 
    at controller.LoginPage.doGet(LoginPage.java:61) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

如何解決它呢?

問候

+4

你確定你沒有一個密碼更改您的密碼? – jn1kk 2012-08-15 15:17:26

+0

好像你的密碼錯了。 – 2012-08-15 15:18:42

+0

您可以嘗試使用這些憑據手動登錄到您的MySQL數據庫嗎? – Tudor 2012-08-15 15:19:41

回答

3

我遇到了同樣的錯誤,這是我的固定它:

我用DriverManager類是這樣的:

Class.forName("com.mysql.jdbc.Driver"); 
    Connection Hello = DriverManager.getConnection("jdbc:mysql://YOUR-URL-HERE", "USERNAME", "PASSWORD"); 

就是這樣。另外,選擇一個表時,remenber首先把數據庫的名稱,如:

"DB_NAME.TABLE_NAME". 
+0

有人可以舉一個這個部分的例子:YOUR-URL-HERE – Yster 2016-01-08 09:53:29

+0

不要擔心,找到它。第一個參數:「jdbc:mysql://127.0.0.1:3306/myDBName」 – Yster 2016-01-08 10:09:27

2

檢查,如果MySQL有一個密碼,進入你的mysql安裝目錄,並在bin目錄設法提出使用

./mysql -u MySQL的root mysql的

提示

如果登錄過程沒有任何問題,則表示沒有設置密碼。
您可以使用mysqadmin腳本來設置/重置密碼。

./mysqladmin -u root密碼 「的口令字符串」

2

您好像纔剛剛mysql安裝。運行mysql_secure_installation。它會提示您輸入root密碼等。

0

無論是用戶名/密碼錯誤或用戶缺少權限訪問模式/表。授予特權使用

授予所有特權在 TO'root'@'localhost'WITH GRANT OPTION

1

我有同樣的問題。您需要爲用戶設置密碼。轉到mysql控制檯。

$ mysqladmin -u root password myFirstpassword 

然後在Java程序中

相關問題