-1
我正在嘗試使用JDBC執行用戶登錄系統,並且一直收到「開始結果集之前」錯誤。有人能解釋並告訴我我的代碼中有什麼問題嗎?謝謝!結果開始之前在數據庫中設置
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
Connection conn2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
Statement stmt = conn.createStatement();
Statement stmt2 = conn2.createStatement();
ResultSet Rs = stmt.executeQuery("SELECT * FROM users");
ResultSet Rs2 = stmt2.executeQuery("SELECT * FROM users");
String uN = this.userName.getText();
char[] password = this.passWord.getPassword();
String pW = String.valueOf(password);
String RsUser = Rs.getString("Name");
String RsPass = Rs2.getString("Password");
String Query = "SELECT * FROM USERS WHERE NAME='"+uN+"' AND PASSWORD='"+pW+"'";
stmt.executeQuery(Query);
while(Rs.next()) {
Rs.beforeFirst();
Rs.next();
if(uN.equals(RsUser)) {
while(Rs2.next()) {
if(pW.equals(RsPass)) {
this.dispose();
GameLauncherGUI gui = new GameLauncherGUI();
gui.setVisible(true);
gui.setTitle("Launcher");
gui.setLocationRelativeTo(null);
} else {
System.out.println("Wrong username");
}
}
}
}
} catch(SQLException ex) {
System.err.println(ex.getMessage());
}
順便說一句,要清楚: 的userName是jTextField的變量名,passWord是jPasswordField的變量名。在你的while循環中使用 – Nuby
,你實際上用Rs.beforeFirst();重置了你的ResultSet,看起來它可能導致無限循環以防止有多個結果。 – cello