无法将类型为java.util.Date的17/04/16 00:00转换为类java.sql.Date

时间:2016-04-24 19:25:56

标签: spring jdbc

我正在尝试从我的数据库恢复数据列表,这取决于日期“datJourCchn”;

我找不到错误在哪里; PS:我将Date导入更改为java.sql.Date而不是java.util.Date

这是我的代码:

CoursChangeDaoImpl

The method createFileModelMaker(String) is undefined for the type ModelFactory

page.xhtml

 @Override
 public List<CoursChange> getAllCoursParDate(Date datJourCchn)
 {
     System.out.println("date------------->" + datJourCchn);
     List<CoursChange> coursChanges =new ArrayList<CoursChange>();
     Devise devise =new Devise();
    Connection conn = null;
    String sql=
             "SELECT d.LIB_DEV_DEV , d.LIB_SIGL_DEV , c.DAT_JOUR_CCHN , c.COD_ETAT_CCHN , c.MONT_CABA_CCHN , c.MONT_CABC_CCHN  , c.MONT_CVBA_CCHN , c.MONT_CVBC_CCHN  ,c.TIME_CCHN ,c.ID_COURS_CHANGE   FROM COURS_CHANGE c , DEVISE d  where c.COD_DEV_DEV=d.COD_DEV_DEV and c.DAT_JOUR_CCHN=?";

    System.out.println("date------------->" + datJourCchn);

    try
     {
         conn =  dataSource.getConnection();
         PreparedStatement ps = conn.prepareStatement(sql);
         ps.setDate(1,  datJourCchn);
         ResultSet rs=ps.executeQuery();
        while(rs.next())
         { CoursChange coursChange =new CoursChange();
                coursChange.setIdCoursChange(rs.getInt("ID_COURS_CHANGE"));
                 coursChange.setCodEtatCchn(rs.getString("COD_ETAT_CCHN"));
                 coursChange.setMontCabaCchn(rs.getFloat("MONT_CABA_CCHN"));
                 coursChange.setMontCabcCchn(rs.getFloat("MONT_CABC_CCHN"));
                 coursChange.setMontCvbaCchn(rs.getFloat("MONT_CVBA_CCHN"));
                 coursChange.setMontCvbcCchn(rs.getFloat("MONT_CVBC_CCHN"));
                 devise.setLibDevDev(rs.getString("LIB_DEV_DEV"));
                 devise.setLibSiglDev(rs.getString("LIB_SIGL_DEV")); 
                 coursChange.setDatJourCchn(rs.getDate("DAT_JOUR_CCHN"));
                 coursChange.setTimeCchn(rs.getString("TIME_CCHN"));
                 System.out.println(rs.getInt("ID_COURS_CHANGE"));
                 System.out.println(rs.getString("LIB_DEV_DEV"));
                 coursChanges.add(coursChange );
         }
         rs.close();
         ps.close();
     }
     catch(Exception e)

     {



        throw new RuntimeException(e); 
     }

     finally
     {
         try
         {
             conn.close();
         }
         catch(SQLException e)
         {

         }
     }
    return  coursChanges;
    }

错误是:

<p:calendar id="button" value="#{coursChangeCtr.datJourCchn}"
            showOn="button" pattern="MM-dd-yy">

        </p:calendar>


        <p:commandButton value="submit"
            action="#{coursChangeCtr.listCoursPreparedStatement()}" update="AjoutTab" />


        <p:dataTable value="#{coursChangeCtr.listCoursPreparedStatement()}" var="cours"
            id="AjoutTab" emptyMessage="aucune info!!" rows="5"
            style="width:1000px;font-size:13px;margin-left: 25px">

1 个答案:

答案 0 :(得分:0)

而不是

ps.setDate(1,  datJourCchn);

你可以使用

ps.setDate(1,  new java.sql.Date(datJourCchn.getTime()));

或,如果datJourCchn可能是null

if (datJourCchn == null) {
    s.setNull(1, java.sql.Types.DATE);
} else {
    s.setDate(1, new java.sql.Date(datJourCchn.getTime()));
}