Solved

PreparedStatements ? after WHERE

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
factorial example 4 50
SHA2 certs for IIS AND Java? 2 113
Java exception bubble up 2 21
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

820 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