Jdbc方法返回的结果差异

时间:2014-11-19 17:45:17

标签: java debugging jdbc plsql out-parameters

我似乎无法弄清楚下面的代码有什么问题。方法getSalaryAverageDepartment调用属于get_salary_average_dept模式中EMP_PGK包的PL / SQL过程HR

    public float getSalaryAverageDepartment(int deptId)
    {
        Connection conn =  null;
        CallableStatement callStmt = null;
        Float avgDeptSal = -1f;

        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 SQL String
            String callProc = "{ call HR.EMP_PKG.get_salary_average_dept( ? , ? )}";

            // Create a callable statement
            callStmt = conn.prepareCall(callProc);

            // Bind value to the IN parameter
            callStmt.setInt(1, java.sql.Types.NUMERIC);

            // Register the OUT parameter's type to the SQL type of the return value
            callStmt.registerOutParameter(2, java.sql.Types.FLOAT);

            // Execute the callable statement
            callStmt.execute();

            // Retrieve the OUT parameter
            avgDeptSal = callStmt.getFloat(2);
            System.out.println("Department with Id : "+ deptId + " has average employee salary of : " + avgDeptSal);
        }
        catch (SQLException se) 
        {
            System.out.println("Exception occured in the database");
            System.out.println("Exception message: "+ se.getMessage());
            System.out.println("Dataabse 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 avgDeptSal;
    }

这里是调用上述代码的主要方法:

public static void main(String[] args) 
{
    HrManager hrMngr = new HrManager();

    int deptId = 80;

    hrMngr.getSalaryAverageDepartment(deptId);
}

以下是PL / SQL程序:

  -- Purpose: Returns the average salary of a department with given id
  PROCEDURE get_salary_average_dept(dept_id IN departments.department_id%type, avg_salary OUT FLOAT) IS
  BEGIN
   SELECT AVG(CAST(salary as FLOAT))
   INTO avg_salary
   FROM employees
   WHERE department_id = dept_id;
   dbms_output.put_line('Average salary for department with id : '|| dept_id ||' is : '|| avg_salary);
  EXCEPTION
   WHEN NO_DATA_FOUND THEN
   dbms_output.put_line('No department found with id: '|| dept_id);
   WHEN others THEN
   dbms_output.put_line('Error!');
  END get_salary_average_dept;

以下是java程序的输出:

Department with Id : 80 has average employee salary of : 0.0

我在Jdeveloper中独立执行了PL / SQL过程,这里有以下输出:

Connecting to the database Insight_Dev_Hr.
Average salary for department with id : 80 is : 9000
Process exited.

为什么两个输出都不同。我在这里遗漏了什么吗?

1 个答案:

答案 0 :(得分:1)

我认为你的问题在这里:

      // Bind value to the IN parameter
      callStmt.setInt(1, java.sql.Types.NUMERIC);

您将java.sql.Types.NUMERIC(2)的值绑定为存储过程的第一个参数的值。相反,你应该这样做:

      callStmt.setInt(1, deptId);