Java

How to create a Stored procedure in JDBC Java

This tutorial will teach you how to create a a stored procedure in JDBC. In the example which will help you to create a stored procedure step by step using java code simple way.

The Simple way to create the stored procedure.you can use the following code snippet to create a parameterized stored procedure.

String str = "CREATE PROCEDURE authors(p_auth_id varchar(15), OUT p_auth_fname varchar(20),
 OUT p_auth_lname varchar(20))\n" + 
"BEGIN\n" + " SELECT auth_fname, auth_lname INTO p_auth_fname, p_auth_lname 
FROM authors where auth_id = p_auth_id;\n" + "END;";

In the above code snippet  we created the stored procedure name authors. it created that accept auth_id as parameter and retrieve the corresponding author information from the database.the retrieved information stored in the OUT parameters.

Store procedure Consist of two parameters

IN : argument can pass to a stored procedure.

OUT : return value of a stored procedure.

the following source code will explain how to create a stored procedure jdbc.

import java.sql.*;

public class Jdbcc {
 
 Connection con;
 PreparedStatement pst;
 
 public Jdbcc()
 {
     Connect();
        }
public void Connect()
{
try 
 {
 Class.forName("com.mysql.jdbc.Driver");
 con = DriverManager.getConnection("jdbc:mysql://localhost/abclibrary", "root","");   
            
 String str = "CREATE PROCEDURE authors(p_auth_id varchar(15), OUT p_auth_fname varchar(20), OUT p_auth_lname varchar(20))\n" + 
         "BEGIN\n" +
                   "  SELECT auth_fname, auth_lname INTO p_auth_fname, p_auth_lname FROM authors where auth_id = p_auth_id;\n" +
                   "END;";
        Statement stmt = con.createStatement();
  int k = stmt.executeUpdate(str);
 } 
 catch (ClassNotFoundException e) 
  {
  e.printStackTrace();
   } 
  catch (SQLException e) 
  {
  e.printStackTrace();
         }
 }
 public static void main(String args[])
 {
  Jdbcc jk = new Jdbcc();
 }
}

I have attached the video tutorial below it will help you  to do this  step by step.

 

admin

Recent Posts

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

3 weeks ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

1 month ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

2 months ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

2 months ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

3 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago