我正在使用JDBC CallableStatement调用具有命名参数的过程。
步骤:
create or replace PROCEDURE VIST_COMP(I_STUDY IN VARCHAR2, I_STARTDATE IN VARCHAR2, I_ENDDATE IN VARCHAR2, I_EMPLOYEE IN VARCHAR2, I_EMPLOYEE_OPTIONAL IN NUMBER, I_SPONSOR IN VARCHAR2, I_SPONSOR_OPTIONAL IN NUMBER, p_rc OUT SYS_REFCURSOR)
AS
o_cursor SYS_REFCURSOR;
sqlqry clob;
BEGIN
//procedure code
END VIST_COMP;
Java代码:
String runSP = "call VIST_COMP(:protocols,:alertStartDate,:alertEndDate,:employee,:employeeOptional,:sponsor,:sponsorOptional, :out_cursor)";
try (Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:localhost:389/oracle", "username", "password");
CallableStatement callableStatement = conn.prepareCall(runSP)) {
callableStatement.setString("protocols","12345");
callableStatement.setString("alertStartDate", "03-Apr-2019");
callableStatement.setString("alertEndDate", "03-Jun-2019");
callableStatement.setString("employee",null);
callableStatement.setInt("employeeOptional",1);
callableStatement.setString("sponsor", "abc");
callableStatement.setInt("sponsorOptional",0);
callableStatement.registerOutParameter("out_cursor", OracleTypes.CURSOR);
// run it
callableStatement.execute();
ResultSet rset = (ResultSet) callableStatement.getObject("out_cursor");
while(rset.next()) {
System.out.println(rset.getString("LABEL_VALUE"));
}
}catch(Exception e) {
e.printStackTrace();
}
上面的代码运行良好,我也收到了ResultSet。但是问题是,如果我更改设置参数的顺序,它将不起作用。例如,如果我将sponsor
设置为高于日期,例如以下给出错误。
callableStatement.setString("protocols","12345");
callableStatement.setString("alertStartDate", "03-Apr-2019");
callableStatement.setString("sponsor", "abc");
callableStatement.setString("alertEndDate", "03-Jun-2019");
callableStatement.setString("employee",null);
callableStatement.setInt("employeeOptional",1);
callableStatement.setInt("sponsorOptional",0);
DB错误是
ORA-01858:在一个数字为预期
ORA-06512:位于“ VIST_COMP”,行266
使用构建路径Java 1.7(已安装JDK 1.8),Oracle 12c,Ojdbc6.jar。>>
我正在使用JDBC CallableStatement调用带有命名参数的过程。过程:创建或替换PROCEDURE VIST_COMP(I_STUDY IN VARCHAR2,I_STARTDATE IN VARCHAR2,I_ENDDATE IN VARCHAR2,...
如果要使用命名参数来调用Oracle存储过程,可以考虑使用morejdbc。您的通话将如下所示: