Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6581357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:08:32+00:00 2026-05-25T16:08:32+00:00

How can I get SQL query that was received by MySQL server when it

  • 0

How can I get SQL query that was received
by MySQL server when it raises exception?

I have a code similar to the following:

Connection con = null;
PreparedStatement ps = null;
try {
    con = DbConnectionManager.getConnection();
    ps = con.prepareStatement(query);
    setStatementParameters(ps, params);
    ps.executeUpdate();
} catch (SQLExeption e) {
    // How to get wrong query here?
} finally {
    DbConnectionManager.closeConnection(ps, con);
}

Where query variable is like “INSERT into someTable (qwe, asd) VALUES (?, ?)”
The question is how can I get query string in the catch block?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:08:33+00:00Added an answer on May 25, 2026 at 4:08 pm

    I’ve run across another solution to this problem.

    The MySQL JDBC driver overrides the toString of PreparedStatement to display the query as it is sent to the database. This is implementation dependent so it may not the best thing to rely on, but it’s very simple to get at. I’m now using this to dump query text to a log file for debugging purposes. While there are probably other solutions that are more portable and future-proof, this has the advantage of getting exactly the string that the MySQL driver says it’s sending to the database.

    The string comes back with an object ID, then a colon, then the SQL string. You can split it on the colon to get just the SQL.

    The type com.mysql.jdbc.PreparedStatement also exposes a protected method call asSql(). You could override the class with your own implementation that gives public access to this method. From looking at the disassembly of the class’s toString() method, it seems to be using asSql() to get the actual SQL string. This approach adds the problem of how to instantiate your subclass, though; the simplest approach is just to use the toString that you already have access to, without even having to downcast your PreparedStatement to a MySQL-specific subtype.

    Again, just be aware that the maintainers of the MySQL API probably don’t consider this part of the public interface to their software (JDBC defines the standard interface), so they may make changes later that would break this mechanism. For the time being, though, it will get the job done.

    This is true for the version of the MySQL driver I’m currently using, which is 5.1.7.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get a SQL dump of a SQL Server 2008 database? That
I need to get a SQL Query that I can execute to extract certain
Question: I can get the SQL Server database language by querying: SELECT @@language And
I'm using SQL Server 2005 and would like to know how I can get
Where can I get the 64 bit bootstrapper for SQL Server Express 2005 64
How can I get the list of available databases on a SQL Server instance?
I have a query that works on MySQL but doesn't work on Oracle, and
I have an sql query that counts the number of results for a complex
I know that you can get the SQL of a given QuerySet using print
I have a view defined in SQL server 2008 that joins 4 tables together.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.