1、TIPS
(1)SQL语句在执行时需先编译再执行。
存储过程是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中。用户通过指定存储过程的名字并给定参数来调用执行它。
一个存储过程是一个可编程的函数,在数据库中创建并保存。
可对存储过程设置权限,这样可以避免非授权用户对数据的访问。
MySQL5.0开始支持存储过程,能提高数据库处理速度。
MySQL存储过程创建的格式:
create procedure 过程名(in|out|inout 参数名 数据类型.....)
begin
....
end
存储过程调用:call 过程名();
变量定义: declare 变量名 数据类型 [default value];注:仅在begin/end语句里,必须在开头!
变量赋值: set 变量名=表达式值;
if....then if...then
....; ...;
else end if;
....;
end if;
删除存储过程: drop procedure if exists 存储过程名;
列出所有的存储过程: show procedure status;
selete id, data into x, y from 表名;
(2)Java调用存储过程
callableStatement = connection.prepareCall("{call 过程名(?)}");
callableStatement.setInt(1, 1);//设置输入参数
resultSet = callableStatement.executeQuery();//执行存储过程
while(resutlSet.next()) {
}
使用callableStatement.registerOutParameter(1, Types.Integer);//设置输出参数 java.sql.Types中定义的类型
2、PS
(1)
(2)