Search in mysql database

Asked

Viewed 90 times

2

I have to do a search in a table, I have to pass up to 3 parameters to filter my search, this is my method:

public List<Motores> pesquisaMotores(String serie, String marca, String modelo) {
    PreparedStatement comando = null;
    ResultSet lista_resultados = null;
    List<Motores> motores = new ArrayList();

    String sql = "SELECT *FROM motores WHERE numMotor LIKE ? AND marcaMotor LIKE ? AND modeloMotor LIKE ?";

    try {
        comando = BD.conection.prepareStatement(sql);
        comando.setString(1, serie + "%");
        comando.setString(2, marca + "%");
        comando.setString(3, modelo + "%");
        lista_resultados = comando.executeQuery(sql);
        while (lista_resultados.next()) {
            motores.add(
                    new Motores(
                            lista_resultados.getString("numMotor"),
                            lista_resultados.getString("modeloMotor"),
                            lista_resultados.getString("rpmMotor"),
                            lista_resultados.getString("polos"),
                            lista_resultados.getString("volts"),
                            lista_resultados.getString("amps"),
                            lista_resultados.getString("marcaMotor"),
                            lista_resultados.getString("carc"),
                            lista_resultados.getString("hz"),
                            lista_resultados.getString("qtdCanais"),
                            lista_resultados.getString("tipoEnrol"),
                            lista_resultados.getString("canal"),
                            lista_resultados.getString("cvMotor"),
                            lista_resultados.getString("dataColeta"),
                            lista_resultados.getInt   ("numBobinas")));
        }
        comando.close();
    } catch (SQLException ex) {
        Logger.getLogger(interfaces.ClientePesquisa.class.getName()).log(Level.SEVERE, null, ex);
        motores = null;
    }
    Collections.sort(motores);
    return motores;
}

but the same only brings me this mistake:

GRAVE: null

com.mysql.jdbc.exceptions.jdbc4.Mysqlsyntaxerrorexception: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '?' at line 1

If I do the direct search in the database, that is, running the sql string, changing the "?" for some value, the same works. Any suggestions?

1 answer

1


EDIT 1

Missing a space between select, * and from. It would look like this:

String sql = "SELECT * FROM motores WHERE numMotor LIKE ? AND marcaMotor LIKE ? AND modeloMotor LIKE ?";

EDIT 2

You must be running wrong:

lista_resultados = comando.executeQuery(sql);

The right thing would be:

lista_resultados = comando.executeQuery();
  • It keeps popping up: GRAVE: null com.mysql.jdbc.exceptions.jdbc4.Mysqlsyntaxerrorexception: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '? AND marcaMotor LIKE ? AND modelMotor LIKE ? ' at line 1

  • 1

    See the EDIT 2...

Browser other questions tagged

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