I am trying to construct an sql string within a servlet that will execute based on what the user inputs. Thanks to Bryce, I know where in the servlet to put it, and I can do something like "SELECT thing1 FROM thing2 WHERE thing1=7...etc." However, I need the wherestring to depend on certain conditions. I am displaying an html page initially with four input fields. If the user leaves one of them out, then that field does not need to be included in the where clause. I have done this in SQL free format but never in java. Here is the basic setup that I'm guessing at:
I could be completely wrong, but this is my guess. Do you guys have any advice?
Code:
String selectString = "SELECT ...";
String fromString = "FROM...";
String whereString = "WHERE ...";
if userInputNameField1 <> null
{
sql.append(" and ucase(NAME) Like ucase (userInputField1) ");
//I guess this is how you would add it to the where clause
}
if userInputNameField2 <> null
{
sql.append//same here
} //for all four fields
String sqlString = selectString + fromString + whereString + orderByString;





Comment