| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3675 人关注过本帖
标题:DAO模式下,怎么用servlet调用dao里面的list打印在jsp页面上
只看楼主 加入收藏
霜月逐华
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-12-29
收藏
 问题点数:0 回复次数:0 
DAO模式下,怎么用servlet调用dao里面的list打印在jsp页面上
程序代码:
       public ProductBean searchinfom(String username){
              Connection conn = null;
              PreparedStatement pstmt = null;
              ResultSet rst = null;
             // ProductBean  product = new ProductBean();
              List<ProductBean> productList=new ArrayList<ProductBean>();

              try{
                conn = dataSource.getConnection();
                pstmt = conn.prepareStatement("SELECT * FROM userproduct WHERE username like ?"); 
                pstmt.setString(1,"%"+username+"%");
                rst = pstmt.executeQuery();
                while(rst.next()){    
                    ProductBean product=new ProductBean();
                    product.setUsername(rst.getString("username"));
                    product.setProduct_id(rst.getString("product_id"));
                    product.setFaddress(rst.getString("faddress"));
                    product.setSaddress(rst.getString("saddress"));
                    product.setWeight(rst.getDouble("weight"));
                    product.setPrice(rst.getDouble("price"));
                    productList.add(product);                                    
                    //return product;
                }//else{
                  //return  null;
                //}
           
             }catch(SQLException se){
                 
                 System.out.println("SQLException:"+se.getMessage());
                 return null;
                
             }finally{
                 try{
                    conn.close();
                 }catch(SQLException se){
                 }
             }
            return null;
           }
}
程序代码:
public class QueryInformServlet extends HttpServlet{
       public void doPost(HttpServletRequest request,HttpServletResponse response)
                 throws ServletException,IOException{
        String username = request.getParameter("username");
        ProductDao productdao = new ProductDao();
        ProductBean product = productdao.searchinfom(username);
        ArrayList<ProductBean> productList=null;
          productList =new ArrayList<ProductBean>();
        //if(!productList.isEmpty()){
          
          request.getSession().setAttribute("productList",productList);
          
          RequestDispatcher view = request.getRequestDispatcher("/displayinform.jsp");
          view.forward(request, response);
       //}else{
       // RequestDispatcher view = request.getRequestDispatcher("/error.jsp");
       //   view.forward(request, response);
      //  }
      } 
    }
程序代码:
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.* ,com.bean.ProductBean"%>
<html>
<body>
<table border=1>
<tr><td>用户名</td><td>商品号</td><td>发货地</td><td>收货地</td><td>重量</td><td>价格</td></tr>
<% ArrayList<ProductBean> productList=(ArrayList<ProductBean>)session.getAttribute("productList");
for(ProductBean product:productList)
{
%>
<tr><td><%= product.getUsername() %></td>
<td><%= product.getProduct_id() %></td>
<td><%= product.getFaddress() %></td>
<td><%= product.getSaddress() %></td>
<td><%= product.getWeight() %></td>
<td><%= product.getPrice() %></td>
</tr>
<%
}
%>
</table> 
</body>
</html>


[ 本帖最后由 霜月逐华 于 2015-6-28 23:19 编辑 ]
2015-06-28 23:18
快速回复:DAO模式下,怎么用servlet调用dao里面的list打印在jsp页面上
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021428 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved