Java CallableStatement InterfaceCallableStatement interface is used to call the stored procedures and functions. We can have business logic on the database by the use of stored procedures and functions that will make the performance better because these are precompiled. Suppose you need the get the age of the employee based on the date of birth, you may create a function that receives date as the input and returns age of the employee as the output. What is the difference between stored procedures and functions.The differences between stored procedures and functions are given below:
How to get the instance of CallableStatement?The prepareCall() method of Connection interface returns the instance of CallableStatement. Syntax is given below: The example to get the instance of CallableStatement is given below: It calls the procedure myprocedure that receives 2 arguments. Full example to call the stored procedure using JDBCTo call the stored procedure, you need to create it in the database. Here, we are assuming that stored procedure looks like this. The table structure is given below: In this example, we are going to call the stored procedure INSERTR that receives id and name as the parameter and inserts it into the table user420. Note that you need to create the user420 table as well to run this application. Now check the table in the database, value is inserted in the user420 table. Example to call the function using JDBCIn this example, we are calling the sum4 function that receives two input and returns the sum of the given number. Here, we have used the registerOutParameter method of CallableStatement interface, that registers the output parameter with its corresponding type. It provides information to the CallableStatement about the type of result being displayed. The Types class defines many constants such as INTEGER, VARCHAR, FLOAT, DOUBLE, BLOB, CLOB etc. Let's create the simple function in the database first. Now, let's write the simple program to call the function. Output: 53 Next TopicTransaction Management in JDBC |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India