Error searching in Database

Asked

Viewed 32 times

0

My application is integrated to my bank and makes several queries, but in one of them something unexpected is happening, because the search is not performed.

Note that my instruction line is simple and correct (it works when running manually in Mysql, for example.

Another fact is that the variable "aero" is replaced by "LAS" - which is one of the values of the bank - the search normally occurs.

I put those 3 test messages to know how far he’s going and the only one that doesn’t perform is "Test 3," which means for some reason doesn’t go beyond the rs = stmt.executeQuery();

I do not believe that the variable "Aero" is the problem, because it is going through just right (I have already printed it) and rs = stmt.executeQuery(); is the same as the other methods that work correctly.

Anyway, I’ve read and reread the code a few times and I can’t figure out what it might be.

Follows the code:

 public List<Dados> buscarAero(String aero, boolean check1, boolean check2) {

    Connection con = ConnectionDB.getConnection(); 
    PreparedStatement stmt = null;
    ResultSet rs = null;

    List<Dados> dados = new ArrayList<>(); 

    if (check1 && !check2) { 

        try {
            JOptionPane.showInputDialog("teste 1");

            stmt = con.prepareStatement("SELECT * FROM `2008` WHERE Origin = " + aero);

            JOptionPane.showInputDialog("teste 2");

            rs = stmt.executeQuery();

            JOptionPane.showInputDialog("teste 3");

            while (rs.next()) {    
                Dados info = new Dados();

                info.setFlightNum(rs.getInt("FlightNum"));
                info.setOrigin(rs.getString("Origin"));
                info.setDest(rs.getString("Dest"));

                dados.add(info);       
}
  • 1

    Do not post code image, post it directly in question.

  • 1

    @diegofm editei. Please remove the negative vote

1 answer

2


It is necessary to surround the value of aero in simple quotes, which is how Mysql will understand that this is a literal string and not some identifier.

stmt = con.prepareStatement("SELECT * FROM `2008` WHERE Origin = '" + aero + "'");
  • Perfect @jbueno. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.