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

Handling multiple values from stored procedures

From: Java Database Integration with JDBC

Video: Handling multiple values from stored procedures

Many Database Management Systems let you create stored procedures that return more than one value, for example, in MySQL. You can create a stored procedure that returns both a result set and one or more of what are called Out Parameters. Here's the syntax in MySQL, I have provided this code in the file MySQLStoredProcs.txt, which is part of the free exercise files that accompany this course. As with any stored procedure in MySQL, you start by setting the DELIMITER to a double slash or some other String that won't be in conflict with SQL.

Handling multiple values from stored procedures

Many Database Management Systems let you create stored procedures that return more than one value, for example, in MySQL. You can create a stored procedure that returns both a result set and one or more of what are called Out Parameters. Here's the syntax in MySQL, I have provided this code in the file MySQLStoredProcs.txt, which is part of the free exercise files that accompany this course. As with any stored procedure in MySQL, you start by setting the DELIMITER to a double slash or some other String that won't be in conflict with SQL.

When you create the procedure, you define arguments. Here I am declaring an IN parameter, the same parameters as I used in a previous exercise, but I'm also defining an OUT parameter, a value that can be passed back from the stored procedure. In this stored procedure, I'm executing two separate select statements, the first one selects the count of rows from the tours table. It selects the count, but then instead of naming that return value as a part of the result set as you might do in conventional SQL, I'm taking the value of count, and I am passing it to the OUT parameter by its name, total.

Then I'm executing a second SELECT statement. This one will return a conventional result set, I am returning all columns, but I could just as easily put it on this column names that I want to return. It's essential that the select statement that returning the result set be executed last. That's how you'll get that data out of the StoredProcedure. The SELECT statements that are simply setting the value of OUT parameters such as my first select statement can be placed anywhere in the chain. So I'm going to take this code and import it into my MySQL database.

I'll select everything starting with a DROP command and ending with the DELIMITETR command, and I'll copy it to the clipboard. Then I'll go to phpMyAdmin, I'll go to my database, and to the SQL panel I'll paste in that code and click Go. I'll confirm and I'll check and make sure that everything was executed correctly. Now I'm ready to run this code for my Java application. I'll switch back to Eclipse where I am working with a project named StoredProcOut, this version of my project is using the CallableStatement interface already.

And right now it's calling the stored procedure GetToursByPrize, which receives a single argument. In this version of my application I'll change MySQL String from the original stored procedure GetToursByPrice to the new stored procedure GetToursWithCountByPrice. And I'll match the arguments in the original stored procedure in MySQL. I had two arguments, so I'll set two arguments here. I'm still calling a stored procedure, so I'll use the CallableStatement just like I did before. But now I have a couple more bits of code to add to deal with that OUT parameter.

I'll place the cursor after the code that's setting the IN parameter, which is the double value of the Max price, then I'll add another line here, and I'll call another method called registerOutParameter. Just as with the set methods, you can identify your parameter either by its ordinal position, that is 1, 2 and so on in the SQL String, or by its name. I'm going to use a version of registerOutParameter where I pass in the name of the parameter as a String, but again I could also pass the value of 2. Next, I indicate the data type of the value. I'm expecting that you set the data Type by using a constant that's the part of a class called Types, and I'm going to set this to Types.INTEGER.

So I have set the IN value with setDouble, and I have registered the OUT value with registerOutParameter. Now I execute the query. After I execute the query, the results will be returned automatically. It always returns the result set resulting from the last SQL statement in the stored procedure. But to get the OUT parameter, I have to do a little bit more work. I'll declare an integer variable that I'll name nRows, and I'll get its value by calling a method of this statement object not of the result set. It will look like this stmt.getint you'll find that there are other methods called getdouble, getstring, and so on.

And as before, I can reference the parameter either by its position in the argument list or by its name. I'll choose the version where I reference by Name, and I'll type in the name of the parameter, and now I have that value returned as the variable nRows. The code in my displayData method in the Tours class already has code to figure out how many rows there are, but I'm not going to need that anymore. That value is already being returned by the stored procedure. So I'll go into the displayData method and comment out those two lines of code, and I'll re-factor this so that nRows is passed in as an argument.

I'll save those changes, I'll go back to my main class, and I'll change the way that I'm calling displayData, and I'll pass in nRows. So I have moved some of the actual work from the client application back to the database. In fact, I no longer even need a scrollable result set because now I'm not moving the cursor forward and back, I'm only moving it forward looping through the returned rows. I'll test my application, I enter a value of 300, and there is a result the Number of tours is correct, and there is the data.

And I'll run it once again, and this time I'll pass in a value that gets back no rows, and there's the result. From the user's point of view it's all working exactly like it did before, but now I have moved as much of the logic as I can back to the database. This lightens the load on the client, reduces the amount of work you have to do and the amount of memory you have to use, and eliminates the need for a scrollable result sets, which do take a little bit more memory and resources than forward only result sets. You can do it either way moving the logic into your client or defining it and keeping it on the database.

As a JDBC developer, the more you know about what your database management system can do, the more choices you'll have.

Show transcript

This video is part of

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

32 video lessons · 7127 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
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.

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

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

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.