我使用用户登录,管理员登录等在考试系统上工作。 当我检查是否输入了用户名时(例如) 如果它已经在数据库中,则不返回 并再次创建!
try{
// Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/big_project_3","root","");
//imporrrrrrrrrrrrrrrrrrrrrrrtant
String selectquery = "select count(*)from uesrs where username='" + username + "'and password='" + password + "'";
Statement stat=con.createStatement();
System.out.println(selectquery);
ResultSet rs=stat.executeQuery(selectquery);
System.out.println(rs.next());
if(rs.next()==true){
infoMessage("Already registered ","Welcom");
}
else{
String insertQuery = "insert into uesrs values(null,'" + username_new_student.getText() + "','" + password_new_student.getText() + "','" + First_name_new_student.getText()+"')";
stat.executeUpdate(insertQuery);
infoMessage("info is inserted ","Alert!!!!!");
dispose();
user_login ul=new user_login();
ul.setLocationRelativeTo(null);
ul.setVisible(true);
}
}
catch (Exception ex) {
System.out.println(ex);
}
答案 0 :(得分:1)
一旦您在rs.next()
内调用System.out.println ()
,它将向前移动指针。然后,当您在rs.next()
条件内调用if
时,将不再显示结果。
try{
// Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/big_project_3","root","");
//imporrrrrrrrrrrrrrrrrrrrrrrtant
String selectquery="select count(*)from uesrs where username='"+username+"'and password='"+password+"'";
Statement stat=con.createStatement();
System.out.println(selectquery);
ResultSet rs=stat.executeQuery(selectquery);
if(rs.next() ){
infoMessage("Already registered ","Welcom");
}
else{
String insertQuery="insert into uesrs values(null,'"+username_new_student.getText()+"','"+password_new_student.getText()+"','"+First_name_new_student.getText()+"')";
stat.executeUpdate(insertQuery);
infoMessage("info is inserted ","Alert!!!!!");
dispose();
user_login ul=new user_login();
ul.setLocationRelativeTo(null);
ul.setVisible(true);
}
}
catch (Exception ex) {
System.out.println(ex);
}