3

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

user836026
  • 10,608
  • 15
  • 73
  • 129
  • 2
    http://stackoverflow.com/q/2683214/839527 previous question will help you. – JProgrammer Feb 25 '12 at 16:11
  • 1
    there's a good discussion of this question [here](http://stackoverflow.com/questions/2382532/how-can-i-get-the-sql-of-a-preparedstatement) – mut1na Feb 25 '12 at 16:20

1 Answers1

4

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561