2015-10-21 60 views
0

使用mac on Yosemite上的java連接到mysql數據庫以及CLASS PATH中的jdbcdriver jar文件的完整url。 netbeans控制檯給出了「java.sql.SQLException:訪問拒絕用戶'root'@'localhost'(使用密碼:YES)」。我嘗試了很多方法作爲論壇,但它仍然不起作用。連接失敗!檢查輸出控制檯

這裏我的代碼

//STEP 1. Import required packages 
import java.sql.*; 

public class JDBCExample { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost:3307/library"; 

    // Database credentials 
    static final String USER = "root"; 
    static final String PASS = "default$"; 

    public static void main(String[] args) { 
    Connection conn = null; 
    Statement stmt = null; 
    ResultSet rs = null; 
    try{ 
     //STEP 2: Register JDBC driver (automatically done since JDBC 4.0) 
     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 4: Execute a query 
     System.out.println("Creating database..."); 
     stmt = conn.createStatement(); 
     rs = stmt.executeQuery("select uid uname from User"); 

     //STEP 5: Process the results 
     while(rs.next()){ 
      System.out.println("User ID="+rs.getInt("uid")+", Name="+rs.getString("uname")); 
     } 

    }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 
    System.out.println("Goodbye!"); 
}//end main 
}//end JDBCExample 

這裏有結果:

Connection Failed! Check output console 
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) 
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1686) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084) 
    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:408) 
    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 com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:422) 
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:134) 
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:105) 
    at cs157a_test.DataSourceTester.main(DataSourceTester.java:54) 
+1

您確定,您的db url端口(默認爲3306),用戶名和密碼是否正確?只是確保它 –

+0

AFAIK這將是其他錯誤的情況下,如「拒絕連接」的錯誤端口 @ B-Y你能夠連接co mysql命令行客戶端與代碼中指定的憑據嗎?例如。在終端'mysql -u root -p'? – Klever

回答

0

你得告訴你,你已經成功地連接到服務器,但是,root不允許用戶異常/批從localhost連接。

您必須檢查您的mysql用戶權限表,並確保您有一行允許rootlocalhost連接正確的密碼。

下面的SQL將顯示此:

SELECT host, user FROM mysql.user; 

如果沒有爲rootlocalhostroot%那麼你必須有這個SQL重置您的密碼中的一行:

set password for 'root'@'localhost' = password('your_password'); 

如果沒有行,您必須創建它,然後在您想要與該用戶訪問的數據庫上設置權限。

您可以用下面的SQL實現這一點:

CREATE USER 'root'@'localhost' IDENTIFIED BY 'your_password'; 
GRANT ALL ON YOU_DATABASES.* TO 'root'@'localhost'; 

最後你要刷新權限或重啓MySQL。再試一次,確保你會連接。

+0

我展示了我所做的,但它不起作用。我錯了什麼? –

+0

@ B-Y我希望你的代碼中的密碼字符串如下所示: 'static final String PASS =「829891」;' – Klever

+0

您是否在更改密碼後重新啓動mysql? – malaguna

0

您想檢查一下嗎?這個對我有用。

import java.*; 
    import java.sql.*; 
    import java.util.*; 
    //STEP 1. Import required packages 
    import java.sql.*; 

    public class Memory { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost:3306/library"; 

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

    public static void main(String[] args) { 
    Connection conn = null; 
    Statement stmt = null; 
    ResultSet rs = null; 

    try{ 
     //STEP 2: Register JDBC driver (automatically done since JDBC 4.0) 
     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 4: Execute a query 
     System.out.println("Creating database..."); 
     stmt = conn.createStatement(); 
     rs = stmt.executeQuery("select uid, uname from uisend"); 

     //STEP 5: Process the results 
     while(rs.next()){ 
      System.out.println("User ID="+rs.getInt("uid")+", Name="+rs.getString("uname")); 
     } 

    }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 
    System.out.println("Goodbye!"); 
    }//end main 
    }//end JDBCExample 
+0

我更改了2行代碼。static final String DB_URL =「jdbc:mysql:// localhost:3306/library」; static final String PASS =「root」;如果我的答案是upvoted,那麼其他人會知道這個答案有效,並可以像你一樣從中受益。 – Don

0

我不知道它爲什麼作爲我的代碼,不需要改變。

User ID=1001, Name=17 
User ID=1002, Name=Kim 
User ID=1003, Name=Jane Koffman 
User ID=1004, Name=Katherine H. Lang 
User ID=1005, Name=Smith 
User ID=1006, Name=Juanita J. Palmer 
User ID=1007, Name=Otherone with no age 
User ID=1008, Name=Ethel W. Williams 
User ID=1009, Name=Someone with no age 
User ID=1010, Name=Candis C. Whitehead 
User ID=1011, Name=Kim 
User ID=1012, Name=Margaret F. Delmonte 
User ID=1013, Name=Susan M. McKeel 
User ID=1014, Name=Kim 
User ID=1015, Name=Shirley A. Dehaven 
User ID=1016, Name=Smith 
User ID=1017, Name=Chad G. Turner 
User ID=1018, Name=Suzanne J. Champine 
User ID=1019, Name=Harry King 
Goodbye!