Solved

PreparedStatements ? after WHERE

Posted on 2002-06-12
4
194 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You can still use a PreparedStatement, you just have to build up your SQL yourself before create your PreparedStatement.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
firstChar challenge 13 83
Systems talking to each other 5 107
ArrayIndexOutOfBoundException 9 30
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…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now