Link to home
Start Free TrialLog in
Avatar of BabyFace
BabyFace

asked on

PreparedStatements ? after WHERE

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
Avatar of bobbit31
bobbit31
Flag of United States of America image

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);


Avatar of BabyFace
BabyFace

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
You can still use a PreparedStatement, you just have to build up your SQL yourself before create your PreparedStatement.