您的位置:首页 > 其它

带有输出参数的存储过程

2015-08-08 23:36 375 查看
创建带有输出参数的存储过程:

drop procedure if exists proc_user_out;
delimiter //
create procedure proc_user_out(out out_param varchar(20))
begin
select username into out_param from user where userid=2;
end //
delimiter ;


以上代码即在当前数据库中创建了带有输出参数的存储过程,名字为proc_user_out,可调用此存储过程查询 userid=2 的 username。

在命令行中调用此存储过程代码:

call proc_user_out(@result);
select @result;
注意:以上的@result不能变

在类中写如下关键代码调用此存储过程:

public static void main(String[] args) {
Connection con = null;
CallableStatement cs = null;
try {
con = getConnection();
String sql = "{call proc_user_out(?)}";
cs = con.prepareCall(sql);
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
String name = cs.getString(1);
System.out.println(name);
} catch (Exception e) {
e.printStackTrace();
}
}
以上两种调用均可得到 userid=2 的 username 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: