New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Calling stored procedures

From: Java Database Integration with JDBC

Video: Calling stored procedures

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.

Calling stored procedures

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.

Show transcript

This video is part of

Image for Java Database Integration with JDBC
Java Database Integration with JDBC

32 video lessons · 5997 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 5m 23s
    1. Welcome
      53s
    2. What you should know before starting this course
      1m 20s
    3. Using the exercise files
      3m 10s
  2. 12m 2s
    1. Testing your Java development environment
      5m 14s
    2. Importing a MySQL database
      5m 1s
    3. Creating a database user in MySQL
      1m 47s
  3. 32m 47s
    1. What is JDBC?
      4m 26s
    2. Choosing a JDBC driver
      6m 46s
    3. Connecting to a MySQL database server
      8m 7s
    4. Connecting to a HyperSQL database file
      6m 23s
    5. Executing a static SQL statement
      7m 5s
  4. 17m 42s
    1. Connecting to multiple databases
      6m 24s
    2. Handling JDBC exceptions
      7m 7s
    3. Closing database resources in Java 7
      4m 11s
  5. 47m 25s
    1. Looping through result sets
      8m 23s
    2. Moving the cursor in scrollable result sets
      5m 51s
    3. Limiting the number of fetched rows
      6m 57s
    4. Filtering data with prepared statements
      6m 58s
    5. Calling stored procedures
      5m 48s
    6. Handling multiple values from stored procedures
      5m 54s
    7. Using generic getter methods in Java SE 7
      7m 34s
  6. 45m 23s
    1. Managing data entities with JavaBean classes
      5m 0s
    2. Retrieving a single row as a JavaBean object
      6m 5s
    3. Inserting rows with prepared statements
      8m 2s
    4. Updating rows with prepared statements
      5m 4s
    5. Deleting rows with prepared statements
      4m 9s
    6. Managing data with updatable result sets
      6m 6s
    7. Using a persistent database connection
      6m 43s
    8. Committing and rolling back transactions
      4m 14s
  7. 9m 35s
    1. Getting the DatabaseMetaData object
      3m 40s
    2. Getting column and data type information
      5m 55s
  8. 50s
    1. Next steps
      50s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.