我會在我的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端口與信息連接被拒絕:連接。
或者(可能)德比服務器需要啓動。 OP在標題(mysql)和body(derby)之間包含矛盾的jdbc url。 –