0

hello everyone..
im trying to insert date into mysql..the field is of date type
bt when i select a date from datepicker and insert it into database,it takes a random date..not getting where the problem is..
the code is as follow

   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    java.sql.Date d = null;

    Date parsed = null;
    try {
        parsed = format.parse(request.getParameter("dt"));
        System.out.println(parsed);
        if (parsed != null) {
            d = new java.sql.Date(parsed.getTime()); //parsed.getTime(
            System.out.println(d);
        } else {

            d = new java.sql.Date(new Date().getTime());
        }
    } catch (ParseException e1) {
        e1.printStackTrace();
    }

    String nature = request.getParameter("call_nature");
    String name = request.getParameter("c_name");
    String cat = request.getParameter("call").toString();
    String num = request.getParameter("phone_no");
    String street = request.getParameter("streetno").toString();
    String rbut = request.getParameter("c_room");
    String val = request.getParameter("hidd");

    String zone = request.getParameter("combo1").toString();
    String div = request.getParameter("combo2");
    String hrs = request.getParameter("hr1");
    String mns = request.getParameter("mn1");
    String am = request.getParameter("ap1");
    String occup = request.getParameter("occu");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionurl = "jdbc:mysql://localhost:3306/fms";
        String user = "root";
        String pass = "root";
        Connection con = DriverManager.getConnection(connectionurl, user, pass);

        String sql = "insert into fire_reg values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement prst = con.prepareStatement(sql);
        prst.setString(1, nature);
        prst.setString(2, cat);
        prst.setString(3, name);
        prst.setString(4, num);

        prst.setString(5, val);
        prst.setString(6, street);
        prst.setDate(7, d);
        prst.setString(8, rbut);
        prst.setString(9, zone);
        prst.setString(10, div);
        prst.setString(11, hrs);
        prst.setString(12, mns);
        prst.setString(13, am);
        prst.setString(14, occup);

        if (prst.executeUpdate() == 1) {
            request.setAttribute("loc", street);
            //System.out.println(street);
            request.setAttribute("phone", num);
            request.setAttribute("calltypee", cat);
            request.setAttribute("zonee", zone);

            RequestDispatcher rd = request.getRequestDispatcher("FMS14_DelhiRegMap.jsp");
            rd.forward(request, response);
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}


pls help..thanks
2
Contributors
1
Reply
12
Views
3 Years
Discussion Span
Last Post by pritaeas
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.