4

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

2
  • 2
    stackoverflow.com/q/2683214/839527 previous question will help you. Commented Feb 25, 2012 at 16:11
  • 1
    there's a good discussion of this question here Commented Feb 25, 2012 at 16:20

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ... it's OK i'm just going to use it for debug purpose only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.