Preparedstatement select with like 'something%' in Java Swing

Asked

Viewed 263 times

3

how to perform the survey LIKE 'c%' with PreparedStatement?
I have the following code:

PreparedStatement pstm;
ResultSet rs;

 public void pesquisarAdministrador(){
    String sql = "SELECT administrador.idadministrador, administrador.usr_adm FROM administrador WHERE usr_adm LIKE ?";
    try {
        pstm = conn.prepareStatement(sql);
        pstm.setString(1,"%" + txtId.getText() + "%'");
        rs = pstm.executeQuery();
        tblAdministrador.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (SQLException error) {
        JOptionPane.showMessageDialog(null, error);
    }           
}

however it does not return anything, but when I remove the tables and put * he brings normal.

1 answer

2


According to your other question, I still think your problem is in the like + case sensitive, so I would do something like this:

Of

String sql = "SELECT administrador.idadministrador, administrador.usr_adm FROM administrador WHERE usr_adm LIKE ?";

To

String sql = "SELECT administrador.idadministrador, administrador.usr_adm FROM administrador WHERE LOWER(usr_adm) LIKE ?";

And tidy the parameter too:

Of

pstm.setString(1,"%" + txtId.getText() + "%'");

To

pstm.setString(1,"%" + txtId.getText().toString().toLowerCase() + "%");

His last "%'" has a simple missing quotes, remove it.

  • 1

    My wobble was not txtId.getText() and yes txtbuscar.getText()

  • @Hebertdelima hahaha, is part :)

Browser other questions tagged

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