1

I am trying to get the string from textfield and search it even if the string is partial.

  public void jTable1(){
            String a1=jTextField1.getText();
            try{
                String sql="select Instrument_ID, Name,Type,Version,Company,Status from Issue where Name like'"+a1+"'";
                String Sql;
                pst=conn.prepareStatement(sql);
                rs=pst.executeQuery();
                jTable1.setModel(DbUtils.resultSetToTableModel(rs));
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, e);


            }
        }

1 Answer 1

1

Try this:

public void jTable1() {
    String a1 = jTextField1.getText();
    try {
        String sql = "select Instrument_ID, Name,Type,Version,Company,Status from Issue where Name like ? ";
        pst = conn.prepareStatement(sql);
        pst.setString(1, "%" + a1 + "%");
        rs = pst.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

Use the parameterized SQL and set the string after suffixing and prefixing the % to it.

Sign up to request clarification or add additional context in comments.

3 Comments

@Raju Didnt work how? Does it give you wrong result? no result? error?
error:: method setString in Interface Prepred Statement cannot be applied to given types. required: int, string found: string
well i'm still confused

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.