2014-11-14 46 views
0

我會在我的Class.forName裏面寫些什麼?剛剛對於java來說我很新,而且我很難連接數據庫。 謝謝你的幫助如果我有jdbc:mysql:// localhost:3306

我試過德比,但我的數據庫是在mysql中使用xampp。

Statement stmnt; 
    ResultSet rs; 
    try{ 
     String host = "jdbc:derby://localhost:3306/population"; 
     String uName= "root"; 
     String uPass="root"; 
     Connection con = DriverManager.getConnection(host, uName, uPass); 
     //execute some sql and load into the result set 
     stmnt = con.createStatement(); 
     String sql = "SELECT * FROM users_admin"; 
     rs = stmnt.executeQuery(sql); 
     //move the cursor the first record and get the data 
     rs.next(); 
     int id = rs.getInt("admin_id"); 
     String id_admin = Integer.toString(id); 
     String username=rs.getString("username"); 
     String pwd = rs.getString("password"); 

     ta_user.setText(username); 
     ta_pwd.setText(pwd); 
     ta_id.setText(id_admin); 


    } 
    catch(SQLException err){ 
     System.out.println(err.getMessage()); 
    } 

我已經試過了,代碼在我的其他樣本項目,它顯示了一個錯誤:

java.net.ConnectException:錯誤連接到本地主機服務器上的3306端口與信息連接被拒絕:連接。

回答

2

好消息!您不需要Class.forName,並且您的JDBC驅動程序已成功加載。現在你需要一個mysql服務器,或者derby服務器,你在問題中使用兩個不同的jdbc url,在端口3306上偵聽連接。

0

你不需要它。 Connection refused表示您的驅動程序運行正常,並且您的MySQL或Derby數據庫服務器(無論您的標題和問題中的矛盾URL是否相信)都不會啓動,甚至可能安裝在本地主機上。

+0

或者(可能)德比服務器需要啓動。 OP在標題(mysql)和body(derby)之間包含矛盾的jdbc url。 –

相關問題