I have three table 1.User 2.Branch 3 userbranch. I have trying to solve login form. but when login button is click it's show this error 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);
}
}
Shows error:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
Your SQL doesn't have any parameters:
Select u.username,u.password,b.branchname from user u, branch b, userbranch
ubwhere u.userid = ub.userid and b.branchid=ub.branchid
So it's failing when you try to set parameters. You probably want:
and u.userid = ? and u.password = ? and b.branchid = ?
... or something similar. Except that would suggest that you're storing passwords in plain text, which would be horrible from a security perspective.
Oh, and I think you want a space between ub
and where
...
See more on this question at Stackoverflow