Tuesday, August 20, 2013

Insert Data in Table Using Stored Procedure in java

mp1.java 

import  java.sql.*;

public class mp1{

  public static void main(String a[])throws Exception {

  Class.forName("com.mysql.jdbc.Driver");

  Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","enggheads");
  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();

  calstat.close();
  System.out.println("Your data has been inserted into table.");
 conn.close();
  }
}
}
}

steps:-

1.Create database
2.Change the database
3.Create the table
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>beginand 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)

No comments:

Post a Comment