get the query created by PreparedStatement

Поиск
Список
Период
Сортировка
Искать

get the query created by PreparedStatement

От:
Eric Frazier <ef@kwinternet.com>
Дата:
Hi,

Is there an easy way to get the actual query sent by a preparedstatement
without contstructing it yourself?

I have a long case statment with many setXXX and the whole of my query is
basicly just

INSERT INTO TABLE(?,?,?,?,?,?,?,?,?) 

along with those setXXX statments. 

I would like to be able to print out to a log what the resulting SQL ends up
being. But I don't see any way to do that. Isn't there some part of the JDBC
API that will give you the last query executed or something like that? 


Thanks,

Eric 


Frazier Consulting
http://www.kwinternet.com/eric
(250) 655 - 9513



Re: get the query created by PreparedStatement

От:
Garry Thuna <garry.thuna-pgsql-jdbc@tacticalExecutive.com>
Дата:
Eric,

I have always wanted the same thing for both debugging as well as logging.
The best I have been able to come up with is a wrapper class to
PreparedStatement.  This works well with the exception that some of the
methods of PreparedStatement are depreciated and you will always get a
compiler warning about them (but you have to implement them because your
implementing the PreparedStatement interface).

All you need do is put the code in a package of your choosing and implement
the static StringUtil function that simply counts the '?'s in the query.  A
simple statement.toString() should give you useful output.

Hope this helps.



package com.thuna.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Ref;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Array;
import java.sql.ResultSetMetaData;
import java.util.Calendar;
import java.sql.SQLWarning;
import java.sql.Connection;
import java.sql.SQLException;
import com.thuna.misc.StringUtil;

/**
  * a wrapper class for prepared statments to allow them to be logged
  */
public class PreparedStatementLogable
        implements PreparedStatement
{
    PreparedStatement stmt;
    String sql;
    String[] parm;

    public PreparedStatementLogable(Connection con, String sql)
            throws SQLException
    {
        this.stmt = con.prepareStatement(sql);
        this.sql = sql;
        parm = new String[StringUtil.countOccurances(sql, "?")];
    }

    public String toString() {
        StringBuffer buff = new StringBuffer(sql);
        for (int i=0; i Hi,
>
> Is there an easy way to get the actual query sent by a preparedstatement
> without contstructing it yourself?
>
> I have a long case statment with many setXXX and the whole of my query is
> basicly just
>
> INSERT INTO TABLE(?,?,?,?,?,?,?,?,?)
>
> along with those setXXX statments.
>
> I would like to be able to print out to a log what the resulting SQL ends
> up being. But I don't see any way to do that. Isn't there some part of the
> JDBC API that will give you the last query executed or something like that?
>
>
> Thanks,
>
> Eric
>
>
> Frazier Consulting
> http://www.kwinternet.com/eric
> (250) 655 - 9513
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl

========================================
Garry Thuna
Tactical Executive Systems
garry.thuna@tacticalExecutive.com

Re: get the query created by PreparedStatement

От:
Peter T Mount <peter@retep.org.uk>
Дата:
Quoting Garry Thuna :

> Eric,
> 
> I have always wanted the same thing for both debugging as well as
> logging.

How about the toString() method ;-)

Ok, may not be standard JDBC, but:

PreparedStatement ps = ....;

// later
org.postgresql.jdbc2.PreparedStatement ps2 =
      (org.postgresql.jdbc2.PreparedStatement) ps;
System.out.println(ps2.toString());

Peter

-- 
Peter Mount peter@retep.org.uk
PostgreSQL JDBC Driver: http://www.retep.org.uk/postgres/
RetepPDF PDF library for Java: http://www.retep.org.uk/pdf/

Re: get the query created by PreparedStatement

От:
Eric Frazier <ef@kwinternet.com>
Дата:
Hi,

This does work, both with the postgress and mysql drivers. 
But I do need to make SURE it will work no matter the driver, so it sounds
like Garry's code would be the best bet for that. 

Thoughts?

Thanks,

Eric 

>How about the toString() method ;-)
>
>Ok, may not be standard JDBC, but:
>
>PreparedStatement ps = ....;
>
>// later
>org.postgresql.jdbc2.PreparedStatement ps2 =
>      (org.postgresql.jdbc2.PreparedStatement) ps;
>System.out.println(ps2.toString());
>
>Peter
>
>-- 
>Peter Mount peter@retep.org.uk
>PostgreSQL JDBC Driver: http://www.retep.org.uk/postgres/
>RetepPDF PDF library for Java: http://www.retep.org.uk/pdf/
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Don't 'kill -9' the postmaster
>


Frazier Consulting
http://www.kwinternet.com/eric
(250) 655 - 9513



Re: get the query created by PreparedStatement

От:
Garry Thuna <garry.thuna@tacticalExecutive.com>
Дата:
Eric,

I have always wanted the same thing for both debugging as well as logging.  
The best I have been able to come up with is a wrapper class to  
PreparedStatement.  This works well with the exception that some of the 
methods of PreparedStatement are depreciated and you will always get a 
compiler warning about them (but you have to implement them because your 
implementing the PreparedStatement interface).

All you need do is put the code in a package of your choosing and implement
the static StringUtil function that simply counts the '?'s in the query.  A 
simple statement.toString() should give you useful output.

Hope this helps.



package com.thuna.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Ref;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Array;
import java.sql.ResultSetMetaData;
import java.util.Calendar;
import java.sql.SQLWarning;
import java.sql.Connection;
import java.sql.SQLException;
import com.thuna.misc.StringUtil;

/**
  * a wrapper class for prepared statments to allow them to be logged
  */
public class PreparedStatementLogable
        implements PreparedStatement
{
    PreparedStatement stmt;
    String sql;
    String[] parm;

    public PreparedStatementLogable(Connection con, String sql)
            throws SQLException
    {
        this.stmt = con.prepareStatement(sql);
        this.sql = sql;
        parm = new String[StringUtil.countOccurances(sql, "?")];
    }

    public String toString() {
        StringBuffer buff = new StringBuffer(sql);
        for (int i=0; i Hi,
>
> Is there an easy way to get the actual query sent by a preparedstatement
> without contstructing it yourself?
>
> I have a long case statment with many setXXX and the whole of my query is
> basicly just
>
> INSERT INTO TABLE(?,?,?,?,?,?,?,?,?)
>
> along with those setXXX statments.
>
> I would like to be able to print out to a log what the resulting SQL ends
> up being. But I don't see any way to do that. Isn't there some part of the
> JDBC API that will give you the last query executed or something like that?
>
>
> Thanks,
>
> Eric
>
>
> Frazier Consulting
> http://www.kwinternet.com/eric
> (250) 655 - 9513
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl

 
========================================
Garry Thuna
Tactical Executive Systems
garry.thuna@tacticalExecutive.com

FAQ