私は現在、このコードのいくつかの問題抱えている:準備文のエラー: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= ?
'
更新を追加する必要はありませんへ
こんにちは、私はこのエラーを取得します: – Pavol
@ Pavol which error? –
@Pavolはまだ問題があれば私の答えを更新しました。ここでコードをチェックしてください http://www.mkyong.com/jdbc/jdbc-preparestatement-example-select-list-of-the-records/ –