Javaプログラム呼び出しストアド・プロシージャ

5246 ワード

///SSH構成中の方法

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("
dump table regions after calling to procnone--" + "insert 101 Africa
"); 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("
dump table regions after calling to procin--" + "insert 303 Australia
"); 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 ("
Out argument to procout in PL/SQL block is:
\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 ("
dump table regions after calling to procinout--" + "insert 404 North Pole
"); dumpTable (conn); System.out.println ("Out argument in PL/SQL block definition of procinout is:
\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 ("
Return 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 ("
Return 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 ("
Return value of funcout is: " + funcout.getString (1)); System.out.println ("Out argument is: " + funcout.getString (2)); funcout.close(); }

//本人項目例

/**
 *      
 * 
 * @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";
	}