mardi 16 juin 2015

Pass a string date and select a Datetime field from a table

I want to select from my table which has a field date and it's type is DATETIME. So, in my servlet I have:

        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String timeStamp = sdf.format(dt);

and later i call a method which does the select actions.getTodayInfo(session, timeStamp);. In this method I have:

        public void getTodayInfo(HttpSession session, String date) 
            throws ClassNotFoundException, Exception{            

        String sq = "SELECT date, morning, noon, night FROM IsSELECTED WHERE date=?";

        try {       
            Class.forName(typeDB);
            c = DriverManager.getConnection(path);            
            stm = c.prepareStatement(sq);
            stm.setDate(1, date);  //<- here I have problem.
            ResultSet rs = stm.executeQuery();

            if (rs.next()) {
                do {
                    //some things
                } while(rs.next());
            } else {
                flag =true; 
            }       

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (stm != null)
                stm.close();
            if (c != null)
                c.close();
        }
        }

How can i convert the date to the proper format? I read many similar questions but I do sth wrong..

Aucun commentaire:

Enregistrer un commentaire