2013-02-16 177 views
-1

我有一個讓我瘋狂的問題,我真的不習慣使用mySQL數據庫,我真的需要一些幫助。我想連接到我的MySQL數據庫來運行查詢,並最終能夠將數據加載到數據庫中,並測量加載,查詢等所消耗的時間,以便我可以比較不同類型的數據庫上的性能。 我認爲我得到了forName的工作,但現在它抱怨dbUrl。 有人可以向我解釋我做錯了什麼?MySQL jdbc驅動程序

在此先感謝!

import java.sql.*; 
import java.util.Properties; 

import javax.sql.*; 


public class jdbcdemo{ 

public static void main(String args[]){ 
String dbtime; 
String dbUrl = "jdbc:mysql://localhost:3306/Meeting"; 
String dbClass = "com.mysql.jdbc.Driver"; 
String query = "Select * FROM customer"; 

Properties properties = new Properties(); 
properties.put("user", "student"); 
properties.put("password", "ingan86"); 

try { 

Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection (dbUrl,properties); 
Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery(query); 

while (rs.next()) { 
dbtime = rs.getString(1); 
System.out.println(dbtime); 
} 

con.close(); 
} 
catch(SQLException e) { 
e.printStackTrace(); 
} 

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


} 

} 

堆棧跟蹤:

java.sql.SQLException: Access denied for user 'student'@'localhost' (using password: YES) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:798) 
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3700) 
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1203) 
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2572) 
at com.mysql.jdbc.Connection.<init>(Connection.java:1485) 
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266) 
at java.sql.DriverManager.getConnection(Unknown Source) 
at java.sql.DriverManager.getConnection(Unknown Source) 
at jdbcdemo.main(jdbcdemo.java:22) 
+0

可以使用該登錄名/密碼組合從命令行連接嗎? – Axel 2013-02-16 22:23:15

+0

嘗試顯式添加'student' @'localhost'用戶並授予其權限,如@Reimus的答案。 – hd1 2013-02-16 22:27:38

+1

什麼是* exact *錯誤訊息? – 2013-02-16 23:16:35

回答

0

確保用戶student有足夠的權限來使用數據庫:

GRANT ALL PRIVILEGES 
ON Meeting.* 
TO [email protected] 
IDENTIFIED BY 'ingan86'; 
+0

非常感謝您的答案!我想通了,我只是把用戶從「學生」改爲「根」:) – glaring 2013-02-17 12:48:30