Message in AutoComplete JtextField in java eclipse

-1

Can someone help me with my problem please? I have a text field that has the autocomplete action when writing something about it. My problem would be how do I do when I enter another word that does not appear in the autocomplete I get a message saying that the word entered is not correct? I leave the code

public void calle(){

    try{
    TextAutoCompleter completar = new TextAutoCompleter(txtCalle);

    String  SQL = "SELECT nombre FROM calle";

    Statement st = cn.createStatement();
    ResultSet rs = st.executeQuery(SQL);

       while(rs.next())
       {

           completar.addItem(rs.getString("nombre"));

       }


    }
    catch(Exception e) { JOptionPane.showConfirmDialog(null, e);}

}
    
asked by Victor 11.12.2016 в 18:29
source

1 answer

0

Well, if the code you provide works with existing words in the query, the ResultSet can verify it if it is empty:

public void calle(){

   try{
   TextAutoCompleter completar = new TextAutoCompleter(txtCalle);

   String  SQL = "SELECT nombre FROM calle";

   Statement st = cn.createStatement();
   ResultSet rs = st.executeQuery(SQL);

   if(!rs.next()){
     completar.addItem("Palabra no existe");
     return;
   }

      while(rs.next())
      {

         completar.addItem(rs.getString("nombre"));

      }


      }
     catch(Exception e) { JOptionPane.showConfirmDialog(null, e);}

}

Edit

If you look at your code, you are always selecting ALL from your table calle , so it would never enter the declaration if , a better implementation would be to perform a search depending on what you are looking for (which is precisely what you want), it is totally unnecessary and inefficient to extract all the information provided and if not then keep it in an arrangement ...

Something like that:

public void calle(String palabra){

  String queryString = "SELECT nombre FROM calle WHERE nombre LIKE ?";
  PreparedStatement ps =  cn.prepareStatement(queryString);
  ps.setString(1,"%"+palabra+"%");

  ResultSet rs = ps.executeQuery();

  // resto de código ....

}

In this way, select what contains the word and not ALL, apart from this you can verify that it is empty ..

    
answered by 11.12.2016 в 18:59