使用date数据类型将日期插入mysql

时间:2013-12-16 07:05:54

标签: java mysql

我正在尝试将date插入mysql。该字段为date type 但是当我从date中选择一个datepicker并将其插入数据库时​​,它会随机date ...而不是问题所在。 代码如下:

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

0 个答案:

没有答案