使用jsp,bean类和servlet在MySQL数据库中插入时间和日期

时间:2015-10-23 12:11:03

标签: java mysql jsp servlets

我想在数据库表中插入日期和时间。我在insertdao.java中出错,因为不兼容的类型时间戳无法转换为时间。 我的MySQL数据库中有一个dttable,列名为&数据类型Logindate DATE,Logintime TIMESTAMP,LogoutTime TIMESTAMP。

在index.jsp中,我们必须在Logindate,Logintime,LogoutTime

中插入值
<%@page language="java" contentType="text/html charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Timesheet Page</title>
</head>

<body>
    <header>
        <h2>WEEKLY TIME SHEET MANAGEMENT V 1.0</h2>
    </header>

    <form action="insertservlet" method="post">
        <fieldset style="width: 90%">
            <legend>Timesheet</legend>
            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Time In</th>
                        <th>Time Out</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>

                        <td><input type="date" name="logindate" required="required" /></td>
                        <td><input type="time" name="logintime" required="required" /></td>
                        <td><input type="time" name="logouttime" required="required" /></td>


                    </tr>

                </tbody>
            </table>
        </fieldset>
        <input type="submit" value="Submit">
    </form>
    <a href="logout.jsp">Logout</a>
</body>

</html> 

Bean.java:

package com.eis.bean;

import java.sql.Date;
import java.sql.Timestamp;

public class Bean {

    public Bean() {}

    private java.sql.Date logindate;
    private java.sql.Timestamp logintime;
    private java.sql.Timestamp logouttime;

    public Date getLogindate() {
        return logindate;
    }

    public void setLogindate(Date logindate) {
        this.logindate = logindate;
    }

    public Timestamp getLogintime() {
        return logintime;
    }

    public void setLogintime(Timestamp logintime) {
        this.logintime = logintime;
    }

    public Timestamp getLogouttime() {
        return logouttime;
    }

    public void setLogouttime(Timestamp logouttime) {
        this.logouttime = logouttime;
    }

    @
    Override
    public String toString() {
        return "Bean [logindate=" + logindate + ", logintime=" + logintime + ", logouttime=" + logouttime + "]";
    }

}   

insertdao.java:

package com.eis.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.sql.Timestamp;

public class insertdao {
    public static int addBean(Bean user, String sql) throws SQLException {
        int i = 0;
        Connection conn = ConnectionProvider.getConn();
        try {

            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            // Parameters start with 1

            preparedStatement.setDate(1, new Date(user.getLogindate().getTime()));
            preparedStatement.setTimestamp(2, new Timestamp(user.getLogintime().getTime()));
            preparedStatement.setTimestamp(3, new Timestamp(user.getLogouttime().getTime()));

            i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}

insertservlet.java:

import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.eis.bean.Bean;

import com.eis.bean.insertdao;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
public class insertservlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private insertdao dao;
    public insertservlet() {
        super();
        dao = new insertdao();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Bean user = new Bean();

        try {
            Date logindate = new SimpleDateFormat("yyyy/MM/dd").parse(request.getParameter("logindate"));
            user.setLogindate((java.sql.Date) logindate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        try {
            Date logintime = new SimpleDateFormat("HH:mm").parse(request.getParameter("logintime"));
            user.setLogintime((java.sql.Timestamp) logintime);
        } catch (ParseException ex) {
            Logger.getLogger(insertservlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            Date logouttime = new SimpleDateFormat("HH:mm").parse(request.getParameter("logouttime"));
            user.setLogouttime((java.sql.Timestamp) logouttime);
        } catch (ParseException ex) {
            Logger.getLogger(insertservlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        insertdao dao = new insertdao();
        String sql = "INSERT INTO dttable (LoginDate, LoginTime, LogoutTime) VALUES (?, ?, ?)";
        try {
            int i = insertdao.addBean(user, sql);

            if (i != 0) {
                out.print("<p style=\"color:Green\">Record saved successfully!!</p>");
                RequestDispatcher rd = request.getRequestDispatcher("Sucsess.jsp");
                rd.forward(request, response);
            } else {
                out.print("<p style=\"color:red\">**Record cannot be saved!**</p>");
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                rd.include(request, response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @
    Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }@
    Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }@
    Override
    public String getServletInfo() {
        return "Short description";
    }

}

如果我的代码不正确,请查看我的代码并提供帮助。

1 个答案:

答案 0 :(得分:0)

尝试setTimestamp(int parameterIndex, java.sql.Timestamp x)

     preparedStatement.setTimestamp(2, new Timestamp(user.getLogintime().getTimes()));
     preparedStatement.setTimestamp(3, new Timestamp(user.getLogouttime().getTime()));