之前使用JDBC一直都没用过存储过程;
今天在看看JDBC的书的时候,才发现,这些才是真正的有水平的技术,使用存储过程很方便;
1、使用CallableStatement来调用存储过程;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetUser`()
BEGIN
select ID,NAME from user;
END
2、对于带有参数的存储过程:
(1)in, JDBC使用类似于cst.set(1, 10)的方式来设置
(1)out,JDBC使用类似于cst.registerOutParameter(2, Types.VARCHAR);的方式来设置
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetUserByID`(in id int)
BEGIN
set @sqlstr=concat('select * from user where ID=', id);
prepare psmt from @sqlstr;
execute psmt;
END