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

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

1、增加控件jPanel、jTable、jTextField、jTextAreaField、jButton,修改相关属性及美化(略)
 
2、鼠标点击事件
private void jt_tableMousePressed(java.awt.event.MouseEvent evt) {

        int row=jt_table.getSelectedRow();

        jt_id.setText((String)jt_table.getValueAt(row, 0));

        jt_booktypename.setText((String)jt_table.getValueAt(row, 1));

        jt_booktypedesc.setText((String)jt_table.getValueAt(row, 2));

    }

3、删除事件
  3.1删除方法

public int bookTypemodefy(Connection con,BookType bookType) throws Exception{

        String sql="update t_booktype set bookTypeName=?,bookTypeDesc=? where id=?";

        PreparedStatement pstmt=con.prepareStatement(sql);

        pstmt.setString(1, bookType.getBookTypeName());

        pstmt.setString(2, bookType.getBookTypeDesc());

        pstmt.setInt(3, bookType.getId());

        return pstmt.executeUpdate();

    }

  3.2删除点击并清空表格

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        String id=jt_id.getText();

        if(StringUtil.isEmpty(id)){

            JOptionPane.showMessageDialog(null, "请选择要删除的记录");

            return;

        }

        int n=JOptionPane.showConfirmDialog(null, "确定要删除这个记录吗?");

        if(n==0){            

            Connection con=null;

            try {

                con=dbUtil.getCon();

                int deleteNum=bookTypeDao.bookTypeDelete(con, id);

                if(deleteNum==1){

                    JOptionPane.showMessageDialog(null, "删除成功");

                    fillTable(new BookType());

                    resetValue();

                }else{

                    JOptionPane.showMessageDialog(null, "删除失败");

                }

                

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

                JOptionPane.showMessageDialog(null, "删除失败");

            }finally{

                try {

                    dbUtil.closeCon(con);

                } catch (Exception e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }

        }

    }

4修改事件

  4.1修改方法

public int bookTypemodefy(Connection con,BookType bookType) throws Exception{

        String sql="update from t_booktype set bookTypeName=?,bookTypeDesc=? where id=?";

        PreparedStatement pstmt=con.prepareStatement(sql);

        pstmt.setString(1, bookType.getBookTypeName());

        pstmt.setString(2, bookType.getBookTypeDesc());

        pstmt.setInt(0, bookType.getId());

        return pstmt.executeUpdate();

    }

  4.2修改事件

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

        String id=jt_id.getText();

        String bookTypeName=jt_booktypename.getText();

        String bookTypeDesc=jt_booktypedesc.getText();

        if (StringUtil.isEmpty(id)) {

            JOptionPane.showMessageDialog(null, "请选择要修改的记录");

            return;

        }

        BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);

            Connection con = null;

            try {

                con = dbUtil.getCon();

                int modefyNum =bookTypeDao.bookTypemodefy(con, bookType);

                if (modefyNum == 1) {

                    JOptionPane.showMessageDialog(null, "修改成功");

                    fillTable(new BookType());

                    resetValue();

                } else {

                    JOptionPane.showMessageDialog(null, "修改失败");

                }



            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

                JOptionPane.showMessageDialog(null, "修改失败");

            } finally {

                try {

                    dbUtil.closeCon(con);

                } catch (Exception e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }



    }

 

你可能感兴趣的:(swing)