0

问题陈述是这样的:

我必须创建一个过程来获取Employees1 表中的所有数据(实际上只有9 条记录),然后通过在Java 中调用这个过程来显示这些数据。我正在开发一个基于 struts 1.3 的遗留应用程序,但我想这不应该是任何问题。

下面是我创建的过程:

CREATE OR REPLACE PROCEDURE getALLEmployees(ref_cur out sys_refcursor)
IS
BEGIN
OPEN ref_cur FOR 'SELECT * FROM employees1';
END;

这是我的 Java 代码:

callableStatement = conn.prepareCall("{call getAllEmployees()}");

// Below line is (EmployeeDataJdbc.java:33) as mentioned in error
callableStatement.registerOutParameter(1,oracle.jdbc.driver.OracleTypes.CURSOR); 

boolean isResultSet = callableStatement.execute(); 

        rs = (ResultSet)callableStatement.getObject(1);

        /*Employee paramater Order in DB
        employee_id, employee_first_name, employee_last_name, employee_address, 
        employee_blood_group, employee_email, employee_department, employee_role, 
        employee_band, employee_mobile_number*/

        while(rs.next()){
            emp.setEmployeeId(rs.getInt(1));
            emp.setEmployeeFirstName(rs.getString(2));
            emp.setEmployeeLastName(rs.getString(3));
            emp.setEmployeeAddress(rs.getString(4));
            emp.setEmployeeBloodGroup(rs.getString(5));
            emp.setEmployeeEmail(rs.getString(6));
            emp.setEmployeeDepartment(rs.getString(7));
            emp.setEmployeeRole(rs.getString(8));
            emp.setEmployeeBand(rs.getString(9));
            emp.setEmployeeMobileNumber(rs.getLong(10));

            employeeList.add(emp);
        }

这是我在控制台遇到的错误:

Connected to Database

java.sql.SQLException: Invalid column index
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.jdbc.driver.OracleCallableStatement.registerOutParameterInternal(OracleCallableStatement.java:121)
    at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:268)
    at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:348)
    at jdbcHandles.EmployeeDataJdbc.getEmployeeList(EmployeeDataJdbc.java:33)

我已经很久没有使用 PL-SQL 了。请帮我找出我哪里出错了。提前致谢。

注意:我使用 Oracle 10G express edition 作为数据库。

4

1 回答 1

0
  callableStatement = conn.prepareCall("{call getAllEmployees(?)}");

您必须提到必须使用绑定out参数的位置?

而且由于列数可以改变。最好使用Metadatadata 调用进行检查 rSet.getColumnCount()

于 2014-11-10T08:10:21.217 回答