Hi.
I have a web application whereby I perform a search through 4 fields, namely:book serial no.,tel no.,gender and publishing date.In my jsp page variables picking the values respectively are:serial,tel,gender and datez.These are submitted to the java class.Currently, I perform my searches as follows(in java class):
/***********************************************/
//......after connecting to db and all that
if(serial.equals("") )
{
if(!tel.equals("") )//
{
query = "SELECT * FROM tbooks WHERE tbooks.tel=' "+tel+" ' ";
}
else if(gender.equals(""))
{
query = "SELECT * FROM tbooks WHERE tbooks.gender=' "+gender+" ' ";
}
}//end if
else if (!serial.equals(""))
{
query = "SELECT * FROM tbooks WHERE tbooks.serial=' "+serial+" ' ";
}
//query is the statement to be executed later as
//ResultSet rs = statement.executeQuery(query);
// more code goes here.......
/********************************************/
The main question here is, Is there an SQL statement that works well in place of my nested if statements? (i.e if serial no is present ,tel is absent, gender is present and date is absent, then it brings results considering the present values only)....Kind of bringing results of ..
SELECT * FROM tbooks WHERE serial = ' "+serial+" ' AND tel= ' "+tel+" ' AND gender= ' "+gender+" ' AND datez= ' "+datez+" '
....even if one variable is null it considers only the non-empty variables.Thank you so much in advance.