带有输入参数的存储过程

来源:互联网 发布:大张伟 天天向上 知乎 编辑:程序博客网 时间:2024/05/17 01:37

创建带有输入参数的存储过程:

drop procedure if exists proc_user_in;delimiter //create  procedure proc_user_in(in in_param int)beginselect * from user where userid > in_param;end//delimiter ;
以上代码即在当前数据库中创建了存储过程,名字为proc_user_in,可调用此存储过程查询userid大于一定值的记录。

在命令栏里调用此存储过程代码:

call proc_user_in(2);
在类中写如下关键代码调用此存储过程:

public static void main(String[] args) {Connection con = null;CallableStatement cs = null;try {con = getConnection();String sql = "{call proc_employee_in(?)}";cs = con.prepareCall(sql);cs.setInt(1, 2);ResultSet rs = cs.executeQuery();while (rs.next()) {System.out.print(rs.getInt(1) + "..." + rs.getString(2));}} catch (Exception e) {e.printStackTrace();}}

以上两种方式调用该存储过程,均可打印出userid>2的全部记录。




0 0
原创粉丝点击