由于前边有写java代码调用数据库,感觉应该把java调用存储过程也写一下,所以笔者补充该篇!
package testSpring; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import oracle.jdbc.OracleTypes; public class Test1 { public static void main(String[] args) { Connection con=null; ResultSet res=null; CallableStatement call=null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url="jdbc:oracle:thin:@192.168.224.87:1523:orcl"; String userName="gb_report"; String passWord="gb_report123$"; System.out.println("连接数据库成功!"); con=DriverManager.getConnection(url,userName,passWord); call=con.prepareCall("{call P_COMMON.QUERY_PROVINCE_PAGE(?,?,?,?,?,?,?,?,?,?,?)}");//P_COMMON为包名,QUERY_PROVINCE_PAGE为存储过程名,?代表参数 call.registerOutParameter(4, OracleTypes.NUMBER);//参数下标为4及返回值的类型 call.registerOutParameter(5, OracleTypes.NUMBER); call.registerOutParameter(7, OracleTypes.NUMBER); call.registerOutParameter(8, OracleTypes.INTEGER); call.registerOutParameter(9, OracleTypes.VARCHAR); call.registerOutParameter(10,OracleTypes.VARCHAR); call.registerOutParameter(11,OracleTypes.CURSOR);//注意这里返回值类型为游标 call.setString(1, "");//参数下标为1,输入参数类型为varchar(java对应string) call.setString(2, ""); call.setString(3, ""); call.setString(6, ""); //执行 call.execute(); res = (ResultSet)call.getObject(11);//接受参数下标为11的返回值为游标类型,用ResultSet while(res.next()){ System.out.println("行政ID:"+res.getString("ITEMID")+";省份名称:"+res.getString("ITEMNAME")+";"); } } catch (Exception e) { e.printStackTrace(); }finally{ try { if(res!=null){ res.close(); }else if(call!=null){ call.close(); }else if (con!=null) { con.close(); } System.out.println("关闭连接!"); } catch (Exception e2) { e2.printStackTrace(); } } } }