-3

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 に答える