2014-07-19 94 views
5

我使用Eclipse的Java連接與MySQL數據庫值java.sql.SQLException:拒絕訪問用戶 '根@本地' @ 'localhost' 的(使用密碼:YES)

CODE

import java.sql.*; 
import java.io.*; 

public class DbDemo { 

    public static void main(String args[]) throws ClassNotFoundException, SQLException { 

     String s;  
     String uname="[email protected]"; 
     String url="jdbc:mysql://localhost:3306/student"; 

     String password="Hsun123"; 

     int i; 

     try { 

      Class.forName("com.mysql.jdbc.Driver").newInstance(); 

      Connection con=DriverManager.getConnection(url,uname,password); 

      Statement st=con.createStatement(); 

      ResultSet rs=st.executeQuery("select * from student_detail"); 

      if(rs.next()) { 

       i=rs.getInt(1); 

       s=rs.getString(2); 

       System.out.println(i+"/t"+s); 
      }   

      rs.close(); 

      st.close(); 

      con.close(); 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

錯誤

java.sql.SQLException: Access denied for user '[email protected]'@'localhost' (using password: YES) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:935) 
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4101) 
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1300) 
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337) 
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370) 
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154) 
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792) 
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:381) 
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) 
at java.sql.DriverManager.getConnection(Unknown Source) 
at java.sql.DriverManager.getConnection(Unknown Source) 
at database.DbDemo.main(DbDemo.java:13) 

我應該怎麼做才能解決我的問題?

+0

是root @ localhost的密碼正確嗎? –

+3

我想'字符串uname =「根」;' –

回答

0

試試這個...

String password="Hsun123"; 

,而不是

String password=""; 
+2

OP已經使用'字符串密碼=「Hsun123」;',是嗎? –

2

而不是使用:

String uname="[email protected]"; 

用途:

String url="jdbc:mysql://localhost:3306/student"; 
String userName="root" 
String password="Hsun123" 
... 
try{ 

     Class.forName("com.mysql.jdbc.Driver").newInstance(); 

     Connection con=DriverManager.getConnection(url,username,password); 
... 

這應該工作(前提是您要設置符合要求的密碼)

+0

謝謝.......它工作........... –

+0

@ user3855389不客氣... –

+0

有人可以刪除我添加的10米。我做了一個4個字符的變化,用'usernamename - >用戶名',不符合6個字符的限制。所以我想通過添加額外的字符,然後刪除它們在一個單獨的編輯將是一個聰明的方式圍繞規則。事實並非如此。對於那個很抱歉。 –

0

對於那些誰從這個問題的痛苦

值java.sql.SQLException:拒絕訪問用戶「根」 @ '本地主機'

(使用密碼:是)

解是:

無論密碼,您所提供的這行代碼中的

Connection con=DriverManager.getConnection( 
      "jdbc:mysql://localhost:3306/Pravin","root","password"); 

無需提供此密碼只需插入""

完整代碼:

import java.sql.*; 
class TestDB{ 
public static void main(String args[]){ 
try{ 
Class.forName("com.mysql.jdbc.Driver"); 
Connection con=DriverManager.getConnection( 
     "jdbc:mysql://localhost:3306/Pravin","root",""); 
//here sonoo is database name, root is username and password 
Statement stmt=con.createStatement(); 
ResultSet rs=stmt.executeQuery("select * from student"); 
while(rs.next()) 
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); 
con.close(); 
}catch(Exception e){ System.out.println(e);} 
} 
} 
1

好了,同樣的問題這裏。我正在使用phpmyadmin。

我不得不去phpmyadmin->users->root->Edit Privileges.

此時場「Database-specific privileges」我必須爲我的數據庫提供的特權。

在字段「Change password」我重新輸入密碼(我不知道爲什麼我必須這樣做,也許是因爲我已經有一個可能的錯誤)。

在字段「Login Information->Host」我把價值任何主機。

問題解決了。

相關問題