1

Eu tenho que fazer uma busca numa tabela, eu tenho que passar até 3 parâmetros para filtrar minha busca, este é meu método:

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;
}

porém o mesmo so me tras este erro:

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

Se eu faço a busca direto no banco, isto é, executando a string sql, trocando as "?" por algum valor, o mesmo funciona. Alguma sugestão?

0

1 Resposta 1

1

EDIT 1

Falta um espaço entre o select, * e from. Ficaria assim:

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

EDIT 2

Você deve está rodando de forma errada:

lista_resultados = comando.executeQuery(sql);

O correto seria:

lista_resultados = comando.executeQuery();
2
  • Continua aparecendo isso: 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 modeloMotor LIKE ?' at line 1 23/10/2015 às 18:11
  • 1
    Veja o EDIT 2... 23/10/2015 às 18:20

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .