• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC - Using variable in sql query?

 
aseem agarwal
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
aseem agarwal
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya got it....thanks
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic