• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Confused about prepared statement vs callable statement (K&B7, chapter 15, page 907)

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

J Deckarm wrote:chapter 15, page 907
"Unlike prepared statements, stored procedures and functions must exist before a CallableStatement can be executed on them."
=> This sentence seems to be somewhat confusing; sounds as if CallableStatement would execute PreparedStatement as well


Thanks for the suggestion! But the previous sentence clearly states that a CallableStatement is used to execute a named stored procedure or function. So I think it's pretty obvious a prepared statement can't be executed using a CallableStatement.



While I see your point, I still find the sentence confusing; what does then "unlike prepared statements" mean in this context?

(This post originated in the K&B7 errata thread)
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J Deckarm wrote:While I see your point, I still find the sentence confusing; what does then "unlike prepared statements" mean in this context?


That's pretty easy

Assume I want to execute the stored procedure do_crazy_stuff I write the following codeWhen I execute this code, I get the following output:
SQLException: PROCEDURE myDb.do_crazy_stuff does not exist
SQLState: 42000
VendorError: 1305

So in order to be able to execute a CallableStatement the stored procedure and/or the function you want to call must already exist on the database itself. If the stored procedure (or function) doesn't exist, you'll get an error. So before I can execute this code, I have to create on the database (or through JDBC) the stored procedure do_crazy_stuff using a CREATE PROCEDURE statement.

But a prepared statement can be executed without first having to create the prepared statement in the database. The database itself will create (and prepare) the statement for you. You don't need to create a prepared statement in advance (like with the stored procedure and/or function). So this code will execute without an exception (if the table and columns exist of course)So you don't need to create a prepared statement in advance on the database before you can (successfully) execute a PreparedStatement (in Java); if you want to execute a stored procedure (or function), you must create it in advance on the database before you can (successfully) execute a CallableStatement (in Java).

Hope it helps!
Kind regards,
Roel
 
Popeye has his spinach. I have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic