I have four text fields for FirstName, LastName, ID, and department. I let the user search for any student record by entering data in the text field. When the user hits submit button, I display a table which corresponds to the student record(s) he requested. So, for example, if a user enters computerScience for department text field, I should display all students in Computer Science department.
So, my SQL select statement should be able to do that at run-time.
I am using a prepared statement like this:
PreparedStatement pre = conn.prepareStatement("select ID,FirstName,LastName,Dept from student where ID = ? or FirstName = ? or lastName = ? or Dept = ?");
This however displays the result only when an ID is entered. But lets say if someone just types in “Mark” for last name and leaves every other text field blank, then this query does not display anything although there are students whose last name is Mark in the database.
Can anyone guide me here please?
Use a dynamic query string as below (assuming minimum one criteria is prsent)
Hope this helps!