简体   繁体   中英

Printing the preparedStatement SQL after the bind for Oracle jdbc driver

I have tried several examples , but none of them seem to work. Here is the code I tried last

 import oracle.jdbc.driver.OracleDriver;
 PreparedStatement prs = null;
 ResultSet rrs = null;
Class stmt1 = null;
java.lang.reflect.Field mem = null;

  requestSQL = "Select FIPS_STATE_CD_TXT, FIPS_COUNTY_CD_TXT from MSTR_FIPS_COUNTY where STATE_ID = ? " + " and COUNTY_TXT = ?";

    prs.setString(1, vPropertyState);
    prs.setString(2, vPropertyCounty);
    System.out.println(prs.toString()); //JRN
             Class stmt1 = prs.getClass();  
          java.lang.reflect.Field mem = stmt1.getField("sql");  
          String value= (String)mem.get(prs);  
          rrs = prs.executeQuery();

I get an error on this at :

   Exception trying to make a TAF call
   java.lang.NoSuchFieldException: sql
    at java.lang.Class.getField(Class.java:1520)

I even tried using this example from JavaWorld, but my compiler doesn't seem to recognize DebugLevel and StatementFactory. Is there a special package I should download for this? http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html?page=3

I am using Java 1.6 and Oracle 11g. I am also looking for a quick fix, rather than installing log4jdbc or p6sy

Different drivers use different names. In your case the sql field you are trying to access is not one of the available ones for that driver.

To get all the names of your JDBC driver use this code:

Class stmt1 = prepStmt.getClass(); 
try {
    java.lang.reflect.Field mem[] = stmt1.getDeclaredFields();  
    for (Field x:mem){
        System.out.println("Field:"+x.getName());
    }    
}  catch (SecurityException ex) {

}

Observe the field name, and then use your code above to print its value.

The answer is, you can't. Atleast not for the Oracle jdbc driver.

What you are trying to achieve is not possible for oracle jdbc driver. But in general some drivers ( mysql ) supports this

prs.toString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM