Short: My problem is writing a method that generates a prepared statement coresponding to the number of checked checkboxes and filling the "?".
Hey, I got a text search searching for corresponding titles and authors that looks like this.
String query = "SELECT * FROM xyz WHERE title LIKE ? OR author LIKE ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, "%" + searchtext + "%");
ps.setString(2, "%" + searchtext + "%");
Works just fine. I now want to implement a checkbox based search. Every database entry has a genre, topic and use case. Each of that 3 has 8-9 possibilities.
As a result i want to show all database entries whose genre is one of the checked ones, whose topic is one of the checked ones and whose use case is one of the checked ones.
What i want might be for example (genre1 or genre2) and topic7 and (usecase5 or usecase9)
As only checked checkboxes return a value to the servlet I dont't know how to write a prepared statement that fits all possible scenarios. I believe I need to count how many checkboxes return a value, get their values and dynamically generate a prepared statement with as many "?" as the number of checked checkboxes. Each ? has to be filled with ps.setString().
Would you group the checkboxes in the same group by using the same name or would you leave every checkbox alone?