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?
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
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).