这与我最近提出的问题有关......
在这种情况下,我有我的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;
}
答案 0 :(得分:0)
一个非常简单的解决方法是将另一个参数类型java.util.Date
传递给DAO方法。但是,我建议用简单的术语创建一个POJO
(一个带有getter和setter的Java Bean),并使用收到的参数设置属性。
将对象传递给DAO
方法,如果您需要更多约束,这将非常有用。