-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