Solved

PreparedStatements ? after WHERE

Posted on 2002-06-12
4
202 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 100 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

773 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