The percent sign (%
) is definitely the right wildcard character to use in this case. For test data
ID Description
-- -----------------------------------
1 I like apple pie.
2 This is a test.
3 Apple makes iThings.
4 This is another test.
5 Name of a tropical fruit: pineapple
the code
String tableName = "SSPWO";
String desc = "[Description]";
String searchText = "apple";
PreparedStatement ps = con.prepareStatement(
String.format("SELECT * FROM %s WHERE %s LIKE ?", tableName, desc)
);
ps.setString(1, "%" + searchText + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(String.format("%d: %s", rs.getInt(1),rs.getString(2)));
}
produces the following console output
1: I like apple pie.
3: Apple makes iThings.
5: Name of a tropical fruit: pineapple