This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes JDBC and the fly likes JDBC - Using variable in sql query? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Reply 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: 2350

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
ITIL foundation
aseem agarwal
Greenhorn

Joined: Oct 17, 2009
Posts: 9
ya got it....thanks
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: JDBC - Using variable in sql query?
 
Similar Threads
LIKE operator
submit form on click of any hyperlink
renaming object and creating object in run time
Need Help in JDBC-Ms access
select .... where... query for a variable?