29 April 2011

MySQL Stored Function 1



MySQL Stored Function Tutorial 1

1. Create The Below Stored Function In MySQL 
               (Here I am  using MySQl Query Browser)

DELIMITER $$


DROP FUNCTION IF EXISTS `test`.`updateOneFunction` $$


CREATE FUNCTION `test`.`updateOneFunction` (tname VARCHAR(30), tid INT)
                                         RETURNS INT
BEGIN

  UPDATE ONE SET name=tname WHERE ID=tid;

  RETURN 1;
END $$


DELIMITER ;



1. Now Write Java Code To Call It in Your Application

        try
{
cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
"test","root","root");
st=cn.createStatement();
st.executeUpdate("SELECT updateOneFunction('"+name+"','"+id+"')");
rs=st.executeQuery("select * from one WHERE id='"+id+"'");
rs.next();
System.out.println("Updated Name = "+rs.getString(2));
}catch(Exception e){
e.printStackTrace();
}
}

No comments:

Post a Comment

Leave Comment If You Like This...Thank You..!