在存储过程中将日期作为参数传递(oracle)

2lpgd968  于 2022-11-03  发布在  Oracle
关注(0)|答案(1)|浏览(549)

我正在尝试执行从休眠到oracle表的插入操作。我的存储过程是:-

create or replace PROCEDURE          ADMSN_TABLE (
  P_ID                            NUMBER,
  P_ADMISSIONFOR                  VARCHAR2,
  P_SUBMISSIONDATE                DATE,                            --NOT NULL
  P_SUBMISSIONLASTDATE            DATE                          --NOT NULL

)
IS
  v_sno number;

  BEGIN
      INSERT INTO ORACLE.ADMISSION(AMSNID,ADMISSIONFOR,SUBMISSIONDATE,
 SUBMISSIONLASTDATE)
      VALUES (P_ID,P_ADMISSIONFOR,P_SUBMISSIONDATE,P_SUBMISSIONLASTDATE);

      COMMIT;
  END;

我从休眠中调用这个过程的方式:

public void callProc(){

        Date utilDate= new Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        System.out.println(sqlDate);

        Date date = new Date();
        SimpleDateFormat smd = new SimpleDateFormat("dd-MMM-YYYY");
        String sdate = smd.format(date);
        System.out.println(sdate);

    try {
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    StoredProcedureQuery query = session.createStoredProcedureQuery("ADMSN_TABLE");
query.registerStoredProcedureParameter("P_ID", Number.class, 
ParameterMode.IN);
query.registerStoredProcedureParameter("P_ADMISSIONFOR", String.class, 
ParameterMode.IN);
query.registerStoredProcedureParameter("P_SUBMISSIONDATE", Date.class, 
ParameterMode.IN);             
query.registerStoredProcedureParameter("P_SUBMISSIONLASTDATE",Date.class, 
ParameterMode.IN);

            query.setParameter("P_ID", 1);
            query.setParameter("P_ADMISSIONFOR", "schoolfee");
            query.setParameter("P_SUBMISSIONDATE", sqlDate);
            query.setParameter("P_SUBMISSIONLASTDATE", sqlDate);
            query.execute();
            System.out.println("excuted");
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

现在我尝试传递日期参数为new Date()sqldate,如上面给出的。和query.setParameter("P_SUBMISSIONLASTDATE", sqlDate,TemporalType.Date);,但它们都抛出错误:-

Caused by: org.hibernate.exception.SQLGrammarException: 
Error calling CallableStatement.getMoreResults at 
org.hibernate.exception.internal.SQLStateConversionDelegate.convert
(SQLStateConversionDelegate.java:106) at 
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert
(StandardSQLExceptionConverter.java:42) at 
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert
(SqlExceptionHelper.java:111)
at org.hibernate.result.internal.OutputsImpl.convert
at org.hibernate.result.internal.OutputsImpl.<init>
(OutputsImpl.java:55)
at org.hibernate.procedure.internal.ProcedureOutputsImpl.<init>
(ProcedureOutputsImpl.java:32)
at org.hibernate.procedure.internal.ProcedureCallImpl.buildOutputs
(ProcedureCallImpl.java:453)
at org.hibernate.procedure.internal.ProcedureCallImpl.getOutputs
(ProcedureCallImpl.java:404)
    at org .hibernate.procedure.internal.ProcedureCallImpl.outputs
(ProcedureCallImpl.java:663)
    at org.hibernate.procedure.internal.ProcedureCallImpl.execute
(ProcedureCallImpl.java:646)
    ... 41 more
Caused by: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'ADMSN_TABLE'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
mzmfm0qo

mzmfm0qo1#

一种可能的解决方案是使用java.sql.timestamp,而不是java. sql. Date。此外,您还可以参考SQL和PL/SQL与Oracle和JDBC类型的Map,以验证数据类型Map是否正确。
这是一个如何获取java.sql.timestamp的示例

java.util.Date utilDate = new java.util.Date();
Calendar cal = Calendar.getInstance();
cal.setTime(utilDate);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(new java.sql.Timestamp(utilDate.getTime()));
System.out.println(new java.sql.Timestamp(cal.getTimeInMillis()));

相关问题