我正在尝试调用以下 PL/SQL 过程,该过程将用户定义的记录类型作为 IN
参数。
-- User Defined Record
TYPE EMP_REC IS RECORD
(
id employees.employee_id%type,
name employees.last_name%type,
dept_name departments.department_name%type,
job_title jobs.job_title%type,
salary employees.salary%type,
manager_id employees.employee_id%type,
city locations.city%type,
phone employees.phone_number%type
);
这是用户定义记录的定义:
-- PURPOSE: Prints all employee information from the employee record
-- Example Of: PROCEDURE that takes in a parameter of RECORD type
PROCEDURE print_employee_all_details(empl1 emp_rec , emp_rec_string OUT VARCHAR2)
我正在查看 Oracle JDBC Documentation这表明 JDBC 不支持像 RECORDS 这样的复合类型:
我在网上搜索到 this link
这是我试图将用户定义的记录传递给 PL/SQL 过程的代码:
public String printEmployeeAllDetails() {
Connection conn = null;
CallableStatement callStmt = null;
String empDetails = null;
try {
// Register the Jdbc Driver
// Class.forName(JDBC_DRIVER_ORACLE);
// Create a Database Connection
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
// Create a query string
String callProc = "{call HR.EMP_PKG.print_employee_all_details( ? , ?) }";
// Create a Callable Statement
callStmt = conn.prepareCall(callProc);
// Create descriptor for the Oracle Record type "EMP_REC" required
StructDescriptor recDescriptor = StructDescriptor.createDescriptor("EMP_REC", conn);
// Stage values for each field in the Oracle record in an array
Object[] javaEmpRec = new Object[8];
// Populate those values in the Array
javaEmpRec[0] = 100;
javaEmpRec[1] = "Joe Matthew";
javaEmpRec[2] = "IT";
javaEmpRec[3] = "Senior Consultant";
javaEmpRec[4] = 20000;
javaEmpRec[5] = 101;
javaEmpRec[6] = "lombard";
javaEmpRec[7] = "222333444";
// Cast the java array into the oracle record type
STRUCT oracleEmpRec = new STRUCT(recDescriptor, conn, javaEmpRec);
// Bind Values to the IN parameter
callStmt.setObject(1, oracleEmpRec);
// Register OUT parameter
callStmt.registerOutParameter(2, java.sql.Types.VARCHAR);
// Execute the Callable Statement
callStmt.execute();
// Retrieve the value from the OUT parameter
empDetails = callStmt.getString(2);
System.out.println("Emp Details: " + empDetails);
} catch (SQLException se) {
System.out.println("Exception occured in the database");
System.out.println("Exception message: " + se.getMessage());
System.out.println("Database error code: " + se.getErrorCode());
se.printStackTrace();
} finally {
// Clean up
if (callStmt != null) {
try {
callStmt.close();
} catch (SQLException se2) {
se2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException se2) {
se2.printStackTrace();
}
}
}
return empDetails;
}
运行此代码时出现以下异常:
Exception occured in the database
Exception message: invalid name pattern: HR.EMP_REC
java.sql.SQLException: invalid name pattern: HR.EMP_REC
at oracle.jdbc.oracore.OracleTypeADT.initMetadata(OracleTypeADT.java:554)
at oracle.jdbc.oracore.OracleTypeADT.init(OracleTypeADT.java:471)
at oracle.sql.StructDescriptor.initPickler(StructDescriptor.java:324)
at oracle.sql.StructDescriptor.<init>(StructDescriptor.java:254)
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:135)
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:103)
Database error code: 17074
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:72)
at com.rolta.HrManager.printEmployeeAllDetails(HrManager.java:1214)
at com.rolta.HrManager.main(HrManager.java:1334)
我正在使用 ojdbc6.jar
JDBC Thin for All Platforms
标题下的第一个 jar for Oracle Database 11g Release 2 (11.2.0.4) JDBC驱动程序
on this page .
我想知道是否允许将用户定义的记录(作为 IN 参数)传递给 PL/SQL 过程?有没有人尝试过上述操作?
最佳答案
是的,允许使用 JDBC 将用户定义的数据类型作为 IN 参数传递。但它不能是 RECORD
。它必须是模式级别的对象,例如
CREATE TYPE EMP_REC AS OBJECT
(
id employees.employee_id%type,
name employees.last_name%type,
dept_name departments.department_name%type,
job_title jobs.job_title%type,
salary employees.salary%type,
manager_id employees.employee_id%type,
city locations.city%type,
phone employees.phone_number%type
);
在您的 PL/SQL 中,您可以将对记录的引用更改为您的新对象类型,或者您可以编写一个快速的小翻译函数来将对象类型转换为记录类型,如果您不能更改其余的代码。
关于java - 使用 JDBC 以用户定义的记录作为其 IN 参数调用 PL/SQL 过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113075/