Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print used SQL query when using PreparedStatement (JDBC)

Tags:

java

mysql

jdbc

I used PreparedStatement in my Java program. I need to debug the SQL query because it's not working fine.

Is there is away to print the used SQL statement with the values inserted

for example in PreparedStatement:

select * from table where a=?

and than I set the ?

could I print the used SQL, for example:

select * from table where a=1

like image 761
user836026 Avatar asked Oct 12 '25 10:10

user836026


1 Answers

It can't be done with java.sql.PreparedStatement interface itself; it depends on the implementation by your database vendor.

But you're in luck; the MySQL driver allows you to do it using its toString implementation:

http://www.avajava.com/tutorials/lessons/how-do-i-display-a-prepared-statement-with-bind-variables-using-mysql.html

You need to be aware that using this vendor-specific feature binds your code to MySQL. You can't change databases without changing your code.

like image 51
duffymo Avatar answered Oct 15 '25 01:10

duffymo