wood burning stoves 2.0*
The moose likes JDBC and the fly likes JDBC - Using variable in sql query? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC - Using variable in sql query?" Watch "JDBC - Using variable in sql query?" New topic
Author

JDBC - Using variable in sql query?

aseem agarwal
Greenhorn

Joined: Oct 17, 2009
Posts: 9
I want to execute something like this...

String s=new String("aa");

query="select * from users where id="+s;

Basically i want to put a variable in where condition and the above statement is not working...Can anyone help me with this....
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

You can use a PreparedStatement.
See Java JDBC Tutorial
You put a '?' in your sql statement where the variable has to be places.
You later on bind the variable by calling a PreparedStatement.setXXX() method.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
aseem agarwal
Greenhorn

Joined: Oct 17, 2009
Posts: 9
ya got it....thanks
 
 
subject: JDBC - Using variable in sql query?
 
Similar Threads
select .... where... query for a variable?
renaming object and creating object in run time
submit form on click of any hyperlink
Need Help in JDBC-Ms access
LIKE operator