一下代码是在grails框架中的Service包中书写的,所以各位在参照的时候,不能照搬 只能作为借鉴
第一种情况 存储过程没有输出:类似于
procedure XX(
p_operator_id in varchar2,--操作人员ID
p_num in VARCHAR2 --日常作业计划数
) 。。。。。。。。。。。。。。。。。
1。导Oracle驱动包(不多说了。。)
2.。获得数据库连接
def dbDriver = "oracle.jdbc.driver.OracleDriver";
def url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
def username = "xx";
def password = "xx";
Connection conn = null;
Class.forName(dbDriver);
DriverManager.setLoginTimeout(30);
3 。得到参数
CallableStatement proc=null;
conn = DriverManager.getConnection(url, username, password);
4。调用存储过程
//包名.存储名(参数)注意:有几个参数(输入,输出都包括),写几个问号
proc=conn.prepareCall("{call PKG_SQM.P_MESSAGE_SEND(?,?,?,?,?,?)}");
proc.setString(1, p_type);
proc.setString(2, p_flag);
proc.setString(3, p_person_flag);
proc.setString(4, p_user_id) ;
proc.setString(5, p_operator_id) ;
proc.setString(6, p_num);
proc.execute();
5.关闭
proc.close();
conn.close();
第一种情况 存储过程有输出:类似于
procedure YY(
p_start_date in varchar2,--开始时间 2012-12-01
p_end_date in varchar2,--结束时间 2012-12-18
p_cursor out t_cursor)。。。。
1。导Oracle驱动包(不多说了。。)
2.。获得数据库连接
def dbDriver = "oracle.jdbc.driver.OracleDriver";
def url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
def username = "xx";
def password = "xx";
Connection conn = null;
Class.forName(dbDriver);
DriverManager.setLoginTimeout(30);
3 。得到参数
CallableStatement proc=null;
conn = DriverManager.getConnection(url, username, password);
4。调用存储过程
proc = conn.prepareCall("{call PKG_XYYYCP.p_xyyycp_stat(?,?,?,?,?)}");
proc.setString(1, start_d);
proc.setString(2, end_d);
proc.setString(3, flag);
proc.setString(4, areaCode);
proc.registerOutParameter(5, oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet)proc.getObject(5);
def num_xyyycp_price="";
if(null != rs){
while(rs.next()){
num_xyyycp_price=num_xyyycp_price+","+rs.getObject("num_xyyycp_price") //存储过程要返回的字段名。如果要返回多个字段,照这样写多个就行。
}
}
5。关闭连接。
proc.close();
conn.close();
有输出的话 可以使用一个公共方法来调用。这是公共方法。 public static List<List<String>> getList(String call,String[] params){
List<List<String>> list = new ArrayList<List<String>>();
Connection con = SqlCon.getCon();//写一个公共方法获得数据库的连接。
CallableStatement proc = null;
ResultSet rs = null;
try{
proc = con.prepareCall(call);
int index = 0;
if(params != null && params.length > 0){
for (int i = 1; i <= params.length; i++) {
proc.setString(i, params[index]);
index +=1;
}
}
index = index+1;
proc.registerOutParameter(index, oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet)proc.getObject(index);
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
List<String> temp ;
String str;
while (rs.next()){
temp = new ArrayList<String>();
for(int k=1;k<=columnCount;k++){
if(rs.getObject(k)==null){
str = "";
}else{
str = rs.getObject(k).toString();
}
temp.add(str);
str = null;
}
list.add(temp);
temp = null;
}
}catch (Exception e) {
System.out.println("exec procedure exception... ...");
e.printStackTrace();
}finally{
SqlCon.closeCstmt(proc);
SqlCon.closeRst(rs);
SqlCon.closeConn(con);
}
return list;
} 在Controller中调用 String[] p = new String[6];
p[0] = p_flag;
p[1] = p_lan_code;
p[2] = p_task_type;
p[3] = p_user_type;
p[4] = p_start_date;
p[5] = p_end_date;
def list = ProcedureUtil.getList("{call PKG_REPORT.p_report_rcxj(?,?,?,?,?,?,?)}",p);
2808

被折叠的 条评论
为什么被折叠?



