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
5

1 Answer 1

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()));
}
Sign up to request clarification or add additional context in comments.

2 Comments

java.lang.NullPointerException Sir;
thank you Sir; it works perfectly ; I am grateful :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.