データベースで関数またはストアド プロシージャを使用しSYS_REFCURSOR
て、Java からこれらを返し、呼び出すことをお勧めします。CallableStatement
Oracle データベースを使用している場合は、次のことを試すことができます。
データベース機能
CREATE OR REPLACE FUNCTION my_func (p_deptno IN number,p_emp_no IN varchar2)
RETURN SYS_REFCURSOR
AS
p_cursor SYS_REFCURSOR;
BEGIN
OPEN p_cursor FOR
select *
from emp
where deptno = p_deptno and emp_number=p_emp_no;
RETURN p_cursor;
END;
/
ジャワ
callablestatement =
connection.prepareCall("begin ? :=my_func(?,?); end;");
callablestatement.registerOutParameter(1, OracleTypes.CURSOR);
callablestatement.setString(2, param);
callablestatement.setString(3, param);
callablestatement.execute();
resultSet = ((OracleCallableStatement)callablestatement).getCursor(1);
このアプローチを使用すると、Java での SQL ステートメントのハード コーディングを回避できます。