Big_A
Big_A

Reputation: 21

Adding a variable into a sql statement in Java

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";

Upvotes: 2

Views: 23339

Answers (4)

Akki
Akki

Reputation: 1251

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

Upvotes: 1

developer
developer

Reputation: 9478

PreparedStatement ps = connection.prepareStatement(
    "SELECT * FROM Tennis1294966077108.container_tbl " +
    "WHERE parent_id = ? ORDER BY creation_time asc");
ps.setInt(1, par_id); 

Upvotes: 0

objects
objects

Reputation: 8677

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);

Upvotes: 11

James Anderson
James Anderson

Reputation: 27478

Try:

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

Upvotes: -1

Related Questions