Printing a prepared SQL query after binding for Oracle jdbc driver

I tried a few examples, but none of them seem to work. Here is the code I tried in the past

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 message:

  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 does not seem to recognize DebugLevel and StatementFactory. Is there a special package that I have to 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, not installing log4jdbc or p6sy

+4
source share
3 answers

Answer: you cannot. At least not for the Oracle jdbc driver.

+1
source

Different drivers use different names. In your case, the sql field you are trying to get is not one of the available for this 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 to print its value.

+4
source

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

prs.toString()

0
source

Source: https://habr.com/ru/post/1501481/


All Articles