Swing入门级项目全程实录第7讲

 惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

1、添加BookManageInterFrm窗口、控件及美化(略)
 
2、在MainFrm中添加链接BookManageInterFrm窗口代码
//添加图书维护窗口 
        BookManageInterFrm BookManageInterFrm = new BookManageInterFrm();
        BookManageInterFrm.setVisible(true);
        table.add(BookManageInterFrm);

 3、添加下拉框数据

/**
     * 添加jcb_BookType下拉列表框
     */
    private void fillBookType() {
        BookType bookType = null;
        Connection con = null;
        try {
            con = dbUtil.getCon();
            ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());

            while (rs.next()) {
                bookType = new BookType();
                bookType.setId(rs.getInt("id"));
                bookType.setBookTypeName(rs.getString("bookTypeName"));
                jcb_bookType.addItem(bookType);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

4、在BookModel中增加关联查询顶bookTypeName,并增加set、get方法,设置bookTypeId默认值为-1,方便查询

/**
     * 新增数据库查询项
     */
    /**
     * @return the bookTypeName
     */
    public String getBookTypeName() {
        return bookTypeName;
    }
        
    /**
     * @param bookTypeName the bookTypeName to set
     */
    public void setBookTypeName(String bookTypeName) {
        this.bookTypeName = bookTypeName;
    }
private int bookTypeId=-1;

 5、编写多条件关联查询语句

/**
     * 多条件关联查询
     * @param con
     * @param book
     * @return resultset
     * @throws Exception
     */
    public ResultSet bookList(Connection con,Book book) throws Exception{
        StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId = bt.id");
        if(StringUtil.isNotEmpty(book.getBookName())){
            sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
        }
        if(StringUtil.isNotEmpty(book.getAuthor())){
            sb.append(" and b.author like '%"+book.getAuthor()+"%'");
        }
        if(StringUtil.isNotEmpty(book.getSex())){
            sb.append(" and b.sex = '"+book.getSex()+"'");
        }
        if(book.getBookTypeId()!=-1){
            sb.append(" and b.bookTypeId = "+book.getBookTypeId());
        }
        PreparedStatement pstmt=con.prepareStatement(sb.toString());
        return pstmt.executeQuery();
    }

6、填充数据表格

    /**
     * 添加表格数据
     * @param bookType
     */
    private void filltable(Book book) {
        //获取表格模型
        DefaultTableModel dtm = (DefaultTableModel) bookTable.getModel();

        //连接数据库添加数据
        Connection con = null;
        dtm.setRowCount(0);
        try {
            con = dbUtil.getCon();
            ResultSet rs = bookDao.bookList(con, book);
            while (rs.next()) {
                Vector v = new Vector();
                v.add(rs.getString("id"));
                v.add(rs.getString("bookTypeName"));
                v.add(rs.getString("author"));
                v.add(rs.getString("sex"));
                v.add(rs.getString("price"));
                v.add(rs.getString("bookDesc"));
                v.add(rs.getString("bookTypeName"));
                dtm.addRow(v);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

  7、条件查询 

/**
     * 查询
     */
    private void s_searchActionPerformed(java.awt.event.ActionEvent evt) {
        //获取数据
        String bookName=s_bookNameTxt.getText();
        String author=s_authorTxt.getText();
        String sex="";
        if(s_jrbman.isSelected()){
            sex="男";
        }
        if(s_jrbmefale.isSelected()){
            sex="女";
        }
        BookType bookType=(BookType) s_jcbBookType.getSelectedItem();
        int bookTypeId=bookType.getId();
        
        //封装
        Book book=new Book(bookName,author,sex,bookTypeId);
        
        //填充表格
        filltable(book);
    }

 

posted @ 2013-06-10 23:21  cnmotive  阅读(216)  评论(0编辑  收藏  举报