Say i have a Table
+----------------+-----------------------+---------------------+
| colID | colTitle | colKeywords |
+----------------+-----------------------+---------------------+
| 1 | Jumanji | comedy adventure |
| 2 | Moana | adventure animation |
| 3 | Shawshank Redemption | drama tearjerker |
| 4 | Avengers | action |
+----------------+-----------------------+---------------------+
+-----------------------------+ +---------+
Search: | adventure and action movies | |button GO|
+-----------------------------+ +---------+
What I want to do is if i type "adventure and action movies" in the textfield, and after I hit the button go, the result in the tableview should be:
Jumanji
Moana
Avengers
I want to achieve this using the Like clause. so far, been trying a lot of examples and asked questions, and nothing seem to work.
the code i am using as of writing:
@FXML
private void goSearch(ActionEvent event) throws IOException, SQLException{
String sql = "SELECT * FROM table_entry WHERE colKeywords LIKE ? ";
conn = SqlConnection.ConnectDB();
pst=conn.prepareStatement(sql);
String criteria = txt_search.getText();
if (criteria.trim().length() == 0) { return; }
List<String> results = new ArrayList<>();
String[] arryCriterion = criteria.split(" ");
for (int i = 0; i < arryCriterion.length; i++) {
String criterion = "%" + arryCriterion[i] + "%";
pst.setString(1, criterion);
rs = pst.executeQuery();
while (rs.next()) {
results.add(rs.getString("colKeywords"));
}
}
table_title.setItems(results);
}
edit for sedrick:
String word = txt_search.getText();
String sql = "SELECT * FROM table WHERE colKeywords LIKE '%word%'";
Am i doing this right? What if there are mutiple words in the textfield?