创建带有输入输出参数的存储过程:
drop procedure if exists proc_user_inout;
delimiter //
create procedure proc_user_inout(inout inout_param int)
begin
select count(*) into inout_param from user where userid > inout_param ;
end //
delimiter ;
以上代码即在当前数据库中创建了带有输入输出参数的存储过程,名字为proc_user_inout,可调用此存储过程查询 userid >2 的记录数。
在命令行中调用此存储过程代码:
set @result=2;
call proc_user_inout(@result);
select @result;
注意:以上的@result不能变
在类中写如下关键代码调用此存储过程:
public static void main(String[] args) {
Connection con = null;
CallableStatement cs = null;
try {
con = getConnection();
String sql = "{call proc_user_inout(?)}";
cs = con.prepareCall(sql);
cs.registerOutParameter(1, Types.VARCHAR);
cs.setInt(1, 2);
cs.execute();
int number = cs.getInt(1);
System.out.println(number);
} catch (Exception e) {
e.printStackTrace();
}
}
以上两种调用均可得到 userid >2 的记录数。