Results 1 to 6 of 6
  1. #1
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Extracting the query from PreparedStatement

    Can you anyone suggest me a method to get the query from the PreparedStatement?
    Please dont suggest me toString method because it prints the query along with some com...... and then followed by the query, is there any way to get the query alone?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Extracting the query from PreparedStatement

    Not as standard.
    toString() is entirely dependent on what the driver writer has done for it, as you've seen.
    For example I think one of the Postgres drivers would provide the query with the set parameters replaced (badly, ie no quotes for string parameters) in an attempt to show the sort of query that would be run.

    You'll need to look a the specific driver, even then it's not all that likely. After all, most JDBC is run with the caller knowing what the SQL is, or having access to it, prior to the creation of the PreparedStatement.

  3. #3
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Extracting the query from PreparedStatement

    i did this way
    Got the String from PreparedStatement using toString,

    And then the query was like this \com.mysql.jdbc.PreparedStatement@2f9dc6f7: select date,temp from `weather` where date='2012-02-13' AND place='chennai'
    So to get the query part alone i used the split function in the String class as below

    String delimiter = "\\:"
    String[] temp = ps.split(delimiter);
    So the temp[1] now holds the query i need .

    Note ps is obtained by converting the PreparedStatement to String using toString :-)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Extracting the query from PreparedStatement

    So long as you stick to MySQL...:)

    ...and possibly that version of the driver...

  5. #5
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Extracting the query from PreparedStatement

    Yeah i do aggree.. Else what shall i do ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Extracting the query from PreparedStatement

    As I said earlier on the other thread, I'd look to get the SQL string before creating the PreparedStatement.
    You can then get the actual bind values from the PreparedStatement later on and associate the two things.

    Indeed you could do this by wrapping some bits of JDBC (Connection and PreparedStatement come to mind) in your own version:
    Java Code:
    public Connection getConnectio() {
       // This would get hold of a connection object as you are now, except it would wrap it in your own version -
       return new YourConnection(connection);
    }
    YourConnection would simply pass all calls down to the underlying connection object:
    Java Code:
    public class YourConnection implements Connection {
        private Connection realConnection;
        public YourConnection(Connection realConnection) {
            this.realConnection = realConnection;
        }
        // dummy implementations of everything here, which just call the realCOnnection methods except -
        public PreparedStatement prepareStatement(String sql) {
            return YourPreparedStatement(realConnection.prepareStatement(sql), sql);
        }
    // And any of the other prepareStatement() methods you might use.
    }
    And then YourPreparedStatement would do a similar thing, except it would store the sql String as well and provide a way to get it.
    You could even add a method that returns the SQL with the bind variables in place.
    Indeed you could stick whatever PreparedStatement analysis stuff you wanted in there.

Similar Threads

  1. preparedStatement
    By axenos in forum JDBC
    Replies: 10
    Last Post: 03-31-2011, 12:00 PM
  2. Regarding PreparedStatement
    By adeeb in forum JDBC
    Replies: 0
    Last Post: 06-09-2008, 10:07 PM
  3. Using PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:24 PM
  4. PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 04:56 PM
  5. Help me. PreparedStatement
    By Felissa in forum JDBC
    Replies: 2
    Last Post: 06-28-2007, 06:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •