PreparedStatement select with like 'something%' in Java Swing

3

How to perform LIKE 'c%' search 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);
    }           
}

But it does not return anything, but when I remove the tables and put * it's normal.

    
asked by anonymous 19.06.2015 / 18:20

1 answer

2

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

From

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 set the parameter too:

From

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

To

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

Your last "%'" has a missing single quote, remove it.

    
19.06.2015 / 18:29