| | |
SQL statement for easier db a search
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 12
Reputation:
Solved Threads: 0
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):
/***********************************************/
/********************************************/
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 .. ....even if one variable is null it considers only the non-empty variables.Thank you so much in advance.
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)
//......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 ..
sql Syntax (Toggle Plain Text)
SELECT * FROM tbooks WHERE SERIAL = ' "+serial+" ' AND tel= ' "+tel+" ' AND gender= ' "+gender+" ' AND datez= ' "+datez+" '
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)
0
#2 21 Days Ago
Hey.
How about something like:
My Java is a bit rusty, but you get the idea.
Is that what you are trying to do?
How about something like:
java Syntax (Toggle Plain Text)
// Create a list object for the conditions, and the query string. List conditions = new List(); String query = "SELECT * FROM tbooks"; // Add each condition to the list if it is present. if(!serial.equals("")) { conditions.add("serial='" + serial +"'"); } if(!tel.equals("")) { conditions.add("tel='" + tel +"'"); } if(!gender.equals("")) { conditions.add("gender='" + gender +"'"); } if(!date.equals("")) { conditions.add("date='" + date +"'"); } // If any conditions were present, compile a WHERE clause. if(conditions.size() > 0) { query += " WHERE "; Iterator i = conditions.iterator(); int cLength = conditions.size(); while(i.hasNext()) { query += i.next().toString(); if(cLength > 1) { // We want to skip the last loop, so > 1 query += " AND "; } --cLength; } } // And you should have your query.
Is that what you are trying to do?
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
![]() |
Similar Threads
- SQL statement help! (Visual Basic 4 / 5 / 6)
- PHP variable in SQL statement (PHP)
- is there something wrong with my SQL statement? (PHP)
- Question about SQL statement (MS SQL)
- SQL statement error (ASP.NET)
- sql statement (Visual Basic 4 / 5 / 6)
- run sql statement in asp (ASP)
Other Threads in the MySQL Forum
- Previous Thread: SET datatype
- Next Thread: What does mysql return when the selection doesnt exist?
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





