views:

45

answers:

3

Is there any way to get the Oracle query from PreparedStatement .I know its not possible as such I can't use log4jdbc or p6spy as it is a secured application and using this will create bigger problems..toString won't work as I am using Oracle? I can't change PreparedStatement to Statement either.

+2  A: 

If only need for debug time then You can use DebuggableStatement follow this article

pinichi
yes, but that would require him to change all the places where the statements are prepared. I am assuming that he cannot do that (why not, by the way?).
Thilo
No I can do that :) Thanks anyway
Harish
+1  A: 

I don't think you should be doing it this way, as there is no officially documented API for this.

If you can mess with the code, why cannot you use log4jdbc ? Oracle JDBC also supports java.util.logging, which you could try to enable.

If you are just interested in the SQL itself, you can turn on session tracing on the Oracle server.

Or maybe you can put your code to where the statement is being prepared (using something like @pinichi is suggesting)?

But just for fun, poking around with the debugger, with my version of Oracle JDBC, I can do

  if (stmt instanceof oracle.jdbc.driver.OraclePreparedStatement) {
        String x = ((oracle.jdbc.driver.OraclePreparedStatement) stmt)
             .getOriginalSql();
        System.out.println(x);
  }
Thilo
+1  A: 

If you just want to check SQL statement you can also go straight to the database and check v$sql table.

There you can find all sqls and other information about query. More info: http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/dynviews_2113.htm

Lukasz Stelmach