• 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();
                    }
                }
    
        }
  • 相关阅读:
    each和foreach的区别
    apply和call的用法
    js小知识点
    关于react的一些疑问点
    c语言字符动画的实现
    解决'chromedriver' executable needs to be in PATH问题!
    二叉树的创建和遍历
    dns和dhcp
    编写一个application程序实现如下功能:接受命令行中给出的一个字符串,先将字符串原样输出,然后判断该穿的第一个字母是否为大写,若是大写则统计该串中大写字母的个数,并将所有大写字母输出。
    学生成绩管理系统究极版
  • 原文地址:https://www.cnblogs.com/cnmotive/p/3129539.html
Copyright © 2020-2023  润新知