1.基本操作
CallableStatement来执行存储过程
String sql = "{call f()}";
conn.prepareCall(sql) //执行sql语句
2.实例
/**存储过程f()函数
* DELIMITER $$
CREATE PROCEDURE p2(in sno1varchar(10),out sname1varchar(20))
begin selectsnameinto sname1 from student wheresno=sno1;
end $$ DELIMITER;
*@paramsno使用存储过程实现输入一个学号查询出学生姓名来
*/
publicstaticvoidcallProc2(Stringsno){
Connectionconn= ConnectionUtil.getConnection();
CallableStatementcs=null;
ResultSetrs=null;
Stringsql="{call p2(?,?)}";
try{
cs=conn.prepareCall(sql);
cs.setString(1,sno);
cs.registerOutParameter(2, Types.VARCHAR);
booleanbool=cs.execute();
if(bool){
rs=cs.getResultSet();
while(rs.next()){
Stringsname=rs.getString(1);
System.out.println(sname);
}
}else{
System.out.println(cs.getString(2));
}
}catch(SQLExceptione) {
e.printStackTrace();
}
}