-3

我有三个表 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).
4

2 回答 2

2

您的 SQL 没有任何参数:

Select u.username,u.password,b.branchname from  user u, branch b, userbranch
ubwhere u.userid = ub.userid and b.branchid=ub.branchid

因此,当您尝试设置参数时它会失败。你可能想要:

and u.userid = ? and u.password = ? and b.branchid = ?

...或类似的东西。除了这会建议您以纯文本形式存储密码,从安全角度来看,这将是可怕的。

哦,我想你想要ub和之间有一个空间where......

于 2013-10-05T11:28:22.130 回答
1

基本上,问题如错误消息所示。您的 SQL 语句没有参数,但您正在尝试设置一些参数。

SQL 语句中的(未命名的)参数由?占位符指示。

于 2013-10-05T11:28:05.867 回答