• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JDBC Statements

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inheritance hierarchy for JDBC statements puzzles me a little.
Specifically, why would PreparedStatement and CallableStatement want to inherit some of the methods of Statement?

For example, I tried the following:


I tried code similar to the above with MS SQL Server 2000 driver, only to be given an exception.
Why does the JDBC API allow this behaviour?
Is there ever a reason why you'd do such a thing?
Is it a fair statement that some of the Statement methods should never be called on a PreparedStatement (and CallableStatement) instance?

Advice is most appreciated.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Why does the JDBC API allow this behaviour?

Both the CallableStatement and PreparedStatements are still implementations of the Statement interface, and hence must implement the methods declared in Statement. Now, I would expect an exception to be thrown when you try to execute the ps.executeQuery(<query string> method on an instance of PreparedStatement. My question is which exception did you receive? I have not decompiled the PreparedStatement class to see if Sun decided to allow Driver providers to determine the exception that should be thrown when this method is executed, or whether they decided to throw a NotImplementedException. Still, getting an exception is what I would have expected.

>> Is there ever a reason why you'd do such a thing?

Yes, as stated above the PreparedStatement must provide an implementation of all methods in the Statement interface. The logical result of executing the method would be an exception.

>> Is it a fair statement that some of the Statement methods should never be called on a PreparedStatement (and CallableStatement) instance?

Yes, I think this is a fair and correct statement. While the method must be present due to implementing the Statement interface, calling executeQuery(String query) method of an instance of PreparedStatement is not valid.

Mike
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Reterstorf:
I have not decompiled the PreparedStatement class to see if Sun decided to allow Driver providers to determine the exception that should be thrown when this method is executed, or whether they decided to throw a NotImplementedException.



Good news - you needn't.
PreparedStatement is itself an Interface, and just inherits from Statement - not adding it's own thoughts to the method.

Since the docs are generated automatically, you may investigate such Details only by looking at the javadocs, but may of course look at '$JAVA_HOME/src.zip' too.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A reasonable conclusion, then, is that the inheritance hierarchy for the JDBC Statement interfaces has a mild design issue (that isn't documented?).
Specifically, a Prepared(/Callable)Statement is not a Statement, since the operations defined are distinct between the two, and therefore, PreparedStatement should never have inherited from Statement.

Some of the inconsistencies and inaccuracies of the statements made by Mike make me wonder about the authenticity of the other claims that are made.

Can anyone back up my hypothesis with some documentation?
I find nothing in the JDBC Specification or the API Specification.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic