In this video we will learn how to call MySQL Stored Procedures with Java. This video covers INOUT parameters.
Please SUBSCRIBE to this channel
Time – 00:00
Hi this is Chad (Shod) with luv2code.com. Welcome back to another tutorial on Java JDBC. In this video, we will learn how to use stored procedures.
Time – 00:11
In this video, we’re going to focus on INOUT parameters. I also have another collection of videos that cover the other parameter types. Please check those out in the playlist.
Time – 00:22
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 will create the table, add sample data and also set up the stored procedures. You can download it from the link below.
Time – 00:38
Let’s learn about INOUT parameters. We will pass in a parameter. The stored procedure will read the parameter and update it with a value. In this example, our DBA created a stored procedure called, “Greet the Department.” It’s a very simple example. It simply reads the parameter name for the department. All the stored procedure will do is create a new stream based on the department name. It will have the stream, “Hello to the awesome Department Team” and will plug in the value of team. That’s it. We could get really fancy and do some sequel calls for the database but for now let’s keep it simple.
Time – 01:12
To use INOUT parameters, we’ve prepared the callers before. We use the question mark as a parameter placeholder. Then we register the parameter as an OUT parameter, this supports INOUT. We also specify the type. Since we are passing in a stream we make use of types.varchar. The types interfaces the file in the Java and the SQL package. Then we execute the stored procedure. Once the execution is complete, then we can get the value of the OUT parameter.
Time – 01:39
In this case, we retrieve it based on the parameter position. Remember that parameters are one based. This will give us the output from the stored procedure and we display it accordingly.
Time – 01:50
Let’s switch over to Eclipse and see this in action. I have a simple Java program called, “GreetTheDepartment.” Now, let’s walk through the code. First, we get a connection to the database. Then we set up a variable for the department name. Then we prepare a callable statements to the stored procedure, with a question mark for the place holder. Then we register the parameter as an OUT parameter. Remember, the OUT parameter here also handles INOUT. Then we set the department name. Next we execute the statement, and it performs the back end update. Finally, we retrieve the value of the OUT parameter and display the result.
Time – 02:27
Okay, let me run the application and check the output. Notice we call the stored procedure. We pass in the department name with “Engineering”. Then it displays the result, “Hello to the awesome Engineering Team.” This is the result of that OUT parameter that came back from the stored procedure. The stored procedure performed as desired. Good job.
Time – 02:51
That wraps up our discussion on stored procedures using INOUT parameters. We were successful in calling the stored procedure and passing in those parameters. Stay tune for the next video. I’m going to show more examples of using stored procedures with parameters. Please subscribe to our channel to view more videos on Java. Click the thumbs up to like our video. Finally, visit our website https://luv2code.com to download the Java source code used in this video.