Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc Jdbc-mysql Insert Data in Table Using Stored Procedure

Related Tutorials


 
 

Share on Google+Share on Google+

Insert Data in Table Using Stored Procedure

Advertisement
In this example, we are inserting data into a MySQL database table using stored procedure.

Insert Data in Table Using Stored Procedure 

     

In this example we are inserting data into a table using stored procedure.

Steps:
1.Create database:
To create database we use syntax: create database database_name. We can create database with the name "Employee"  as 

mysql> create database Employee;

Query OK, 1 row affected (0.09 sec)

2.Change the database:
 We are using
use database_name; to change the database

mysql> use employee;
Database changed

3.Create the table:

mysql> create table emp(name char(12),fathername char(12),password char(12));

Query OK, 0 rows affected (0.55 sec)

4.Create procedure :
 1.We can use delimiter to create multiple statements. To create delimiter we can use following syntax.

mysql> DELIMITER // 

2.To create procedure we use following syntax:
mysql>create procedure procedure_name(IN |OUT | INOUT) param_name type)
3.We write sql statement into begin......end body. To start begin use  
mysql>begin and to end use: mysql>end;
4.To finish the procedure use
mysql>// delimiter.

mysql> delimiter //
mysql> create procedure empproc(in name char(12),in fathername char(12),in password char(12))
-> begin
-> insert into emp values(name,fathername,password);
-> end;
-> //
Query OK, 0 rows affected (0.22 sec)

5.Step to load driver:
To load the driver we are using syntax:
Class.forName("com.mysql.jdbc.Driver").newInstance();

6.Steps to make connection:
We are using getConnection("url/database","user","password") method to create a connection.
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","
root");

7.Step to call procedure:
We are using CallableStatement to execute a stored procedure into java code. The following syntax is used to create an object of CallableStatement interface.
CallableStatement calstat=conn.prepareCall("{call empproc(?,?,?)}");

8.Step to pass the values into procedure:
To set the values we are using setXXX(). Here XXX is data type Object class name, e.g setString() to pass string in procedure.

9.Step to execute query :
To execute query we can use:
ResultSet
 rs = calstat.executeQuery();

10.Close connection :
calstat.close();

The code of the program is given below:

import  java.sql.*;
public class Insert_EMP{
  public static void main(String a[])throws Exception {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection conn=DriverManager.getConnection("jdbc:mysql:
//localhost:3306/employee"
,"root","root");
  CallableStatement calstat=conn.prepareCall("{call 
empproc(?,?,?)}"
);
  calstat.setString(1,a[0]);
  calstat.setString(2,a[1]);
  calstat.setString(3,a[2]);
  ResultSet rs = calstat.executeQuery();
  conn.close();
  calstat.close();
  System.out.println("Your data has been inserted into table.");
  }
}

The code of the stored procedure is given below:

delimiter //
create procedure empproc(in name char(12),in fathername 
char(12),in password char(12))
begin
insert into emp values(name,fathername,password);
end;
//

 

The output of the program is given below:

C:\rajesh\mysql>javac Insert_EMP.java
C:\rajesh\mysql>java Insert_EMP Rajesh Ram  Raju
Your data has been inserted into table.

Download this example.(Java Code)

Download this example.(Procedure)

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 7, 2007

Related Tutorials

Discuss: Insert Data in Table Using Stored Procedure   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:7
Bhai reddy
June 28, 2011
good

nice code.........and good way 2 understood
piyush
September 18, 2011
sdf

it's not working
Dashrath
October 9, 2011
java

your site is best of study
Dharmveer
January 20, 2012
insert using store procedure

i want insert data in mysql using store procedure.....i am using wamp server
bharat
March 20, 2012
while loop in stored procedure

DELIMITER $$ DROP PROCEDURE IF EXISTS `abc`.`myuserproc` $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `myuserproc`() BEGIN declare regno,employeeid bigint; declare x int; set regno=18; set employeeid=603177; set x=1; while x<=5 do insert into userdetails values('bharat_m',regno,'infosys','2012-03-20',employeeid); set regno=regno+1; set employeeid=employeeid+1; set x=x+1; end while; END $$ DELIMITER ;
lokesh
March 26, 2012
.net

question
John Wardan
December 19, 2012
Nothing

Dude
DMCA.com