Reputation: 18807
I'm looking for posibility to get result sql (with injected parameter) from prepared statement in java for Oracle implementation.
By MySQL the method toString()
get wanted sql over the method asSql()
from com.mysql.jdbc.PreparedStatement
.
But by Oracle implementation oracle.jdbc.driver.OraclePreparedStatementWrapper
, I can't find the way to get a result sql.
Upvotes: 3
Views: 2545
Reputation: 13858
I believe there's no such thing with the OraclePreparedStatement
.
If you absolutely need that feature, you could wrap some code around the PreparedStatement I guess:
public class LoggingPreparedStatement implements PreparedStatement {
private PreparedStatement delegate;
private String sql;
private Map<Integer,Object> parameter = new TreeMap<Integer,Object>();
public LoggingPreparedStatement(Connection con, String sql) throws SQLException {
this.sql = sql;
this.delegate = con.prepareStatement(sql);
}
@Override
public void setString(int parameterIndex, String x) throws SQLException {
parameter.put(parameterIndex, x);
delegate.setString(parameterIndex, x);
}
//What you asked for
public String getSQL() {
String returnSQL = sql;
//TreeMap returns sorted by key
for(Object o : parameter.values()) {
//Replace first ? with the value
returnSQL = returnSQL.replaceFirst("\\?", o.toString());
}
return returnSQL;
}
//Lots of other methods to implement and delegate to original statement
[...]
}
You would then instead of
PreparedStatement ps = con.prepareStatement(sql);
Need to change to
LoggingPreparedStatement ps = new LoggingPreparedStatement(con, sql);
So overall the process is quite painful.
Upvotes: 1