This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 2497
    
    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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC - Using variable in sql query?