这与我最近的问题有关...
在这种情况下,我有我的 DAO 和 servlet,现在我想将日期 (event_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);
}
我的 DAO 的一部分
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;
}