定义:存储过程是根据实际功能的需要创建一个程序模块,并存储在数据库中。
语法:
create or replace procedure 存储过程名(参数列表)
begin
end
注意:
创建存储过程的时候不管要不要参数,都要带括号
删除存储过程只指定存储过程名即可,不带括号
--创建实例
create procedure test_procedure(in n int)
begin
select * from users where id = n;
end
--定义变量
set @n=1;
--调用存储过程
call test_procedure(@n);
--jdbc调用mysql存储过程
1 //建立链接对象 2 Class.forName("com.mysql.jdbc.Driver"); 3 String url = "jdbc:mysql:///test?user=root&password=6666"; 4 Connection conn =(Connection) DriverManager.getConnection(url); 5 //调用存储过程 {call 存储过程名(参数列表1,参数列表2)可用?代替} 6 CallableStatement cs = conn.prepareCall("{call test_procedure(?)}"); 7 //设置输入参数 8 cs.setInt(1, 1); 9 //执行存储过程 10 ResultSet rs = cs.executeQuery(); 11 if (rs.next()) { 12 System.out.println(rs.getInt(1)+" ,"+rs.getString(2)); 13 }