当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

如何在ADF 的Application Module中调用存储过程

发表于: 2012-11-16   作者:dana.wang   来源:转载   浏览:
摘要: 1.调用无参数的存储过程 public void callProcWithNoArgs() { getDBTransaction().executeCommand( "begin devguidepkg.proc_with_no_args; end;"); } 2.调用含入参的存储过程 protected void callStoredPro
1.调用无参数的存储过程
public void callProcWithNoArgs() {
getDBTransaction().executeCommand(
"begin devguidepkg.proc_with_no_args; end;");
}


2.调用含入参的存储过程
protected void callStoredProcedure(String stmt, Object[] bindVars) {
PreparedStatement st = null;
	try {
		// 1. Create a JDBC PreparedStatement for
		st = getDBTransaction().createPreparedStatement("begin "+stmt+";end;",0);
		if (bindVars != null) {
		// 2. Loop over values for the bind variables passed in, if any
			for (int z = 0; z < bindVars.length; z++) {
				// 3. Set the value of each bind variable in the statement
				st.setObject(z + 1, bindVars[z]);
			}
		}
		// 4. Execute the statement
		st.executeUpdate();
	}catch (SQLException e) {
		throw new JboException(e);
	}finally {
		if (st != null) {
		try {
			// 5. Close the statement
			st.close();
		}catch (SQLException e) {}
		}
	}
}
public void callProcWithThreeArgs(Number n, Date d, String v) {
	callStoredProcedure("devguidepkg.proc_with_three_args(?,?,?)",	new Object[]{n,d,v});
}



3.含入参的调用存储函数
public static int NUMBER = Types.NUMERIC;
public static int DATE = Types.DATE;
public static int VARCHAR2 = Types.VARCHAR;
protected Object callStoredFunction(int sqlReturnType, String stmt,Object[] bindVars) {
	CallableStatement st = null;
	try {
		// 1. Create a JDBC CallabledStatement
		st = getDBTransaction().createCallableStatement("begin ? := "+stmt+";end;",0);
		// 2. Register the first bind variable for the return value
		st.registerOutParameter(1, sqlReturnType);
		if (bindVars != null) {
			// 3. Loop over values for the bind variables passed in, if any
			for (int z = 0; z < bindVars.length; z++) {
				// 4. Set the value of user-supplied bind vars in the stmt
				st.setObject(z + 2, bindVars[z]);
			}
		}
		// 5. Set the value of user-supplied bind vars in the stmt
		st.executeUpdate();
		// 6. Return the value of the first bind variable
		return st.getObject(1);
	}catch (SQLException e) {
		throw new JboException(e);
	}finally {
		if (st != null) {
			try {
				// 7. Close the statement
				st.close();
			}catch (SQLException e) {}
			}
		}
	}
}

public String callFuncWithThreeArgs(Number n, Date d, String v) {
	return (String)callStoredFunction(VARCHAR2,"devguidepkg.func_with_three_args(?,?,?)",
new Object[]{n,d,v});
}

4.获取当前的数据库事务
private Connection getCurrentConnection() throws SQLException {
/* Note that we never execute this statement, so no commit really happens */
	PreparedStatement st = getDBTransaction().createPreparedStatement("commit",1);
	Connection conn = st.getConnection();
	st.close();
	return conn;
}

如何在ADF 的Application Module中调用存储过程

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号