在java中执行匿名pl/sql块并获取结果集

发布于 2024-10-19 00:14:01 字数 429 浏览 5 评论 0原文

我想执行匿名 PL/SQL 并需要获取结果集对象。我得到了可以通过在 PL/SQL 块内使用游标来完成的代码。

但 PL/SQL 块本身将以文本形式来自数据库。所以我无法编辑该 PL/SQL 块。并且它只会返回两个值,其列名始终相同。它将返回 2 列组合值的列表。

这里我给出了示例 PL/SQL。

BEGIN

RETURN 'select distinct fundname d, fundname r from <table> where condition order by 1';

EXCEPTION
   WHEN OTHERS THEN
    RETURN 'SELECT ''Not Available'' d, ''Not Available'' r FROM dual';
END;

任何回复都会非常有帮助。

I would like to execute the anonymous PL/SQL and need to get the resultset object. I got the code which can be done by using cursors inside the PL/SQL block.

But the PL/SQL block itself will come from the database as text. So I can't edit that PL/SQL block. And it will return only two values whose column names will be same always. It will return list of 2 column combination values.

Here I am giving sample PL/SQL.

BEGIN

RETURN 'select distinct fundname d, fundname r from <table> where condition order by 1';

EXCEPTION
   WHEN OTHERS THEN
    RETURN 'SELECT ''Not Available'' d, ''Not Available'' r FROM dual';
END;

Any reply will be so helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美煞众生 2024-10-26 00:14:01

下面是一个自包含示例,说明如何“执行匿名 PL/SQL 并获取结果集对象”

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Types;

import oracle.jdbc.OracleTypes;

public class CallPLSQLBlockWithOneInputStringAndOneOutputStringParameterAndOneOutputCursorParameter {

    public static void main(String[] args) throws Exception {

        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        // Warning: this is a simple example program : In a long running application,
        // error handlers MUST clean up connections statements and result sets.

        final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "manager");
        String plsql = "" +
        " declare " +  
        "    p_id varchar2(20) := null; " +
        "    l_rc sys_refcursor;" +
        " begin " +
        "    p_id := ?; " +
        "    ? := 'input parameter was = ' || p_id;" +
        "    open l_rc for " +
        "        select 1 id, 'hello' name from dual " +
        "        union " +
        "        select 2, 'peter' from dual; " +
        "    ? := l_rc;" +
        " end;";

        CallableStatement cs = c.prepareCall(plsql);
        cs.setString(1, "12345");
        cs.registerOutParameter(2, Types.VARCHAR);
        cs.registerOutParameter(3, OracleTypes.CURSOR);

        cs.execute();

        System.out.println("Result = " + cs.getObject(2));

        ResultSet cursorResultSet = (ResultSet) cs.getObject(3);
        while (cursorResultSet.next ())
        {
            System.out.println (cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2));
        } 
        cs.close();
        c.close();
    }
}

上面的示例查询“select 1 id, 'hello' name from Dual union select 2, 'peter' from Dual;”可以用任何查询替换。

Here is a self contained example of how to "execute the anonymous PL/SQL and get the resultset object"

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Types;

import oracle.jdbc.OracleTypes;

public class CallPLSQLBlockWithOneInputStringAndOneOutputStringParameterAndOneOutputCursorParameter {

    public static void main(String[] args) throws Exception {

        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        // Warning: this is a simple example program : In a long running application,
        // error handlers MUST clean up connections statements and result sets.

        final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "manager");
        String plsql = "" +
        " declare " +  
        "    p_id varchar2(20) := null; " +
        "    l_rc sys_refcursor;" +
        " begin " +
        "    p_id := ?; " +
        "    ? := 'input parameter was = ' || p_id;" +
        "    open l_rc for " +
        "        select 1 id, 'hello' name from dual " +
        "        union " +
        "        select 2, 'peter' from dual; " +
        "    ? := l_rc;" +
        " end;";

        CallableStatement cs = c.prepareCall(plsql);
        cs.setString(1, "12345");
        cs.registerOutParameter(2, Types.VARCHAR);
        cs.registerOutParameter(3, OracleTypes.CURSOR);

        cs.execute();

        System.out.println("Result = " + cs.getObject(2));

        ResultSet cursorResultSet = (ResultSet) cs.getObject(3);
        while (cursorResultSet.next ())
        {
            System.out.println (cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2));
        } 
        cs.close();
        c.close();
    }
}

The above example query "select 1 id, 'hello' name from dual union select 2, 'peter' from dual;" can be replaced by any query.

心欲静而疯不止 2024-10-26 00:14:01

首先,您发布的代码无效。匿名 PL/SQL 块无法返回表达式。并且没有 PL/SQL 块可以返回这样的查询结果。您需要执行一些操作,例如声明 REF CURSOR 并使用各种 SQL 语句打开该游标。

由于匿名 PL/SQL 块无法向调用者返回任何内容,因此您描述的体系结构是有问题的。至少,您需要修改匿名块,以便有一个 JDBC 代码可以注册的绑定变量。类似于(改编自 Menon 的专家 Oracle JDBC 编程中的示例 (请注意,我可能引入了一些小的语法错误)

CallableStatement stmt := null;
ResultSet         rset := null;
String            query := 'DECLARE 
                              FUNCTION get_result
                                RETURN SYS_REFCURSOR
                              AS
                                l_rc SYS_REFCURSOR;
                              BEGIN
                                OPEN l_rc 
                                 FOR SELECT DISTINCT fundname d, fundname r
                                       FROM some_table
                                      WHERE some_condition
                                      ORDER BY 1;
                                RETURN l_rc;
                              EXCEPTION
                                WHEN others THEN
                                  OPEN l_rc 
                                   FOR SELECT 'Not Available' d, 'Not Available' r
                                         FROM dual;
                                  RETURN l_rc;
                              END get_result;
                            BEGIN
                              ? := get_result;
                            END;';
try {
  cstmt := conn.prepareCall( query );
  cstmt.registerOutParameter( 1, OracleTypes.CURSOR );
  cstmt.execute();
  rset := (ResultSet) cstmt.getObject( 1 );
}
finally {
  <<close cstmt & rset>>
}

First off, the code you posted is not valid. An anonymous PL/SQL block cannot return an expression. And no PL/SQL block can return the result of a query like that. You would need to do something like declaring a REF CURSOR and opening that cursor using the various SQL statements.

Since an anonymous PL/SQL block cannot return anything to a caller, the architecture you're describing is a problematic. At a minimum, you'd need to modify the anonymous block so that there was a bind variable that your JDBC code could register. Something like (adapted from an example in Menon's Expert Oracle JDBC Programming (note that I may have introduced some minor syntax errors)

CallableStatement stmt := null;
ResultSet         rset := null;
String            query := 'DECLARE 
                              FUNCTION get_result
                                RETURN SYS_REFCURSOR
                              AS
                                l_rc SYS_REFCURSOR;
                              BEGIN
                                OPEN l_rc 
                                 FOR SELECT DISTINCT fundname d, fundname r
                                       FROM some_table
                                      WHERE some_condition
                                      ORDER BY 1;
                                RETURN l_rc;
                              EXCEPTION
                                WHEN others THEN
                                  OPEN l_rc 
                                   FOR SELECT 'Not Available' d, 'Not Available' r
                                         FROM dual;
                                  RETURN l_rc;
                              END get_result;
                            BEGIN
                              ? := get_result;
                            END;';
try {
  cstmt := conn.prepareCall( query );
  cstmt.registerOutParameter( 1, OracleTypes.CURSOR );
  cstmt.execute();
  rset := (ResultSet) cstmt.getObject( 1 );
}
finally {
  <<close cstmt & rset>>
}
や莫失莫忘 2024-10-26 00:14:01

尝试这样的操作(伪代码):

[create or replace] function get_dataset (p_query in varchar2) return sys_refcursor
as
  l_returnvalue sys_refcursor;
begin
  open l_returnvalue for p_query;
  return l_returnvalue;
end get_dataset;

返回的 REF CURSOR 可以像普通数据集一样进行处理。

当您使用这样的方法时谨防 SQL 注入...

Try something like this (pseudo-code):

[create or replace] function get_dataset (p_query in varchar2) return sys_refcursor
as
  l_returnvalue sys_refcursor;
begin
  open l_returnvalue for p_query;
  return l_returnvalue;
end get_dataset;

The REF CURSOR which is returned can be processed like a normal dataset.

And beware of SQL injection when you use an approach like this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文