This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatment - Search Page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatment - Search Page" Watch "PreparedStatment - Search Page" New topic

PreparedStatment - Search Page

arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32

I have a search page with some text boxes (empno, ename, designation, deptno) where the user can enter some values to search. How to create a prepared statement for this search?

[edited to add line break]
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30050

You would need to use some if statements to append just the entered fields in the query.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ryan Webb

Joined: Dec 09, 2009
Posts: 28
try this.

hope this helps!
Ryan Webb

frustrated to learn c/c++/objective-c
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30050

That won't work. SQL requires "and" rather than "," to separate commas.

The reason I didn't suggest this is it implies all of the fields were entered. If any are optional, Arunkumar needs to write logic to concatenate the relevant parts of the where clause.
It is sorta covered in the JavaRanch Style Guide.
subject: PreparedStatment - Search Page
Similar Threads
running the result from the end
Order by query..
can a batch have multiple prepared statements
Urgent: Setting values for the bind parameters of a RowSet query
Problem with ContainsKey(obj key) in HashMap