存储过程,Stored Procedure,是在大型数据库系统中,一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。类比于C中的函数。
mysql与sqlserver是不同的。
/*创建存储过程*/ conn = getConnection(); //mysql dialect String procedureSQL = "create procedure USER_EXIST(in loginName varchar(50),out amount int)" + "begin select count(*) into amount from user where username = loginName ; end "; pstmt = conn.prepareStatement(procedureSQL); pstmt.executeUpdate(); /*调用存储过程*/ cstmt = conn.prepareCall("{call USER_EXIST(?,?)}"); cstmt.setString(1, "小明"); cstmt.registerOutParameter(2, Types.INTEGER); //注册输出参数 cstmt.execute(); System.out.println(cstmt.getInt(2)); //与上注册的对应