Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see PreparedStatement's SQL string? [duplicate]

When we create a PreparedStatement we use '?' chars to then be replaced by setted parameters.

How can we see the final SQL string after these parameters are set?

like image 668
Hikari Avatar asked Mar 21 '13 16:03

Hikari


1 Answers

There is no final SQL string, the version with placeholders is what is actually sent to server. The data is sent completely separately from it as you execute queries on the prepared statement.

You can log the string with placeholders, and then each dataset individually.

Your code could combine them in the log to an actual SQL string if that's what you want:

String query = "SELECT a FROM b WHERE c = ?";

...

pstmt.setString(1, "asd");
logSQL( query, "asd");

logSQL would then actually log "SELECT a FROM b WHERE c = 'asd'". Could be that someone has actually implemented this before...

like image 170
Esailija Avatar answered Nov 04 '22 17:11

Esailija