Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

call executeQuery on a CallableStatement (K&B7, chapter 15, page 911)  RSS feed

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 911

Note that the executeQuery() command does not take a string (just like the PreparedStatement executeQuery() method). If you attempt to call executeQuery() on a CallableStatement with a String argument, a SQLException is thrown at runtime.


I use MySQL
SQL:

Java: (Creating of stored procedure, run once)

Java. Testing concepts:

this code demonstrate that no exception in runtime. Just result if any (assumption that passed to executeQuery(String arg) is right SQL querrry ).
also:
http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeQuery%28java.lang.String%29

SQLException - if a database access error occurs, this method is called on a closed Statement, the given SQL statement produces anything other than a single ResultSet object, the method is called on a PreparedStatement or CallableStatement


So i very confused.
Give advice why when i try i have no Exception and how answer to question what is result of execution on exam?

(This post originated in the K&B7 errata thread)
 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sergej Smoljanov wrote:give advice why when i try i have no Exception


It seems that not all versions of every vendor's JDBC implementation seems to be fully compliant with the contract defined by the main JDBC interfaces. You'll find examples here, here, and here. That's why a note should be added somewhere in the (introductory) section of the JDBC chapter to make readers aware of these inconsistencies (so they are not surprised/confused when executing code snippets). Already added to the errata overview.

Sergej Smoljanov wrote:and how answer to question what is result of execution on exam?


I didn't take the exam yet, so I don't know if you'll encounter similar questions on the actual exam. But if I encounter such question(s), I'll answer them according to the Java 7 API Specification (and thus what's mentioned in the K&B7 study guide) and not based on what I have experienced with a specific vendor implementation.

Hope it helps!
Kind regards,
Roel
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - The reason you are not getting an exception is because both PreparedStatement and CallableStatement are interfaces which extend from Statement. Statement DOES have a method called executeQuery(String). When you call executeQuery(String) then it is calling this method in the Statement interface.

The text is trying to tell you that CallableStatement and PreparedStatement do NOT have a executeQuery(String) method and only have executeQuery(). For CallableStatement and PreparedStatement the query is set when getting a statement object from a Connection therefore the query is already set no need to pass in anything to the executeQuery() method. Whereas when using just a Statement you set the query when calling the executeQuery(String) method.

I hope that makes sense??
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kirk Rohani wrote:The text is trying to tell you that CallableStatement and PreparedStatement do NOT have a executeQuery(String) method and only have executeQuery(). For CallableStatement and PreparedStatement the query is set when getting a statement object from a Connection therefore the query is already set no need to pass in anything to the executeQuery() method. Whereas when using just a Statement you set the query when calling the executeQuery(String) method.

I hope that makes sense??


No, that doesn't make any sense. The Java API Specification of the executeQuery(String) method in the Statement interface clearly states that invoking this method on a PreparedStatement or CallableStatement will throw a SQLException. As you know an interface is a contract, so an implementation of this interface should adhere to this contract and throw the SQLException.
 
Kirk Rohani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Kirk Rohani wrote:The text is trying to tell you that CallableStatement and PreparedStatement do NOT have a executeQuery(String) method and only have executeQuery(). For CallableStatement and PreparedStatement the query is set when getting a statement object from a Connection therefore the query is already set no need to pass in anything to the executeQuery() method. Whereas when using just a Statement you set the query when calling the executeQuery(String) method.

I hope that makes sense??


No, that doesn't make any sense. The Java API Specification of the executeQuery(String) method in the Statement interface clearly states that invoking this method on a PreparedStatement or CallableStatement will throw a SQLException. As you know an interface is a contract, so an implementation of this interface should adhere to this contract and throw the SQLException.



Okay I went back and re-read the reasons for a SQLException and it makes sense that the Javadoc says that it should and why you think it is due to the implementation not being correct. Thanks for the clarification!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!