File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 2476
    
    7

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
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
select .... where... query for a variable?
Need Help in JDBC-Ms access