Java调用存储过程步骤如下:
1. 编写存储过程。
2.编写java调用测试类,可以是main方法,也可以JUnit单元测试类。
3.到数据库中检查,是否调用存储过程成功。
下面是调用不带参数的存储过程:
1. 编写简单存储过程如下:(创建customer的临时表,把customer是id小于100的customer插入到临时表中)
CREATE PROCEDURE createTmpTable()
BEGIN
CREATE TABLE TMP_CUSTOMER SELECT id,name,birthday,memo,tid FROM customer;
INSERT into TMP_CUSTOMER SELECT id,name,birthday,memo,tid FROM customer where id<100;
END;
2.Java调用,(JUnit单元测试类)
@Test
public void testCallableStatement() throws Exception{
Connection conn=null;
CallableStatement cst=null;
String sql="{call createTmpTable}";
try{
conn=DBUtils.getConnection(); //这里需要自己编写获取connection公共方法
cst=(CallableStatement) conn.prepareCall(sql);
cst.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
DBTools.release(null, cst, conn); //调用公共方法中的释放数据库资源
}
}
3.自己到数据库进行检查。