MysqlDataTruncation:Java将SQL Date对象评估为表达式

时间:2016-03-18 12:16:17

标签: java mysql date

我需要将Date插入我的数据库,因此我将从用户收到的日期转换为SQL Date,然后尝试将日期插入数据库。但是在进程SQL中,date对象被评估为整数,因此插入失败。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String enrollNo = request.getParameter("enroll_no");
    String name = request.getParameter("name");
    String age = request.getParameter("age");

    java.util.Date date = null;
    try {
        date = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("dob"));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Date SqlDate = new Date(date.getTime());

    process(enrollNo, name, age, SqlDate);
    response.sendRedirect("listing.jsp");
}

void process(String enrollNo, String name, String age, Date date) {
    DatabaseOperations db = new DatabaseOperations("java", "root", "", "jsp");
    db.insert(enrollNo, name, age, date);
}

插入DatabaseOperations类的方法

public int insert(Object...objects) {
    int rowsAffected = 0;
    String query = "INSERT INTO " + tableName + " VALUES (";

    for(int i=0; i<objects.length-1; i++)
        query += "'" + objects[i] + "',";

    query += objects[objects.length-1] + ")";

    try {
        rowsAffected = statement.executeUpdate(query);
    } catch (SQLException e) {
        printSQLExceptions(e);
    } 
    return rowsAffected;
}

插入日期11/02/2000后出现错误

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '1987' for column 'Dob' at row 1

0 个答案:

没有答案