5

Possible Duplicate:
How can I get the SQL of a PreparedStatement?

I make a PreparedStatement (PS) and run some setString(int a,String b) on it. After this, how do I see what my PS finally looks like ?

Community
  • 1
  • 1
sweet dreams
  • 2,094
  • 13
  • 32
  • 46
  • 2
    Possible dupicate of http://stackoverflow.com/questions/2382532/how-can-i-get-the-sql-of-a-preparedstatement – Reimeus Jul 26 '12 at 21:53
  • It depends on the driver. See http://stackoverflow.com/questions/2683214/get-query-from-java-sql-preparedstatement – betomontejo Jul 26 '12 at 22:09
  • @Betoverse ah yes, of course. After years of working with Oracle, sometimes I forget some things are specific to this vendor and mistake them for standards. Thanks for input. – pafau k. Jul 27 '12 at 16:19

1 Answers1

4

If I understand you, you'd want someting like:

    PreparedStatement pstmt = 
           connection.prepareStatement("insert into table tab(col) values(?)");
    pstmt.setString(1, "my string value");

and then you want to see a string like:

"insert into table(col) values('my string value')"

Did I get your meaning right?

If so, I think you can't do that. I think the statement is precompiled without parameter values, and these are kept separately, so you can reuse the statement with different parameters.

What you can do, is to retrieve parameter metadata, like this:

    ParameterMetaData pmd = pstmt.getParameterMetaData();

And then call stuff like:

    pmd.getParameterCount();
    pmd.getParameterClassName(1);
    pmd.getParameterType(1);
pafau k.
  • 1,667
  • 12
  • 20
  • Yes, it seems that I can't do this. In the JavaDocs for ParameterMetaData & ParameterMetaData, i don't see any methods that could give me what i want. – sweet dreams Jul 28 '12 at 07:17
  • What database are you using? Oracle, Derby, MySql, ... ? Try the answer Betoverse provided an try just printing the statement via `System.out.println(pstmt);` – pafau k. Jul 28 '12 at 10:06