In this video we will learn how to call MySQL Stored Procedures with Java. This video covers OUT parameters.
Please SUBSCRIBE to this channel
Time – 00:00
Hi, this is Chad (Shod) at luv2code.com. Welcome back to another tutorial on Java JDBC. In this video, we will learn how to use store procedures. In this video we’re covering OUT parameters. Please check on our other videos that cover the other parameter types, such as IN, INOUT and Returning a result set.
Time – 00:21
For this tutorial, we will use the following table, employees. The table has sample data for testing. I have a SQL zip file that you can download. It’ll create the table, add sample data and also set up the store procedures. You can download it from the link below.
Time – 00:38
Let’s learn about OUT parameters. We will call a stored procedure and it will return an output parameter for us. Our DBA created a store procedure called Get Count for department. It is a very simple example that reads the parameter for the department name. The store procedure will return the number of employees in a given department.
Time – 00:58
The first parameter is the department name. The second parameter is the output for the count. This is a contrived example. We could do the same thing with a regular SQL statement, but here we’re trying to demonstrate out parameters with store procedures. You could develop a more advanced store procedure that could return different types of data.
Time – 01:14
To use OUT parameters, we prepare the callers before, using a question mark as a parameter place holder. We set the first input parameter as the name of the department. Next, we register the second parameter as an OUT parameter, since the store procedure will return the OUT parameter as an int, we make use of types.dot integer.
Time – 01:33
The types interface is defined in the JAVA.SQL package. Then, we execute the store procedure. Once the execution is complete, then we get the value of the OUT parameter. In this case, we retrieve it based on the parameter position, which is position two. Remember that parameters are one based. This will give us the output from the stored procedure.
Time – 01:54
Let’s switch over to Eclipse and see this in action. I have a very simple JAVA program called GetCountForDepartment. Let’s walk through the code. First thing I’ll do is get a connection to the database, set up a variable for the actual department name. Then, I’ll prepare a call for the stored procedure, get count for department. I’ll make use of two parameter place holders.
Time – 02:17
I’ll set the first parameter to be the actual name of the department. I’ll register the second parameter as an OUT parameter of type integer. Then, I’ll execute the stored procedure. Once the procedure’s complete, then I’ll get the value of the OUT parameter and I’ll retrieve position two. Then, I’ll simply display the output to the screen.
Time – 02:42
All right. Let’s go ahead and run the application. Do a right click. I’ll just do a run as Java application. That’ll give me the output. We can see that we actually called the stored procedure for engineering and it returns the value of four. This means that there are four employees in the engineering department. I can verify this in my SQL editor tool.
Time – 03:10
I can make use of a select statement. Select * for engineering. Execute and it gives me the result of four employees, so we know that the stored procedure’s working as desire.
Time – 03:24
Well, that wraps up our discussion on stored procedures using OUT parameters. We were successful in calling stored procedures and retrieving parameter values. Stay tuned for the next video. I will show more examples of using stored procedures and parameters. Please subscribe to our channel to view more videos on JAVA. Click thumps up to like our video. Also, visit our website, https://luv2code.com to download the Java source code used in this video.