1

I'm trying to recuperate a list of data from my DB that depends on the date "datJourCchn" ;

I cloudn't find where is the error; PS: I changed the Date imports to java.sql.Date instead of java.util.Date

here is my code:

CoursChangeDaoImpl

 @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;
    }

page.xhtml

<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">

the error is:

Cannot convert 17/04/16 00:00 of type class java.util.Date to class java.sql.Date
khawla belgacem
  • 77
  • 1
  • 4
  • 9
  • 1
    Possible duplicate of [Cannot convert 4/23/12 12:00 AM of type class java.util.Date to class java.sql.Date](http://stackoverflow.com/questions/10286724/cannot-convert-4-23-12-1200-am-of-type-class-java-util-date-to-class-java-sql-d) – Aritz Apr 24 '16 at 19:29
  • I guess JDBC expects a `java.util.Date` instead of `java.sql.Date`: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getDate%28int%29 – Aritz Apr 24 '16 at 19:32
  • at first , the imports were all in java.util.Date; but that doesn't work, that's why I changed it to java.sql.Date :/ – khawla belgacem Apr 24 '16 at 19:36
  • and preparedStatement doesn't support java.util.Date, – khawla belgacem Apr 24 '16 at 19:37
  • can u help me more please !!! – khawla belgacem Apr 24 '16 at 20:13

1 Answers1

2

Instead of

ps.setDate(1,  datJourCchn);

you can use

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

or, if datJourCchn might be null

if (datJourCchn == null) {
    s.setNull(1, java.sql.Types.DATE);
} else {
    s.setDate(1, new java.sql.Date(datJourCchn.getTime()));
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418