Preparing for Java Interview?

My books Grokking the Java Interview and Grokking the Spring Boot Interview can help

Download PDF

Difference between IN, OUT, and INOUT parameters in JDBC Stored Procedure? Answer

Hello guys, Java Database Connectivity, the JDBC API supports three types of parameters, I mean, IN, OUT, and INOUT. They are used to bind values into SQL statements. An IN parameter is the one whose value is unknown when the SQL statement is created and you bind values using various setXXX() method depending upon the type of column those IN parameter refers in SQL query. For example in SQL query, SELECT * from EMPLOYEE where EMP_ID=? if the EMP_ID is a VARCHAR column then you must call the setString() method to pass the value to the IN parameter. 


While the OUT parameter is the one whose value is supplied by the SQL statement it returns. You retrieve values from the OUT parameters by calling the getXXX() methods, depending upon the data type.

On the other hand, the INOUT parameter is the one that provides both input and output values. You can not only bind variables with the setXXX() methods but also can retrieve values with the getXXX() methods. 

A PreparedStatement object only uses the IN parameter. but the CallableStatement object can use all three types of parameters. These were some fundamental differences between IN, OUT, and INOUT parameters in JDBC. Let's see a couple of more differences in point format. 





IN vs OUT vs INOUT parameters in Java JDBC Stored Procedure

Now, you know the definition and purpose of IN, OUT, and INOUT parameters. Let's compare them against each other

1. Main difference between IN and OUT parameters is that IN is used to pass data to the database via SQL query, while OUT parameter is used to read results from the database, especially in the case of stored procedure. 

2. Second difference is that the value of IN paraemter is provided using setXXX() mehtod like the setString(), setInt(), while value of OUT parameter is retrieved using getXXX() method like the getString(), getInt() etc. 

3. IN Parameter is used by both PreparedStatement and CallableStatement but OUT parameter can only be used with CallableStatement.

4. INOUT parameters provide both input and output values. You bind variables with the setXXX() methods and retrieve values with the getXXX() methods.


Difference between IN, OUT, and INOUT parameters in JDBC? Answer


That's all about the difference between IN, OUT, and INOUT parameters in JDBC. Remember, you can use IN parameter to pass input arguments to the stored procedure and can use OUT parameters to retreive results from a stored procedure. You can also use the INOUT parameters to both send inputs as well as retrieve results. The most important thing is that JDBC binds them using both setter and getter methods. 


Other JDBC and SQL Tutorials and  Resources you may like
  1. Top 5 Free JDBC Courses for Java Programmers (free courses)
  2. Difference between the Callable and Prepared Statement? (answer)
  3. 10 JDBC Best Practices Every Java Programmer should follow (read)
  4. How to connect to MySQL database from Java Program? (tutorial)
  5. JDBC Batch Insert and Update example using PreparedStatement (tutorial)
  6. Difference between java.sql.Date, java.sql.Timestamp, and java.util.Date in JDBC? (answer)
  7. Difference between Type 1 and Type 4 JDBC drivers (answers)
  8. Top 10 JDBC Interview Question for Java programmers (read)
  9. 6 Essential JDBC Performance tips for Java Programmers (tips)
  10. How to connect to SQL Server database from Java (example)
Thanks for reading this tutorial so far. If you like this JDBC tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.