944,142 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 682
  • MySQL RSS
Nov 2nd, 2009
0

SQL statement for easier db a search

Expand Post »
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):
/***********************************************/
Java Syntax (Toggle Plain Text)
  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 ..
sql Syntax (Toggle Plain Text)
  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; Nov 2nd, 2009 at 9:42 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dangari is offline Offline
73 posts
since Aug 2009
Nov 3rd, 2009
0
Re: SQL statement for easier db a search
Hey.

How about something like:
java Syntax (Toggle Plain Text)
  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?
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 3rd, 2009
0
Re: SQL statement for easier db a search
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!!
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dangari is offline Offline
73 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: SET datatype
Next Thread in MySQL Forum Timeline: What does mysql return when the selection doesnt exist?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC