Java程序调用存储过程

[color=green][size=small]//SSH配置中方法[/size][/color]

Integer i = (Integer) this.getHibernateTemplate().execute(
new HibernateCallback() {

public Object doInHibernate(Session session)
throws HibernateException, SQLException {
CallableStatement cs = session.connection()
.prepareCall("{call call_info(?)}");
cs.setInt(1, 0);
int i = cs.executeUpdate();
return i;
}

});
// Call a procedure with no parameters
{
CallableStatement procnone = conn.prepareCall ("begin procnone; end;");
procnone.execute ();
System.out.println("\ndump table regions after calling to procnone--"
+ "insert 101 Africa\n");
dumpTable (conn);
procnone.close();
}



// Call a procedure with an IN parameter
{
CallableStatement procin = conn.prepareCall ("begin procin (?, ?); end;");
procin.setInt (1, 303);
procin.setString (2, "Australia");
procin.execute ();
System.out.println("\ndump table regions after calling to procin--"
+ "insert 303 Australia\n");
dumpTable (conn);
procin.close();
}

// Call a procedure with an OUT parameter
{
CallableStatement procout = conn.prepareCall ("begin procout (?, ?); end;");
procout.registerOutParameter (1, Types.INTEGER);
procout.registerOutParameter (2, Types.CHAR);
procout.execute ();
System.out.println ("\nOut argument to procout in PL/SQL block is:\n\t" +
procout.getInt (1) + " " + procout.getString (2));
procout.close();
}

// Call a procedure with an IN/OUT prameter
// This will insert "404, North Pole" instead of "303, Mars" that
// are in PL/SQL block of procedure procinout into table regions
{
CallableStatement procinout = conn.prepareCall ("begin procinout (?, ?); end;");
procinout.registerOutParameter (1, Types.INTEGER);
procinout.registerOutParameter (2, Types.VARCHAR);
procinout.setInt (1, 404);
procinout.setString (2, "North Pole");
procinout.execute ();
System.out.println ("\ndump table regions after calling to procinout--"
+ "insert 404 North Pole\n");
dumpTable (conn);
System.out.println ("Out argument in PL/SQL block definition of procinout is:\n\t" +
procinout.getInt (1) + " " + procinout.getString (2));
procinout.close();
}

// Call a function with no parameters
{
CallableStatement funcnone = conn.prepareCall ("begin ? := funcnone; end;");
funcnone.registerOutParameter (1, Types.CHAR);
funcnone.execute ();
System.out.println ("\nReturn value of funcnone is: " + funcnone.getString (1));
funcnone.close();
}

// Call a function with an IN parameter
{
CallableStatement funcin = conn.prepareCall ("begin ? := funcin (?); end;");
funcin.registerOutParameter (1, Types.CHAR);
funcin.setString (2, "testing");
funcin.execute ();
System.out.println ("\nReturn value of funcin is: " + funcin.getString (1));
funcin.close();
}

// Call a function with an OUT parameter
{
CallableStatement funcout = conn.prepareCall ("begin ? := funcout (?); end;");
funcout.registerOutParameter (1, Types.CHAR);
funcout.registerOutParameter (2, Types.CHAR);
funcout.execute ();
System.out.println ("\nReturn value of funcout is: " + funcout.getString (1));
System.out.println ("Out argument is: " + funcout.getString (2));
funcout.close();
}

[color=green][size=medium]//本人项目例子[/size][/color]

/**
* 月签到统计
*
* @return
*/
public String countRollcall() {
ActionContext ac = ActionContext.getContext();
CallableStatement cstmt = null;
String pmonth = (String) ac.getData("pmonth");
String lmonth = (String) ac.getData("lmonth");
String ppmonth = Integer.valueOf(pmonth) + "";
try {
CallableStatement procin = ac.getConnection().prepareCall(
"begin month_rollcall(?); end;");
procin.setString(1, lmonth);
procin.execute();
procin.close();

ac.setData("goto", "SYS_MONTH_ROLLCALL_LIST.do?pmonth=" + ppmonth);
} catch (SQLException e) {
e.printStackTrace();
}
return "success";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值