?
Solved

PreparedStatements ? after WHERE

Posted on 2002-06-12
4
Medium Priority
?
222 Views
Last Modified: 2010-03-31
Hi,

Can I put the ? marker after the WHERE clause?
Like:
SQL = "SELECT * FROM mytable WHERE ?";
PreparedStatement pstmt = conn.prepareStatement(SQL);
pstmt.setString(1,"ID = 23 AND ODJ = 23 AND ...(etc..)");
ResultSet rs = pstmt.executeQuery();

Thanks
0
Comment
Question by:BabyFace
  • 2
4 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7073680
the ? is interpreted as a value (that's why your above example does not work) it'd have to look something more like this:

SQL = "SELECT * FROM mytable WHERE id = ? and odj = ?";
pstmt.setInt(1,23);
pstmt.setInt(2,23);


0
 
LVL 1

Author Comment

by:BabyFace
ID: 7073761
Hi,
The problem is that I have a variable constaint list.
Sometimes after the WHERE clause has 1 constaint other times it may have 5 or 7 or 13 or etc...
I was wondering if preparedstatments could handle this structure?
Thanks
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 400 total points
ID: 7073798
unfortunately, it doesn't...

as i stated above, the ? expects a value (bind variable)...
take a look at:
http://www.onjava.com/pub/a/onjava/2001/12/19/oraclejdbc.html

for some information on using preparedStatements vs. statements
0
 
LVL 92

Expert Comment

by:objects
ID: 7074285
You can still use a PreparedStatement, you just have to build up your SQL yourself before create your PreparedStatement.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question