Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a variable into a sql statement in Java

Tags:

java

sql

I am trying to write a sql statement in java that uses a string variable in the where clause. I have tries multiple ways to do this but it keep telling me that I am not using the proper syntax. Can someone please tell me the right way to do this? The variable in this query is par_id.

String sql2 = "SELECT * FROM Tennis1294966077108.container_tbl WHERE parent_id =+"'par_id'"+ORDER BY creation_time asc";

like image 909
Big_A Avatar asked Dec 06 '25 04:12

Big_A


2 Answers

Use a PreparedStatement

PreparedStatement ps = connection.prepareStatement("SELECT * FROM Tennis1294966077108.container_tbl WHERE parent_id = ? ORDER BY creation_time asc");
ps.setObject(1, par_id);
like image 146
objects Avatar answered Dec 08 '25 18:12

objects


String sql2 = "SELECT * FROM Tennis1294966077108.container_tbl WHERE parent_id='"+par_id+"'
ORDER BY creation_time asc";
like image 38
Akki Avatar answered Dec 08 '25 18:12

Akki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!