Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Specific search for selected JComboBox value in database

I have a JComboBox, I want to find data from database using a combo box filter the search. But this code does not work.

String txt = cmbItem.getSelectedItem().toString();
Connection cn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
    cn = cfg.getCon();
    String qr = "SELECT ProductName FROM product WHERE ProductName LIKE '%" + txt + "%' limit 5";
    ps = cn.prepareStatement(qr);
    rs = ps.executeQuery();
    while (rs.next()) {
        String name = rs.getString("ProductName");
    }
    cmbItem.addItem(rs.getString("ProductName"));
} catch (SQLException e) {
} finally {
    cfg.close_connection(cn, ps, rs);
}

Comments