0

次のレイアウトのデータベースがあります

databasename:macfast
table name:users

columns

  id,
  user_name,
  password,
  fullName

列 user_name からすべての値を取得し、各値を .But から既に取得されている別の文字列でチェックしたいのですが、TEXTFIELDできません( NullpointerException)。私を助けてください。

  public void deleteFclty() {

             PreparedStatement stmt = null;
             ResultSet rs = null;
             String username=removeText.getText();




     String qry = "SELECT user_name From users ";
    try {
        stmt = (PreparedStatement) connection.prepareStatement(qry);


     rs =  stmt.executeQuery();

     while (rs.next()) {
           String check=(rs.getString("user_name"));

           System.out.println(check);


           if(check.equals(username)){

                Util.showErrorMessageDialog("EQUAL");

           }else{

                     Util.showErrorMessageDialog("NOT EQUAL");
   }
     }
}catch (SQLException ex) {
        Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
    }
}
4

4 に答える 4

2

Problem is with the prepared statement (you don't put id into statement, which should be there instead of question mark).

Also I would recommend to do condition as "username.equals(check)", that can prevent null pointer exception.

于 2013-07-11T11:06:30.697 に答える
1
"SELECT user_name From users where id=?"

このクエリにはパラメーターがあり、それに値を設定していません。PreparedStatement#setInt()または同様のものを使用して設定します。

stmt.setInt(1, 1);
于 2013-07-11T11:15:12.237 に答える
0

The problem is with PreparedStatement as you are using Question mark ? in your query for that you have to set the Id value.

 String qry = "SELECT user_name From users where id=?";
 stmt = (PreparedStatement) connection.prepareStatement(qry);
 stmt.setInt(1,1001);
 rs =  stmt.executeQuery();
于 2013-07-11T11:31:01.810 に答える
0

以下のコメントであなたの質問について:

List<String> values = new ArrayList();
while (rs.next()) {
    values.add(0,rs.getString(<>))
}
// here do whatever you want to do...
// for example
for ( String value: values )
{
    // check if value == TEXTFIELD
    // if true then do something..
    // else don't do anything
}
于 2013-07-11T11:07:11.077 に答える