我有三个表 1.User 2.Branch 3 userbranch。我试图解决登录表单。但是当单击登录按钮时,它会显示此错误 java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)。
public Boolean loginApplication(Connection con, String uname, String pwd, String brnch) {
try {
PreparedStatement ps = con.prepareStatement("Select u.username,u.password,"
+ "b.branchname from user u, branch b ,userbranch ub"
+ "where u.userid = ub.userid and b.branchid=ub.branchid ");
ps.setString(1, uname);
ps.setString(2, pwd);
ps.setString(3, brnch);
ResultSet rs = ps.executeQuery();
System.out.println("query return " + rs);
if (rs.next()) {
return true;
//true if query found any corresponding data
}
else{
return false;
}
}
catch (SQLException ex) {
System.out.println("Error while validating " + ex);
return false;
}
}
private void buttonloginActionPerformed(java.awt.event.ActionEvent evt) {
String uname=username.getText();
String upass=userpassword.getText();
String ubranch=userbranch.getSelectedItem().toString().trim();
if(evt.getSource()==buttonlogin){
if(user.loginApplication(connect.getCon(),uname,upass,ubranch)){
System.out.println("success");
MainForm mainForm=new MainForm();
mainForm.setVisible(true);
}
}
else{
JOptionPane.showMessageDialog(null, "Login failed!","Failed!!",
JOptionPane.ERROR_MESSAGE);
}
}
显示错误:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).