8/18/10

Using MySQL Stored Procedures

Hi all, i have learnt how to use stored procedure in mysql using java. I would like that with you.

create a database and then create a table.

package testproject;


/**
 *
 * @author Raghuram
 */
import java.sql.*;


public class StoredProcedure {

    public static void createProcedure() {
        Connection con = null;
        Statement stat = null;
        Statement stat1 = null;
        Statement stat2 = null;
        ResultSet rs = null;
       
        try {




            /*create procedure empproc(in name char(50),in ph
            char(50))
            begin
            insert into emp values(name,ph);
            end;*/
            /*create procedure selectempproc()
            begin
            select * from emp;
            end;*/
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raghu", "root", "root");




            stat = con.createStatement();


//stored procedure using insert query
            String pr = "create procedure empproc(in name char(50),in ph "
                    + "char(50)) begin insert into emp values(name,ph); end;";
            //stored procedure using select query.
           // String pr = "create procedure selectempproc() begin select * from emp; end;";
            stat.executeUpdate(pr);






        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // rs.close();
                // stat.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void callProcedure() {
        Connection con = null;
        Statement stat = null;
        Statement stat1 = null;
        Statement stat2 = null;
        ResultSet rs = null;
        long now = 0;
        try {






            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raghu", "root", "root");
            now = System.currentTimeMillis();


            stat = con.createStatement();
            CallableStatement calstat = con.prepareCall("{call empproc(?,?)}");


            calstat.setString(1, "Sriram");
            calstat.setString(2, "98765432");
            rs = calstat.executeQuery();
            con.close();
            calstat.close();
            System.out.println("Your data has been inserted into table.");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // rs.close();
                // stat.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void callSelectProcedure() {
        Connection con = null;
        Statement stat = null;
        Statement stat1 = null;
        Statement stat2 = null;
        ResultSet rs = null;
        long now = 0;
        try {






            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raghu", "root", "root");
            now = System.currentTimeMillis();


            stat = con.createStatement();
            CallableStatement calstat = con.prepareCall("{call selectempproc()}");


           // calstat.execute();
            rs = calstat.executeQuery();
            if (rs == null) {
                System.out.println("No resultSet!");
            } else {
                ResultSetMetaData rsmd = rs.getMetaData();
                int nbCol = rsmd.getColumnCount();
                while (rs.next()) {


                    for (int j = 1; j <= nbCol; j++) {
                        System.out.println(j+"--" + rs.getString(j));
                    }
                }
            }


            con.close();
            calstat.close();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // rs.close();
                // stat.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String a[]) {
        createProcedure();
        callProcedure();
        callSelectProcedure();
    }
}

No comments:

Post a Comment

Popular Posts