SQL statement for easier db a search

Thread Solved

Join Date: Aug 2009
Posts: 12
Reputation: dangari is an unknown quantity at this point 
Solved Threads: 0
dangari dangari is offline Offline
Newbie Poster

SQL statement for easier db a search

 
0
  #1
22 Days Ago
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):
/***********************************************/
  1. //......after connecting to db and all that
  2.  
  3.  
  4. if(serial.equals("") )
  5. {
  6. if(!tel.equals("") )//
  7. {
  8. query = "SELECT * FROM tbooks WHERE tbooks.tel=' "+tel+" ' ";
  9. }
  10. else if(gender.equals(""))
  11. {
  12. query = "SELECT * FROM tbooks WHERE tbooks.gender=' "+gender+" ' ";
  13. }
  14. }//end if
  15. else if (!serial.equals(""))
  16. {
  17. query = "SELECT * FROM tbooks WHERE tbooks.serial=' "+serial+" ' ";
  18. }
  19.  
  20. //query is the statement to be executed later as
  21. //ResultSet rs = statement.executeQuery(query);
  22. // 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 ..
  1. 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.
Last edited by peter_budo; 22 Days Ago at 9:42 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 431
Reputation: Atli is on a distinguished road 
Solved Threads: 55
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #2
21 Days Ago
Hey.

How about something like:
  1. // Create a list object for the conditions, and the query string.
  2. List conditions = new List();
  3. String query = "SELECT * FROM tbooks";
  4.  
  5. // Add each condition to the list if it is present.
  6. if(!serial.equals("")) {
  7. conditions.add("serial='" + serial +"'");
  8. }
  9. if(!tel.equals("")) {
  10. conditions.add("tel='" + tel +"'");
  11. }
  12. if(!gender.equals("")) {
  13. conditions.add("gender='" + gender +"'");
  14. }
  15. if(!date.equals("")) {
  16. conditions.add("date='" + date +"'");
  17. }
  18.  
  19. // If any conditions were present, compile a WHERE clause.
  20. if(conditions.size() > 0) {
  21. query += " WHERE ";
  22. Iterator i = conditions.iterator();
  23. int cLength = conditions.size();
  24. while(i.hasNext())
  25. {
  26. query += i.next().toString();
  27. if(cLength > 1) { // We want to skip the last loop, so > 1
  28. query += " AND ";
  29. }
  30. --cLength;
  31. }
  32. }
  33.  
  34. // And you should have your query.
My Java is a bit rusty, but you get the idea.

Is that what you are trying to do?
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: dangari is an unknown quantity at this point 
Solved Threads: 0
dangari dangari is offline Offline
Newbie Poster
 
0
  #3
21 Days Ago
Atli!You are the kind of people that I personally refer to as programming genious.This has to be the best idea that I have seen since I joined Daniweb.Fantastic stuff!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC