简体   繁体   English

如何获取自定义 Oracle function 参数定义

[英]How to get a custom Oracle function parameter definition

We need to get the Custom Function parameter list from Java jdbc.我们需要从 Java jdbc 中获取自定义 Function 参数列表。

we define the custom function in a package named mypkg .我们在名为mypkg 的 package 中定义自定义 function

I am using oracle as the database.我使用 oracle 作为数据库。

we can run command like desc mypkg.customFunction in the console.我们可以在控制台中运行类似desc mypkg.customFunction的命令。

but I want to know how to get similar information from java side.但我想知道如何从 java 方面获得类似的信息。

If you have a stored function (and not a stored procedure), then method getFunctionColumns will retrieve details of its parameters.如果您有存储的 function(而不是存储过程),则getFunctionColumns方法将检索其参数的详细信息。

Consider the following code (which uses try-with-resources ).考虑以下代码(使用try-with-resources )。

Note that there appears to be a bug in Oracle's JDBC driver where method getFunctionColumns retrieves the same parameter multiple times.请注意,Oracle 的 JDBC 驱动程序中似乎存在一个 错误,其中 getFunctionColumns 方法getFunctionColumns检索相同的参数。 The below code will display each parameter's details once only.下面的代码将只显示每个参数的详细信息一次。

More explanations after the code.代码后有更多解释。

String url = "jdbc:oracle...";
try (Connection conn = DriverManager.getConnection(url)) {
    DatabaseMetaData dbmd = conn.getMetaData();
    ResultSet rs = dbmd.getFunctionColumns("mypkg", "<schema>", "customFunction", "%");
    String lastColumn = null;
    while (rs.next()) {
        String columnName = rs.getString(4);
        if (columnName != null  &&  !columnName.equals(lastColumn)) {
            String type;
            switch (rs.getShort(5)) { // parameter type (IN, OUT or IN-OUT)
                case DatabaseMetaData.functionColumnIn:
                    type = "IN";
                    break;
                case DatabaseMetaData.functionColumnInOut:
                    type = "IN OUT";
                    break;
                case DatabaseMetaData.functionColumnOut:
                    type = "OUT";
                    break;
                default:
                    type = "unknown";
            }
            System.out.printf("%s %s %s%n",
                              rs.getString(4),  // parameter name
                              type,             // parameter type
                              rs.getString(7)); // data type length
            lastColumn = columnName;
        }
    }
}
catch (SQLException xSql) {
    xSql.printStackTrace();
}
  • I assume you know the JDBC connection URL that you need in order to connect to your Oracle database.我假设您知道连接到 Oracle 数据库所需的 JDBC 连接 URL。
  • First argument in call to method getFunctionColumns is the PL/SQL package name.调用 getFunctionColumns 方法的第一个参数是 PL/SQL getFunctionColumns名称。
  • Second argument in call to method getFunctionColumns is schema.调用方法getFunctionColumns的第二个参数是模式。 Replace <schema> in the above code with the actual schema name of the owner of mypkg .将上述代码中的<schema>替换为mypkg所有者的实际模式名称。 If you don't know the schema then you can use null.如果您不知道架构,那么您可以使用 null。 Column 2 of the ResultSet returned by method getFunctionColumns contains the schema name. getFunctionColumns方法返回的ResultSet的第 2 列包含架构名称。
  • If customFunction is a stored procedure (and not a stored function), then you need method getProcedureColumns .如果customFunction是存储过程(而不是存储函数),那么您需要方法getProcedureColumns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM