Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In previous videos of this series I have described how to work with static SQL statements using the Statement interface and with parameterized statements using PreparedStatement. There is a third interface that's available in JDBC called CallableStatement which can be used with stored procedures and functions that are defined at the database level. Let's begin by talking about the nature of a stored procedure. In this project, StoredProcs, I have a main class that's using PreparedStatement and a parameterized SQL statement as a String, and I'm filling in the parameter at runtime.
I am going to take this parameterized SQL and move it to the database level as a stored procedure and then show you how to call that stored procedure from your JDBC application. I'll start by minimizing Eclipse and my browser, and I'll get to this file MySQL StoredProcs.txt. The syntax for defining stored procedures can vary widely from one database to another, so I'm going to focus on just one database for this exercise, MySQL, and I have provided all the code you need for MySQL in this file, which is available in the free exercise files that accompany the course. You can find it in the database folder as MySQL StoredProcs.txt.
I have the code here for a number of different stored procedures. I'm going to use this one called GetToursByPrice. Just as in the previous exercise, this SQL has a parameterized value, what MySQL calls an in parameter, a value that's passed in at runtime. I am going to select all the code starting with the DROP command and going all the way through DELIMITER. Now, if you're curious about what those Delimiter commands are doing, the default end of line delimiter for MySQL is a Semicolon, but SQL uses that Semicolon as well.
So in this code, I temporarily set the DELIMITER for MySQL to a double slash, and then the semicolon will be taken as the DELIMITER only for the SQL statement. The double slash is used here to say I'm finished creating the procedure, and then I reset MySQL DELIMITER back to a semicolon. So I'm going to select all of this code starting with the DROP command and going all the way through the call to the DELIMITER command, and I'll copy that to the clipboard. Next, I'll go to my browser where I have already opened phpMyAdmin, and I'll go to my explorecalifornia database and then click on SQL, and this is where I can execute arbitrary SQL statements, I'll paste in that code.
I'll scroll up to make sure I have got all of it starting with the DROP command. This makes sure that if I already have a stored procedure of this name that I'll drop it first before I re-create it and then I'll click Go. I confirmed that want to drop any procedure that might already exist with this name and click OK. And I get back messages indicating that the code was executed but no data was returned, and that's what I expect. Now I'm going to test the procedure for MySQL, I'll delete all that SQL, and in order to call the stored procedure directly in phpMyAdmin, I'll start with the word call, then the name of my stored procedure, then a parameter that matches the required data type, I will pass in 300, and I close the statement with a semicolon, I'll click Go, and I get back a total of seven rows just what would I expected.
I'll go back to MySQL statement, and I'll try it again. I will once again call GetToursByPrice, and this time I'll pass in 100, and I'll click Go, and once again as expected, I get back a single row. So now I know that my stored procedure is working as expected, and I can go and try to call it from Java. I'll close the Browser, and I'll close the Text Editor, and I'll return to Eclipse. Because MySQL logic is now part of my stored procedure I don't need it in the application itself.
So I'm going to delete this SQL code and replace it with the same call I used in phpMyAdmin but I'm going to wrap the call inside a pair of braces. So I'll start with an opening brace, then once again call, then the name of the stored procedure GetToursByPrice, and then a place holder, which is exactly the same as in a standard SQL statement a question mark, I don't need all that with my codes, so I'll close it up a little bit, and now that SQL code is ready to use. Now I'll move down to where I have declared my prepared statement object.
When you're recalling a stored procedure or function from the database instead of using the prepared statement interface, use the CallableStatement. I'll start with word the Callable, I'll press Ctrl+Space, and I'll choose the CallableStatement interface from java.sql, and instead of calling the method to PreparedStatement, I'll call the method PrepareCall. Just as with PrepareStatement when you call the PrepareCall method, you can pass in the SQL String and the type and whether you want your result set to be read-only or updatable.
Everything else is exactly the same just like with PreparedStatement the CallableStatement has set methods for various data types. I'm still passing in a double value so I'll call setDouble, and I'll pass in maxPrice as the first value. And I am ready to test my code. I run the application, I'll type in a value of 300, and I get back my expected number of rows. I'll run the application again, I'll type in a value of 100, and I'll get back one row, and I'll run it one more time, and this time I'll type in 50 and I get back no rows.
So all I have done here is to move MySQL logic from my client application into the database itself. You can do this with any database management system that supports stored procedures and functions, take a look at the documentation for your database management system to learn how to define the stored procedures and functions, but when you're in JDBC the syntax for calling the stored procedures and functions is always pretty much the same.
Get unlimited access to all courses for just $25/month.Become a member