Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Calling stored procedures


From:

Java Database Integration with JDBC

with David Gassner

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

please wait ...
Watch the Online Video Course Java Database Integration with JDBC
2h 51m Intermediate Nov 28, 2012

Viewers: in countries Watching now:

Whether you're building a web- or desktop-based application with Java SE or Java EE, many Java applications need to integrate data from a relational database. This course describes how to read and manage data from relational databases such as MySQL and SQL Server using the Java Database Connectivity (JDBC) API.

Author David Gassner explains how to choose a JDBC driver and connect to one or more databases. He also provides detailed instructions on reading, selecting, and updating data; calling stored procedures; managing data via JavaBean classes or with prepared statements; and working with metadata.

Topics include:
  • Importing a MySQL database
  • Connecting to databases
  • Handling JDBC exceptions
  • Looping through result sets
  • Limiting the number of fetched rows
  • Filtering data with prepared statements
  • Calling stored procedures
  • Inserting, updating, and deleting rows with prepared statements
  • Using a persistent database connection
  • Committing and rolling back transactions
Subject:
Developer
Software:
Java
Author:
David Gassner

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.

There are currently no FAQs about Java Database Integration with JDBC.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Database Integration with JDBC.

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.