- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are stored procedures? How to call stored procedures using JDBC program?
Stored procedures are sub routines, segment of SQL statements which are stored in SQL catalog. All the applications that can access Relational databases (Java, Python, PHP etc.), can access these procedures.
Stored procedures contain IN and OUT parameters, or both. They may return result sets in case you use SELECT statements, they can return multiple result-sets.
Example
Suppose we have a table named Dispatches in the MySQL database with the following data:
+--------------+------------------+------------------+------------------+ | Product_Name | Date_Of_Dispatch | Time_Of_Dispatch | Location | +--------------+------------------+------------------+------------------+ | KeyBoard | 1970-01-19 | 08:51:36 | Hyderabad | | Earphones | 1970-01-19 | 05:54:28 | Vishakhapatnam | | Mouse | 1970-01-19 | 04:26:38 | Vijayawada | +--------------+------------------+------------------+------------------+
And if we have created a procedure named myProcedure to retrieve values from this table as shown below:
Create procedure myProcedure () -> BEGIN -> SELECT * from Dispatches; -> END //
Example
Following is a JDBC example that calls the above mentioned stored procedure using JDBC program.
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; public class CallingProcedure { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Preparing a CallableStateement CallableStatement cstmt = con.prepareCall("{call myProcedure()}"); //Retrieving the result ResultSet rs = cstmt.executeQuery(); while(rs.next()) { System.out.println("Product Name: "+rs.getString("Product_Name")); System.out.println("Date Of Dispatch: "+rs.getDate("Date_Of_Dispatch")); System.out.println("Date Of Dispatch: "+rs.getTime("Time_Of_Dispatch")); System.out.println("Location: "+rs.getString("Location")); System.out.println(); } } }
Output
Connection established...... Product Name: KeyBoard Date of Dispatch: 1970-01-19 Time of Dispatch: 08:51:36 Location: Hyderabad Product Name: Earphones Date of Dispatch: 1970-01-19 Time of Dispatch: 05:54:28 Location: Vishakhapatnam Product Name: Mouse Date of Dispatch: 1970-01-19 Time of Dispatch: 04:26:38 Location: Vijayawada
- Related Articles
- What are Stored procedures in JDBC?
- Call Stored Procedures within a Stored Procedure with IF Logic?
- What are the advantages of stored procedures?
- Create a stored Procedures using MySQL Workbench?
- What are the advantages and disadvantages of using MySQL stored procedures?
- What are the differences between Stored procedures and functions?
- What is stored procedure and how can we create MySQL stored procedures?
- What are recursive stored procedures and why MySQL limits the recursion?
- How can we access tables through MySQL stored procedures?
- Writing Functions and Stored Procedures in SQL Server
- What are the special security requirements for using stored procedures and functions together with replication?
- What are TRIGGERS in DB2? What is the difference between TRIGGERS and STORED PROCEDURES?
- How to call a stored procedure that accepts input parameters, using JDBC program?
- How to call a stored procedure that returns output parameters, using JDBC program?
- How can we create MySQL stored procedures without ‘BEGIN’ and ‘END’?

Advertisements