使用JSP与JavaBean结合JSTL标签,完成图书查询功能

1.创建数据库




2使用JDBC连接数据库



3. 创建图书实体类BookBean,封装图书信息属性


private static final long serialVersionUID = 1L;

    
    private Integer id;
    private String  name;
    private String  publish;
    private Double   price;
    private String   author;
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Book(Integer id, String name, String publish, Double price,
            String author) {
        super();
        this.id = id;
        this.name = name;
        this.publish = publish;
        this.price = price;
        this.author = author;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPublish() {
        return publish;
    }
    public void setPublish(String publish) {
        this.publish = publish;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }



4  创建数据库访问类,bookDao,在该类中封装了各种类型的对数据库访问的方法


public class BookDao {
    // type 查询类型 1-5分别全部查询 按编码查询。按名字查询。出版社 作者
    // name 查询的值
    
    
    public List<Book> findBookByBook(int type, Book book) {

        List<Book> books = new ArrayList<Book>();
        // 声明sql
        String sql = "select * from book ";
        // 构建
        Connection conn = DBcon.getCon();

    
        try {
            PreparedStatement pt = null;
            if (type == 1) {

                pt = conn.prepareStatement(sql);
            } else if (type == 2) {

                sql = sql + "where id =?";

                pt = conn.prepareStatement(sql);
                // 占位符
                pt.setInt(1, book.getId());

            } else if (type == 3) {

                sql = sql + "where name like ?";

                pt = conn.prepareStatement(sql);
                // 占位符
                pt.setString(1, book.getName() + "%");

            } else if (type == 4) {

                sql = sql + "where  publish like ?";

                pt = conn.prepareStatement(sql);
                // 占位符
                pt.setString(1, book.getPublish()+"%");

            } else if (type == 5) {

                sql = sql + "where author like ?";

                pt = conn.prepareStatement(sql);
                // 占位符
                pt.setString(1, book.getAuthor()+"%");

            }
            // 生成结果
            ResultSet rs = pt.executeQuery();

            // 填充对象
            while (rs.next()) {
                Book b = new Book();
                b.setId(rs.getInt(1));
                b.setName(rs.getString(2));
                b.setPublish(rs.getString(3));
                b.setPrice(rs.getDouble(4));
                b.setAuthor(rs.getString(5));
                books.add(b);
            }

            rs.close();
            pt.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return books;

    }
   
}


5

C创建图书查询页面(searchbook.jsp)并进行页面布局,图书查询条件封装到表单中,提交到数据处理页面




67.  在图书结果显示页面,先判断是否有图书信息。如果有,则使用列表显示;没有则显示提示信息。



public class BookServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //页面接受参数
            int type =1;
        if(request.getParameter("type")!=null){
        
             type= Integer.parseInt(request.getParameter("type"));
        }
        
        //填充一个book
        
        Book book = new Book();
        if(type==2){
              if(request.getParameter("name")!=null){
         book.setId(Integer.parseInt(request.getParameter("name")));
                  
              }
            
        }else  if( type==3){
            
            book.setName(request.getParameter("name"));
            
        }else if(type==4){
            
            book.setPublish(request.getParameter("name"));
            
        }else if(type==5){
            
            book.setAuthor(request.getParameter("name"));
            
        }
        //实例化dao 调用方法
        
        BookDao dao = new BookDao();
        List<Book>  books= dao.findBookByBook(type, book);
        //传递list到下一个页面
           request.setAttribute("books", books);     
        request.getRequestDispatcher("result.jsp").forward(request, response);
    
    }
   


7

           在数据处理页面,调用JavaBean(Book类),根据条件调用相应方法,获得符合条件的图书信息,然后封装到request中,
传递到图书结果显示页面(result.jsp 用EL表达式来实现和JSTL来实现)








 




完成:

 




  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值