2016-10-06 4 views
1

私は現在、このコードのいくつかの問題抱えている:準備文のエラー:java.sql.SQLExceptionを:(0 1>パラメータの数、)範囲外のパラメータインデックス

public User validateUser(String username, String password) { 

    boolean found = false; 
    Connection c = DBHelperClass.getConnection(); 
    String query = "Select * from user where username= '?' and password= '?' "; 


    if (c != null) { 
     try { 
      PreparedStatement inserter = c.prepareStatement(query); 
      inserter.setString(1, username); 
      inserter.setString(2, password); 
      System.out.println("help: "+query); 
      ResultSet resultSet = inserter.executeQuery(query); 
      while (resultSet.next()) { 
       this.userId = resultSet.getInt("userId"); 
       this.username = resultSet.getString("usrname"); 
       this.password = resultSet.getString("password"); 
       this.address = resultSet.getString("address"); 
       this.email = resultSet.getString("email"); 
       this.phone = resultSet.getInt("phone"); 

       found = true; 

      } 
     } catch (SQLException ex) { 
      Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
    return this; 
} 

エラーを私は次のようになります:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

他の回答を読んで私は 'タグを使用することはできませんでした。

inserter.executeQuery();inserter.executeQuery(query);を変更します。

Select * from user where username= ? and password= ? 

'

更新を追加する必要はありませんへ

答えて

5

変更

Select * from user where username= '?' and password= '?' 
+0

こんにちは、私はこのエラーを取得します: – Pavol

+0

@ Pavol which error? –

+0

@Pavolはまだ問題があれば私の答えを更新しました。ここでコードをチェックしてください http://www.mkyong.com/jdbc/jdbc-preparestatement-example-select-list-of-the-records/ –

0

クエリにゼロパラメータがあるというエラーメッセージが表示されます。アポストロフィを使用して疑問符文字をエスケープしたためです。 疑問符を中心にアポストロフィを使用せずにクエリを書き直してください。

String query = "Select * from user where username= ? and password= ?"; 
1

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

あなたがexecuteQuery(query)の代わりに、それはそう単純に、クエリが任意のパラメータを必要としませんが、あなたは、この例外につながるつ以上を提供ことを考慮することなどexecuteQuery()を呼び出すため、このエラーが出ますinserter.executeQuery()を使用してください。


すでに述べたように、第2のエラーは、それが唯一のSelect * from user where username= ? and password= ?

0

ヘルプみんなのおかげで、これは固定コードである必要があり、あなたのクエリに引用符を使用する必要がないという事実である。

public User validateUser(String username, String password) { 

    boolean found = false; 
    Connection c = DBHelperClass.getConnection(); 
    String query = "Select * from user where username= ? and password= ? "; 


    if (c != null) { 
     try { 
      PreparedStatement inserter = c.prepareStatement(query); 
      inserter.setString(1, username); 
      inserter.setString(2, password); 
      System.out.println("help: "+query); 
      ResultSet resultSet = inserter.executeQuery(); 
      while (resultSet.next()) { 
       this.userId = resultSet.getInt("userId"); 
       this.username = resultSet.getString("username"); 
       this.password = resultSet.getString("password"); 
       this.address = resultSet.getString("address"); 
       this.email = resultSet.getString("email"); 
       this.phone = resultSet.getInt("phone"); 

       found = true; 

      } 
     } catch (SQLException ex) { 
      Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
    return this; 
} 

私が変更する必要があったのは、 "周囲を取り除くことでしたか? executeQuery(query)to executeQuery()を変更してください

ありがとうございます!

関連する問題