[英]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();
}
getFunctionColumns
is the PL/SQL package name.getFunctionColumns
名称。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.ResultSet
returned by method getFunctionColumns
contains the schema name. getFunctionColumns
方法返回的ResultSet
的第 2 列包含架构名称。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.