2012-05-02 4 views
1
でパラメータとして渡す予定日

...これが私の最近の問題に関連しているDAOとサーブレット

私は今、私は私のパラメータとして日付(EVENT_DATE)を渡したい、私の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

私は本当に 'try {connection.close(); } catch(SQLException logOrIgnore){} 'は疑似コードです! – adarshr

+0

私はなぜ知っているかもしれませんか? – toink

答えて

0

私のDAOの

私のサーブレットの一部

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メソッドにjava.util.Dateの別の引数の型を渡すことです。しかし、POJO(getterとsetterを持つJava Bean)を作成し、受け取ったパラメータを使用してプロパティを設定することをお勧めします。

オブジェクトをDAOメソッドに渡します。これは、より多くの制約が必要な場合に役立ちます。

関連する問題