Domanda

    String val = searchText.getText();

    System.out.println(val);
    System.out.println(tmp);
    if (tmp == "properShippingName") {
        try {

            String sql = "SELECT SDS_NUMBER, PRODUCT_NAME, PROPER_SHIPPING_NAME FROM APP.MASTER WHERE PROPER_SHIPPING_NAME LIKE ?";
            pst = conn.prepareStatement(sql);

            pst.setString(1, val);
            rs = pst.executeQuery();
            // System.out.println("rs");

            jTable1.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (Exception e) {
            e.printStackTrace(System.out);
            JOptionPane.showMessageDialog(null, e);
        }

My first question is I want to run the query using like %val%. I want the query to return anything close to what the user inputs into the "searchText" text field. I can not seem to figure out how to get it to recognize the variable and still execute the LIKE constraint.

Any help would be greatly appreciated!

I do have a second question on the same topic but slightly different problem so I will post another thread.

È stato utile?

Soluzione

You have to include the percent in your setString method call.

pst.setString(1, "%" + val + "%");

Now the like - statement works with this wildcards.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top