Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 在DAO和Servlet中将日期作为参数传递_Jsp_Servlets - Fatal编程技术网

Jsp 在DAO和Servlet中将日期作为参数传递

Jsp 在DAO和Servlet中将日期作为参数传递,jsp,servlets,Jsp,Servlets,这与我最近的问题有关 在这种情况下,我有了DAO和servlet,现在我想将date事件\ u date作为参数传递给我的查询。我在哪里可以这样做 我的servlet的一部分 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SimpleDateFormat df=new Simple

这与我最近的问题有关

在这种情况下,我有了DAO和servlet,现在我想将date事件\ u date作为参数传递给我的查询。我在哪里可以这样做

我的servlet的一部分

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

    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-DD");

    String username = request.getParameter("username");
    String name = request.getParameter("name");
    String dateto = request.getParameter("dateto");        


try
    {


        List prodlistsearch_array = this.pdtDAO.prodlistsearch(name);
        request.setAttribute("prodlistsearch_array", prodlistsearch_array);

        request.getRequestDispatcher("events_audit.jsp").forward(request, response);
    } catch (SQLException e) {
      throw new ServletException("Cannot retrieve areas", e);
    }
我刀的一部分

public List<pdtBean> prodlistsearch(String name) throws SQLException{
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;


    String querystring = "select * from mydb where name = ? and event_date between ? and ?";
    List<pdtBean> prodlistsearch_array = new ArrayList<pdtBean>();

    try {

        connection = database.getConnection();
        statement = connection.prepareStatement(querystring);
                    statement.setString(1, name);

        resultSet = statement.executeQuery();

        while (resultSet.next()) {

            pdtBean prodlistsearcharray = new pdtBean();

                prodlistsearcharray.setId(resultSet.getString("id"));
                prodlistsearcharray.setEvent_date(resultSet.getString("event_date"));
                prodlistsearcharray.setTitle(resultSet.getString("title"));

            prodlistsearch_array.add(prodlistsearcharray);
        }
    } finally {
        try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        try { statement.close(); } catch (SQLException logOrIgnore) {}
        try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    return prodlistsearch_array;

}

一个非常简单的解决方法是将另一个参数类型java.util.Date传递给DAO方法。但是,我建议用getter和setter简单地创建一个POJO和一个javabean,并使用收到的参数设置属性

将对象传递给DAO方法,如果您需要更多约束,这将非常有用。

我真的希望尝试{connection.close;}catch SQLException logOrIgnore{}只是一个伪代码!