• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
Your mother was a hamster and your father was a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic