--------不带参数---------
delimiter $$ create procedure pro1() begin select * from tableName where id<10; end; $$
--------带输入参数---------
delimiter $$ create procedure pro2(in c int) begin select * from tableName where id<c; end; $$
--------带输出参数---------
delimiter $$ create procedure pro3(out b varchar(20)) begin select name into b from tableName where id<10; end;
--------带输出和输入参数---------
delimiter $$ create procedure pro3(in a int, out b varchar(20)) begin select name into b from tableName where id<a; end;
Java中使用存储过程
//mysql> delimiter $ //mysql> create procedure pro1(in a int, out b varchar(20)) //-> begin //-> select name into b from stu where id=a; //-> end; //-> $
//获得链接对象 Connection conn = JdbcUtils.getConnection(); //创建执行存储过程的命令对象 CallableStatement cstmt = conn.prepareCall("{call pro1(?,?) }"); //指定问号的值 cstmt.setInt(1, 1); cstmt.registerOutParameter(2, Types.VARCHAR); //执行存储过程 cstmt.executeUpdate(); //获得返回值 String name = cstmt.getString(2); System.out.println(name); //释放资源 JdbcUtils.release(null, cstmt, conn);
本文出自 “曾颐楠的播客” 博客,请务必保留此出处http://zengyinan.blog.51cto.com/9524976/1721401