Display a PreparedStatement content

M

Max

Hello,

I am using JDBC driver

I would like to know if there is a way to display the SQL query associated
with a
PreparedStatement.

I would like to fill the parameters, and then display the query as it will
be thrown
to the Websphere server to debug my application.

Sample: PreparedStatement ps
= new connection.prepareStatement("Select * from foo where bar = ?") ;
ps.setInt(1, 1) ;

and then use somethind like:

// This line does not actually work. System.out.println("Query is: "
+ps.toString()) ;

// But i would like it to print Query is: Select * from foo where bar = 1

Thanks
 
R

Robert Olofsson

Max ([email protected]) wrote:
: I would like to know if there is a way to display the SQL query associated
: with a
: PreparedStatement.

This was discussed a week ago, under a thread named "print out PreparedStatement"
I gave a brief outline of the logging system I use to achieve this...

/robo
 
R

Robert Olofsson

Max ([email protected]) wrote:
: doesn't help much ...
: > try google groups, a search for "print out PreparedStatement" gives me

What part of it is it that you do not understand?
I think this is somewhere between easy and trivial to build (and I
have done it so I so know). It takes some time to do this since the
interfaces are big, but it is easy.

Building a proxy for connection and preparedstatement goes something
like this:

public class LogConnection implements Connection {
private Connection con;

public LogConnection (Connection con) {
this.con = con;
}

public PreparedStatement prepareStatement (String sql) {
PreparedStatement ps = con.prepareStatement (con);
return new LogPreparedStatement (sql, ps);
}
.....
}

public class LogPreparedStatement implements PreparedStatement {
String sql;
List params;

public LogPreparedStatement (String sql,
PreparedStatement ps) {
this.sql = sql;
this.ps = ps;
}

public void setObject (int idx, Object o) {
// ensure size first....
params.set (idx, o);
}

public void executeUpdate () {
SQLLogger.log (sql, params);
ps.executeUpdate ();
}
.....
}

Fill in the blanks and correct my code (I have not compiled this code
so it probably has lots of errors, but hopefully you get the idea).
Do this for Statement, PreparedStatement, CallableStatement and
build a nice logger, SQLLogger and you have a nice logging for your
SQL.

Now, what more info do you need?

Have fun.
/robo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top