Retrieving query from java sql PreparedStatement

0 votes

I have used java.sql.PreparedStatement in order to execute the setString() method for populating the wildcards. But I wanted to know if I can fetch the final query before I execute the executeQuery() method?

Oct 12, 2018 in Java by misc.edu04
• 1,450 points
16,805 views

1 answer to this question.

0 votes

By using prepared statements, you can't retrieve the query :

  • You have a statement, containing placeholders
    • it is sent to the DB server
    • and prepared there
    • which means the SQL statement is "analyzed", parsed, some data-structure representing it is prepared in the memory
  • And, then, you have bound variables
    • which are sent to the server
    • and the prepared statement is executed -- working on those data

But there is no reconstruction of an actual real SQL query -- neither on the Java-side, nor on the database side. So, there is no way to get the prepared statement's SQL -- as there is no such query. And, if you are using it for debugging purpose you can try following:

  • Output the code of the statement, with the placeholders and the list of data
  • Or "build" some SQL query "by hand
answered Oct 12, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

0 votes
1 answer

How to retrieve column names from java.sql.ResultSet?

You may refer the below code: ResultSet ...READ MORE

answered Jul 4, 2018 in Java by sophia
• 1,400 points
2,866 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,640 points
3,054 views
0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
2,682 views
0 votes
1 answer

What do we understand from string pool in java?

This prints true (even though we don't use equals method: correct ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,140 points
702 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,699 views
0 votes
2 answers

Generate an alpha-numeric string randomly

Java supplies a way of doing this ...READ MORE

answered Jul 18, 2018 in Java by Daisy
• 8,140 points
2,613 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,179 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,920 points
2,780 views
0 votes
1 answer

Importing properties file from Java package

In case your class is present within ...READ MORE

answered Oct 15, 2018 in Java by code.reaper12
• 3,500 points
551 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,500 points
4,703 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP