I am writing a program that gets records from a database. I would like to create a dynamic query based on 3 variables (studentID, firstName, and/or lastname). Here is my java code which returns records:
result = statement.executeQuery("SELECT * FROM student "
+ "WHERE (studentID = "
+ getStudentId() + " AND " + getStudentId() + " <> 0)"
+ " OR (firstName = '"
+ getFirstName() + "' AND '" + getFirstName() + "' IS NOT NULL)"
+ " OR (lastName = '"
+ getLastName() + "' AND '" + getLastName() + "' IS NOT NULL)");
What I would like is for the search to return results that are vague to specific based on what variables are present. Currently if the studentID is the only field provided, it returns that single record or if studentID is not present but firstName is, it returns all records by firstName (same for lastName if only variable present). What doesn't work is if I provide firstName and lastName, it returns all records with the firstName regardless of the last name of the record, and all records with lastName regardless of what the first name is. Example firstName = "Bill" and lastName = "Jackson":
1 Bill Hader
2 Steve Jackson
3 Bill Jackson
4 Bill Stewart
5 Denise Jackson
6 Wendy Jackson
7 Bill Matthews
What I am trying to figure out is if I provide specific criteria such as firstName and lastName how to get the one specific record that contains both first name and last name that was specified.