try{
String Sql ="Select * from learners_info where lrn LIKE %?% ";
pst=conn.prepareStatement(Sql);
pst.setString(1, txtSearch.getText());
System.out.print(pst);
rs = pst.executeQuery();
DefaultTableModel ts = (DefaultTableModel)tableSearch.getModel();
ts.setRowCount(0);
while (rs.next()){
Object searchTable[] = {
rs.getInt("lrn"),
rs.getString("learner_firstname"),
rs.getString("learner_middlename"),
rs.getString("learner_lastname"),
};
ts.addRow(searchTable);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Invalid info idnumber");
e.printStackTrace();
}
//
finally {
try {
rs.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
pst.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
} catch (Exception e) {
e.printStackTrace();
}
}
The problem is I cant find the proper String to use for me to get the LIKE terms in the SQL from text field
**Using this String **
String Sql ="Select * from learners_info where lrn LIKE %?% ";
I seem to get this error :
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4616)
And if I try this String
String Sql ="Select * from learners_info where lrn LIKE '%'?'%' ";
I printed what kind of string is being used using system.out and it shows this.
com.mysql.jdbc.JDBC4PreparedStatement@414a0b8f: Select * from learners_info where lrn LIKE '%''1411237''%'
this query works but it doesn't seem to return any values
I'm sorry cause I'm still new to database in java.
Remove the '%' parts from your SQL String. Try with this :
String Sql ="Select * from learners_info where lrn LIKE ?";
Then try passing the %s along with your String value in your PreparedStatement setString()
method