从Java插入到Oracle

时间:2018-11-11 14:47:19

标签: java sql oracle plsql

im对表EMPLOYEES进行插入:

EMPLOYEE_ID NUMBER(6,0)     
FIRST_NAME  VARCHAR2(20 BYTE)   
LAST_NAME   VARCHAR2(25 BYTE)
EMAIL   VARCHAR2(25 BYTE)
PHONE_NUMBER    VARCHAR2(20 BYTE)
HIRE_DATE   DATE
JOB_ID  VARCHAR2(10 BYTE)   
SALARY  NUMBER(8,2)
COMMISSION_PCT  NUMBER(2,2) 
MANAGER_ID  NUMBER(6,0)     
DEPARTMENT_ID   NUMBER(4,0)     

我从Java调用此SP:

create or replace PROCEDURE SP_EMPLOYES_INSERT 
(
  FIRST_NAME IN EMPLOYEES.EMPLOYEE_ID%TYPE 
, LAST_NAME IN EMPLOYEES.LAST_NAME%TYPE 
, EMAIL IN EMPLOYEES.EMAIL%TYPE  
, PHONE_NUMBER IN EMPLOYEES.PHONE_NUMBER%TYPE  
, HIRE_DATE IN EMPLOYEES.HIRE_DATE%TYPE  
, JOB_ID IN EMPLOYEES.JOB_ID%TYPE  
, SALARY IN EMPLOYEES.SALARY%TYPE 
, COMMISSION_PCT IN EMPLOYEES.COMMISSION_PCT%TYPE  
, MANAGER_ID IN EMPLOYEES.MANAGER_ID%TYPE  
, DEPARTMENT_ID IN EMPLOYEES.DEPARTMENT_ID%TYPE  
, C_EMPLOYE OUT SYS_REFCURSOR
) IS
EMP_ID EMPLOYEES.EMPLOYEE_ID%TYPE;
MENSAJE VARCHAR2(1000);
BEGIN

 SELECT (NVL(MAX(EMPLOYEE_ID),0) + 1) INTO EMP_ID FROM EMPLOYEES;
 INSERT INTO EMPLOYEES(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID)
                VALUES(EMP_ID     ,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID);
  MENSAJE:= 'Se inserto con exito';

  OPEN C_EMPLOYE FOR
   SELECT MENSAJE MSG FROM DUAL;

 EXCEPTION
 WHEN OTHERS THEN
 MENSAJE:= SQLERRM;
   OPEN C_EMPLOYE FOR
   SELECT MENSAJE MSG FROM DUAL;
   --DBMS_OUTPUT.PUT_LINE(SQLERRM);

END SP_EMPLOYES_INSERT;

而Java中的代码是:

   public boolean PutEmployee(int action) throws SQLException
    {
        boolean result = false;
        Connection con = null;
        CallableStatement stmt = null;
        ResultSet rs = null;
        String mensaje = null;

        try 
        {
            if(!validateEmployee(action))
                return false;

            con = OracleConnection.getDBConnection();
              if(action == 1)
                  stmt = con.prepareCall("call SP_EMPLOYES_INSERT(?,?,?,?,?,?,?,?,?,?,?)");
              else if(action == 2)
                  stmt = con.prepareCall("call SP_EMPLOYEES_UPDATE(?,?,?,?,?,?,?,?,?,?,?)");
              else if(action == 3)
                  stmt = con.prepareCall("call SP_EMPLOYEES_DELETE(?)");
              else  if(action == 4)
                  stmt = con.prepareCall("call SP_EMPLOYEES_SELECT_BY_ID(?)");

            if(action == 2)
                stmt.setInt(11, objEMPLOYE.getEmployeeId());
            else if( action == 3 || action == 4)
               stmt.setInt(1, objEMPLOYE.getEmployeeId());

            if(action == 1 || action == 2)
            {
                if(action == 1)
                  stmt.registerOutParameter(11, OracleTypes.CURSOR);

                stmt.setString(1, objEMPLOYE.getFirstName());
                stmt.setString(2, objEMPLOYE.getLastName());
                stmt.setString(3, objEMPLOYE.getPhoneNumber());
                stmt.setString(4, objEMPLOYE.getEmal());

                SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
                oracle.sql.DATE hireDate = new oracle.sql.DATE(objEMPLOYE.getHireDate());
                stmt.setObject(5, hireDate, OracleTypes.DATE);

                stmt.setString(6, objEMPLOYE.getJobId());
                stmt.setDouble(7, objEMPLOYE.getSalary());
                stmt.setDouble(8, objEMPLOYE.getCommissionPct()); 
                stmt.setInt(9,objEMPLOYE.getManagerId());
                stmt.setInt(10,objEMPLOYE.getDepartmentId());
            }

           if(action == 1)
           {
               //INSERT
               if(stmt.execute())
                 result = true;
               else
                  result = false;  
              rs = ((OracleCallableStatement)stmt).getCursor(11);
              while(rs.next())
                 mensaje = rs.getString("MSG");
           }
           else if(action == 2)
           {
               //UPDATE
               if(stmt.execute())
                 result = true;
               else
                  result = false;
           }
           else if(action == 3)
           {
               //DELETE
               if(stmt.execute())                 
                 result = true;
               else
                  result = false;
           }
           else if(action == 4)
           {
               //CONSULTA POR ID
               stmt.registerOutParameter("P_CURSOR", OracleTypes.CURSOR);
               rs = stmt.getResultSet();
               objEMPLOYE = new EMPLOYES();

               while(rs.next())
               {               
                   objEMPLOYE.setCommissionPct(rs.getDouble("COMMISSION_PCT"));
                   objEMPLOYE.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
                   objEMPLOYE.setEmail(rs.getString("EMAIL"));
                   objEMPLOYE.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
                   objEMPLOYE.setFirstName(rs.getString("FIRST_NAME"));
                   objEMPLOYE.setHireDate(rs.getDate("HIRE_DATE"));
                   objEMPLOYE.setJobId(rs.getString("JOB_ID"));
                   objEMPLOYE.setLastName(rs.getString("LAST_NAME"));
                   objEMPLOYE.setManagerId(rs.getInt("MANAGER_ID"));
                   objEMPLOYE.setPhoneNumber(rs.getString("PHONE_NUMBER"));
                   objEMPLOYE.setSalary(rs.getDouble("SALARY"));
               } 
               result = true;
           }
        }
        catch(Exception ex)
        {
          System.out.print(ex.getMessage());
          result = false;
        }
        finally
        {
            if(con!=null)
                con.close();
            if(rs !=null)
                rs.close(); 
        }
        return result;
    }

当我在callablestatement中执行执行时,我遇到了一个错误:ORA-01722:“númeronoválido”。

我不知道如何将java double转换为number(8,2),number(2,2)。 有人知道我该怎么插入吗?

我将此值放入对象employee:

          empService.objEMPLOYE.setCommissionPct(0.3);
          empService.objEMPLOYE.setDepartmentId(3);
          empService.objEMPLOYE.setEmail("asd@hotmail.com");
          empService.objEMPLOYE.setFirstName("Matias");
          empService.objEMPLOYE.setHireDate(new java.sql.Date(2018, 11, 10));
          empService.objEMPLOYE.setJobId("a1");
          empService.objEMPLOYE.setLastName("Perez");
          empService.objEMPLOYE.setManagerId(1);
          empService.objEMPLOYE.setPhoneNumber("1545659865");
          empService.objEMPLOYE.setSalary(150.3);

有什么主意吗? 谢谢!

0 个答案:

没有答案