Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

get SQL statement from PreparedStatement

Thread view: 
JY - 23 Feb 2006 17:54 GMT
How can I get the SQL statement which will be submitted by the
PreparedStatement to the database to execute?

Thanks
Raymond DeCampo - 23 Feb 2006 18:19 GMT
> How can I get the SQL statement which will be submitted by the
> PreparedStatement to the database to execute?

That isn't how prepared statements (necessarily) work.  The idea behind
the prepared statement is that the driver or database will precompile
the SQL into some driver or database specific format that allows for
value bindings.  Typically the execution plan for the SQL is determined
by this (the execution plan is the steps the database will take to
retrieve the results; e.g., use index XYZ, full table scan of ABC, etc.).

It is possible for a JDBC driver to trivially implement
PreparedStatements by generating SQL and transmitting that to the
database.  However, in such a driver PreparedStatements would not be any
more efficient than regular Statements.

If what you really wanted to achieve was to known what parameters are
being passed to the PreparedStatement there are JDBC spy tools that can
help.

HTH,
Ray

Signature

This signature intentionally left blank.

JY - 23 Feb 2006 18:44 GMT
>> How can I get the SQL statement which will be submitted by the
>> PreparedStatement to the database to execute?
[quoted text clipped - 17 lines]
> HTH,
> Ray

I need to print out all the SQL messages as being submitted to database, in
my log file. I want to write a method which will take PreparedStatement as
an argument and print out its SQL to server.log file. Anywhere in my
application after I have set arguments to a PreparedStatement, I will call
this method before executing the SQL in PreparedStatement. Thats why I ask
this question. Any way I can achieve this task?

Thanks
Raymond DeCampo - 23 Feb 2006 19:08 GMT
>>>How can I get the SQL statement which will be submitted by the
>>>PreparedStatement to the database to execute?
[quoted text clipped - 24 lines]
> this method before executing the SQL in PreparedStatement. Thats why I ask
> this question. Any way I can achieve this task?

Perhaps the best approach will be to examine the logging options
provided by your JDBC driver.  But I re-iterate, the term "SQL message"
does not necessarily apply to a PreparedStatement.  Also, you do not
"execute the SQL in [a] PreparedStatement."  You execute the
PreparedStatement.

HTH,
Ray
Signature

This signature intentionally left blank.

JY - 23 Feb 2006 20:08 GMT
>>>>How can I get the SQL statement which will be submitted by the
>>>>PreparedStatement to the database to execute?
[quoted text clipped - 33 lines]
> HTH,
> Ray

So java does not have a function which can tell me the SQL statement which
will be executed? In other languages like PowerBuilder, you can get the SQL
statement you are going to submit.
IchBin - 23 Feb 2006 20:34 GMT
>>>>> How can I get the SQL statement which will be submitted by the
>>>>> PreparedStatement to the database to execute?
[quoted text clipped - 37 lines]
> will be executed? In other languages like PowerBuilder, you can get the SQL
> statement you are going to submit.

That is because the SQL statement is embedded in the DataWindow object.

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Raymond DeCampo - 23 Feb 2006 20:47 GMT
> So java does not have a function which can tell me the SQL statement which
> will be executed? In other languages like PowerBuilder, you can get the SQL
> statement you are going to submit.

I do not know how many other ways I can tell you that once you are at
the PreparedStatement stage you are past the SQL portion of the program.

There is nothing in the standard APIs to help you.  So start looking in
the documentation specific to your driver or start implementing it yourself.

Ray

Signature

This signature intentionally left blank.

steve - 23 Feb 2006 22:52 GMT
>>>>> How can I get the SQL statement which will be submitted by the
>>>>> PreparedStatement to the database to execute?
[quoted text clipped - 37 lines]
> will be executed? In other languages like PowerBuilder, you can get the SQL
> statement you are going to submit.

basically you build an sql string and submit it to the database.
then you add in your  paramaters  etc and submit it to the database..

the database parses and compiles the statement internally. ( or it may even
have a pre-parsed, pre-compiled previous version squirreled away(oracle))

if you want to see the sql "at the database end" after the statement has been
assembled, then you CAN use java, but you must query the database tools to do
it.

the only other way i can think would be to re-write your sql routines, so
that you log the statement & params BEFORE they are sent to the database.

it depends on the database you are using.
Thomas Kellerer - 24 Feb 2006 16:55 GMT
JY wrote on 23.02.2006 21:08:
> So java does not have a function which can tell me the SQL statement which
> will be executed? In other languages like PowerBuilder, you can get the SQL
> statement you are going to submit.

As the Statement class is implemented by the driver it's up to the database
vendor to implement such functionality. This has nothing to do with Java itself.
Actually there are drivers out there that will print everything when calling
toString() on a Statement or a PreparedStatement (e.g. the Postgres driver IIRC)

It's been some time since I have used PowerBuilder but I cannot recall a way to
get that information for statements using bind variables either.

Thomas
IchBin - 23 Feb 2006 20:26 GMT
> How can I get the SQL statement which will be submitted by the
> PreparedStatement to the database to execute?
>
> Thanks

Can find a lot of examples at the "The Java Developers Almanac 1.4"
http://www.google.com/custom?domains=javaalmanac.com&q=PreparedStatement+&sa=Goo
gle+Search&sitesearch=javaalmanac.com&client=pub-6001183370374757&forid=1&ie=ISO
-8859-1&oe=ISO-8859-1&cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A66
3399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3
BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1%3B&hl=en


Example: "e259. Inserting a Row into a Database Table Using a Prepared
Statement"
http://javaalmanac.com/egs/java.sql/InsertPs.html

Documented in "The Big Index"
here:http://java.sun.com/docs/books/tutorial/reallybigindex.html

under: "Trail: JDBC(TM) Database Access"

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Real Gagnon - 24 Feb 2006 03:48 GMT
> How can I get the SQL statement which will be submitted by the
> PreparedStatement to the database to execute?

Enabling the JDBC driver log function may be a way  to get it.

See http://www.rgagnon.com/javadetails/java-0411.html
for an example.

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

Andrea Desole - 24 Feb 2006 17:23 GMT
> How can I get the SQL statement which will be submitted by the
> PreparedStatement to the database to execute?

I found this some time ago. Never really used it, though. Check it out

http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.