jdbc调用oracle 函数,传入String返回CURSOR

create or replace function open_sql_cursor (v_sql varchar2) return   sys_refcursor is
 c   sys_refcursor;
 begin
    open c for v_sql;
     return c;
end;

 

JDBC调用代码

ResultSet rs1=null;
		CallableStatement call=null;
		String type="";
		String newSql ="{? = call asset.open_sql_cursor(?)}";
		try {
			conn = this.getDataSource().getConnection();
			call=conn.prepareCall(newSql);
			call.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
			call.setString(2, sql);
			call.execute();
//			rs = ((OracleCallableStatement)call).getCursor(1);
			rs=(ResultSet)call.getObject(1);			
			AuditInfo auditInfo;
			while(rs.next()){
				String errmsg = rs.getString("errmsg");
				Date loginTime = rs.getDate("loginTime");
				String dbUser = rs.getString("dbUser");
				String application = rs.getString("application");
                         。。。。。。。。。。。

   1函数是在asset账号下面 ,故有    String newSql ="{? = call asset.open_sql_cursor(?)}";

 

  2传参,从左往右,依次为1 ,2.。。。。。。

 

  3执行时使用   CallableStatement call=null;

prepareCall(newSql);

 

 4取值有两种办法

 

rs = ((OracleCallableStatement)call).getCursor(1);
			rs=(ResultSet)call.getObject(1);
 

你可能感兴趣的:(oracle,sql,C++,c,jdbc)