Error fetching the database

0

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

Note that my statement line is simple and correct (it works when I run MySql manually, for example.

Another fact is that if you change the variable "aero" to "LAS" - which is one of the values of the bank - the search usually occurs.

I put those 3 test messages to find out where it is going and the only one that does not run is "Test 3", that is, for some reason it does not go beyond rs = stmt.executeQuery();

I do not think the "Aero" variable is the problem, because it is passing correctly (I have already tried to print it) and rs = stmt.executeQuery(); is the same as other methods that work correctly.

Anyway, I have read and re-read the code a few times and I do not find out what it can be.

Follow 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);       
}
    
asked by anonymous 27.11.2016 / 21:56

1 answer

2

It is necessary to enclose the value of aero in single 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 + "'");
    
27.11.2016 / 22:52