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

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

 1、在BookManageInterFrm添加修改删除等控件,修改属性并美化(略)

 2、在BookDao删除方法

/**
     * 删除数据
     * @param con
     * @param id
     * @return int
     * @throws Exception
     */
    public int bookDelete(Connection con,String id) throws Exception{
        String sql="delete from t_book where id=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1,id);
        return pstmt.executeUpdate();
    }

 3、鼠标点击事件

/**
     * 鼠标事件
     * @param evt
     */
    private void bookTableMousePressed(java.awt.event.MouseEvent evt) {
        //获取行数
        int row = bookTable.getSelectedRow();

        //设置数据
        idTxt.setText((Integer) bookTable.getValueAt(row, 0) + "");
        bookNameTxt.setText((String) bookTable.getValueAt(row, 1));
        authorTxt.setText((String) bookTable.getValueAt(row, 2));
        String sex = (String) bookTable.getValueAt(row, 3);
        if ("男".equals(sex)) {
            jrb_man.setSelected(true);
        } else if ("女".equals(sex)) {
            jrb_female.setSelected(true);
        }
        priceTxt.setText((Float) bookTable.getValueAt(row, 4) + "");
        bookDescTxt.setText((String) bookTable.getValueAt(row, 5));
        String bookTypeName = (String) bookTable.getValueAt(row, 6);
        int n = jcb_bookType.getItemCount();
        for (int i = 0; i < n; i++) {
            BookType item = (BookType) jcb_bookType.getItemAt(i);
            if (item.getBookTypeName().equals(bookTypeName)) {
                jcb_bookType.setSelectedIndex(i);
                break;
            }
        }
    }

 4、初始化及修改BookType下拉框

    //设置默认值
    jrb_man.setSelected(true);
/**
     * 添加s_jcbBookType下拉列表框
     */
    private void fillBookType(String type) {
        BookType bookType = null;
        Connection con = null;
        try {
            con = dbUtil.getCon();
            ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
            if ("search".equals(type)) {
                bookType = new BookType();
                bookType.setBookTypeName("请选择...");
                bookType.setId(-1);
                s_jcbBookType.addItem(bookType);
            }
            while (rs.next()) {
                bookType = new BookType();
                bookType.setId(rs.getInt("id"));
                bookType.setBookTypeName(rs.getString("bookTypeName"));
                if ("search".equals(type)) {
                    s_jcbBookType.addItem(bookType);
                }
                if ("modify".equals(type)) {
                    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();
            }
        }

    }

 5、重置方法

/**
     * 重置
     */
    public void resetValue() {
        idTxt.setText("");
        bookNameTxt.setText("");
        authorTxt.setText("");
        jrb_man.setSelected(true);
        priceTxt.setText("");
        bookDescTxt.setText("");
        fillBookType("modify");
    }

  6、在BookDao修改方法

/**
     * 修改数据
     * @param con
     * @param book
     * @return int
     * @throws Exception
     */
    public int bookModefy(Connection con,Book book) throws Exception{        
        String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, book.getBookName());
        pstmt.setString(2, book.getAuthor());
        pstmt.setString(3, book.getSex());
        pstmt.setFloat(4, book.getPrice());
        pstmt.setString(5, book.getBookDesc());
        pstmt.setInt(6, book.getBookTypeId());
        pstmt.setInt(7, book.getId());
        return pstmt.executeUpdate();
    }

  7、在BookDao判断是否有关联数据

/**
     * 判断是否有关联数据
     * @param con
     * @param bookTypeId
     * @return boolean
     * @throws Exception
     */
    public boolean getBookByBookTypeId(Connection con,String bookTypeId)throws Exception{
        String sql="select * from t_book where bookTypeId=?";    
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, bookTypeId);
        ResultSet rs=pstmt.executeQuery();
        return rs.next();
    }
原文地址:https://www.cnblogs.com/cnmotive/p/3131389.html