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

时间:2017-07-19 02:22:27

标签: java oracle hibernate stored-procedures

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

pt

我从hibernate调用这个过程的方式: -

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);

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是使用java.sql.timestamp而不是java.sql.Date。您也可以参考SQL and PL/SQL Mappings to Oracle and JDBC Types以验证正确的数据类型映射。

This is an example of how get 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()));