博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java swing 的一个多表操作的例子
阅读量:5161 次
发布时间:2019-06-13

本文共 11412 字,大约阅读时间需要 38 分钟。

惯例,开头先给师傅打个广告。

多的不说,直接上代码和效果图。数据连接层代码

public int GoodsXinXi_Add(Connection conn ,GoodsJiChuXinXi yuangong){                int i=0;        String sql="insert goods_jichuxinxi value(null,?,?,?,?,?)";        try{            PreparedStatement pstm=conn.prepareStatement(sql);            pstm.setString(2, yuangong.getGoods_Name());            pstm.setInt(1, yuangong.getGoods_LeiBieID());            pstm.setString(3,String.valueOf(yuangong.getGoods_Color()) );            pstm.setString(4, yuangong.getGoods_GuiGe());            pstm.setString(5, String.valueOf(yuangong.getGoods_Date()));                    i=pstm.executeUpdate();        }        catch(Exception e){            e.printStackTrace();        }                return i;    }public ResultSet GoodsXinXi_XinXiList(Connection conn, String str,String yuangong ) throws Exception{    String sql="select*from goods_jichuxinxi jichuxinxi,tb_goodsleibie goodsleibie where jichuxinxi.Goods_LeiBieID=goodsleibie.Goods_LeiBieID";    StringBuffer sb=new  StringBuffer(sql);    PreparedStatement pstm = null;    try {        if(!"".equals(str)&&!"".equals(yuangong)){            sb.append(" and ");            sb.append(" ");            sb.append( str);            sb.append("  like '%");            sb.append(yuangong);            sb.append("%'");            pstm=conn.prepareStatement(sb.toString());            // System.out.println(sb.toString());        }        else{             pstm=conn.prepareStatement(sb.toString());        }                    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }        return pstm.executeQuery();}public ResultSet GoodsXinXi_XinXiList(Connection conn) throws Exception{    String sql="select*from tb_goodsleibie ";    PreparedStatement pstm=null;    pstm=conn.prepareStatement(sql);    return pstm.executeQuery();}public  int UpdateTable(Connection conn,int row,GoodsJiChuXinXi xinxi) throws Exception{    int updateRow=0;    String sql="update goods_jichuxinxi set Goods_LeiBieID=?,Goods_Name=?,Goods_Color=?,Goods_GuiGe=?,Goods_Date=? where Goods_JiChuID=?";    PreparedStatement pstm=null;    pstm=conn.prepareStatement(sql);    pstm.setInt(1, xinxi.getGoods_LeiBieID());    pstm.setString(2, xinxi.getGoods_Name());    pstm.setString(3, xinxi.getGoods_Color());    pstm.setString(4, xinxi.getGoods_GuiGe());    pstm.setString(5, String.valueOf(xinxi.getGoods_Date()));    pstm.setInt(6, row);    updateRow=pstm.executeUpdate();    return updateRow;    }public int delectData(Connection conn, int rowIndex) throws Exception{    int updateRow=0;    String sql="delete from tb_Client where ClientID=? ";    PreparedStatement pstm=null;    pstm=conn.prepareStatement(sql);    pstm.setInt(1, rowIndex);    updateRow=pstm.executeUpdate();    return updateRow;}

界面层代码

DBUtil dbutil = new DBUtil();    StringUtil stringutil = new StringUtil();    GoodsXinXiDao xinxidao = new GoodsXinXiDao();    /** Creates new form Goods_JiChuXinXiWeiHuFrm */    public Goods_JiChuXinXiWeiHuFrm() {        initComponents();        this.setLocation(300, 50);        String[] combox = { "", "编号", "类别名", "商品名" };        for (int i = 0; i < combox.length; i++) {            this.JcomboxTiaoJian.addItem(combox[i]);        }        String tiaojian = this.JcomboxTiaoJian.getSelectedItem().toString();        String values = this.TiaoJianValueTxt.getText();        this.FillDataTable(tiaojian, values);        this.selectType();    }    public void selectType() {        Connection conn = null;        GoodsLeiBie leibieType = null;        try {            conn = dbutil.getcon();            GoodsXinXiDao goodsdao = new GoodsXinXiDao();            ResultSet rs = goodsdao.GoodsXinXi_XinXiList(conn);            while (rs.next()) {                leibieType = new GoodsLeiBie();                leibieType.setGoodsLeiBieID(rs.getInt("Goods_LeiBieID"));                leibieType.setGoodName(rs.getString("Goods_LeiBieMing"));                this.ComBoxTxt.addItem(leibieType);            }            //this.ShangPinLeiBieTxt.addItem(leibieType);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public void FillDataTable(String tiaojian, String values) {        Connection conn = null;        Client data = null;        DefaultTableModel dtm = (DefaultTableModel) data_Table.getModel();        dtm.setRowCount(0);        try {            conn = dbutil.getcon();            ResultSet rs = xinxidao                    .GoodsXinXi_XinXiList(conn, tiaojian, values);            while (rs.next()) {                Vector v = new Vector();                v.add(rs.getString("Goods_JiChuID"));                v.add(rs.getString("Goods_Name"));                v.add(rs.getString("Goods_LeiBieMing"));                v.add(rs.getString("Goods_GuiGe"));                v.add(rs.getString("Goods_Color"));                v.add(rs.getString("Goods_Date"));                dtm.addRow(v);            }        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            try {                dbutil.closeCon(conn);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }private void Jb_resetActionPerformed(java.awt.event.ActionEvent evt) {        // TODO add your handling code here:        Reset();    }    private void Jb_deleteActionPerformed(java.awt.event.ActionEvent evt) {        // TODO add your handling code here:        int selectedRow = 0;        String selected = "";        selected = this.BianHaoTxt.getText();        ;        if ("".equals(selected)) {            JOptionPane.showMessageDialog(null, "请先选中要删除的行!");        } else {            Connection conn = null;            selectedRow = Integer.parseInt(selected);            try {                conn = dbutil.getcon();                int count = xinxidao.delectData(conn, selectedRow);                if (count != 0) {                    JOptionPane.showMessageDialog(null, "删除成功!");                    String tiaojian = JcomboxTiaoJian.getSelectedItem()                            .toString();                    String values = this.TiaoJianValueTxt.getText();                    this.FillDataTable(tiaojian, values);                    Reset();                } else {                    JOptionPane.showMessageDialog(null, "删除失败!");                }            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            } finally {                try {                    dbutil.closeCon(conn);                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }    public void Reset() {        this.BianHaoTxt.setText("");        this.NameTxt.setText("");        this.ComBoxTxt.setSelectedIndex(-1);        this.GuiGeTxt.setText("");        this.TimeTxt.setText("");    }    private void Jb_XiuGaiActionPerformed(java.awt.event.ActionEvent evt) {        // TODO add your handling code here:        String Id = this.BianHaoTxt.getText();        if ("".equals(Id)) {            JOptionPane.showMessageDialog(null, "请选中要修改的行!");        } else {            String userName = this.NameTxt.getText();            String ColorTxt = (String) this.ColorTxt.getText();            String GuiGeTxt = this.GuiGeTxt.getText();            String TimeTxt = this.TimeTxt.getText();            GoodsLeiBie bookType = (GoodsLeiBie) this.ComBoxTxt                    .getSelectedItem();            int bookTypeId = bookType.getGoodsLeiBieID();            SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd");            GoodsJiChuXinXi xinxi = null;            Timestamp ts;            try {                ts = new Timestamp(time1.parse(TimeTxt).getTime());                xinxi = new GoodsJiChuXinXi(userName, bookTypeId, GuiGeTxt,                        ColorTxt, ts);            } catch (ParseException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }            Connection conn = null;            try {                conn = dbutil.getcon();                int count = 0;                count = xinxidao.UpdateTable(conn, Integer.parseInt(Id), xinxi);                if (count != 0) {                    JOptionPane.showMessageDialog(null, "修改成功!");                    Reset();                    String tiaojian = JcomboxTiaoJian.getSelectedItem()                            .toString();                    String values = this.TiaoJianValueTxt.getText();                    this.FillDataTable(tiaojian, values);                    //this.reset();                } else {                    JOptionPane.showMessageDialog(null, "修改失败!");                }            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            } finally {                try {                    dbutil.closeCon(conn);                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }    private void data_TableMouseClicked(java.awt.event.MouseEvent evt) {        // TODO add your handling code here:        //this.reset();        int row = this.data_Table.getSelectedRow();        this.BianHaoTxt.setText((String) this.data_Table.getValueAt(row, 0));        this.NameTxt.setText((String) this.data_Table.getValueAt(row, 1));        //this.setText((String) this.data_Table.getValueAt(row, 2));        this.ColorTxt.setText((String) this.data_Table.getValueAt(row, 3));        this.GuiGeTxt.setText((String) this.data_Table.getValueAt(row, 4));        //SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd");        String time = (String) this.data_Table.getValueAt(row, 5);        this.TimeTxt.setText(time);        String bookTypeName = (String) data_Table.getValueAt(row, 2);        int n = this.ComBoxTxt.getItemCount();        for (int i = 0; i < n; i++) {            GoodsLeiBie item = (GoodsLeiBie) this.ComBoxTxt.getItemAt(i);            if (item.getGoodName().equals(bookTypeName)) {                this.ComBoxTxt.setSelectedIndex(i);            }        }    }    private void Jb_SearchActionPerformed(java.awt.event.ActionEvent evt) {        // TODO add your handling code here:        String tiaojian = JcomboxTiaoJian.getSelectedItem().toString();        String values = this.TiaoJianValueTxt.getText();        int i = 0;        String subtiaojian = null;        if ("编号".equals(tiaojian)) {            i = 1;        } else if ("类别名".equals(tiaojian)) {            i = 2;        } else if ("商品名".equals(tiaojian)) {            i = 3;        }        switch (i) {        case 1:            subtiaojian = "Goods_JiChuID";            break;        case 2:            subtiaojian = "Goods_LeiBieMing";            break;        case 3:            subtiaojian = "Goods_Name";        }        try {            this.FillDataTable(subtiaojian, values);            this.JcomboxTiaoJian.setSelectedItem("");            this.TiaoJianValueTxt.setText("");        } catch (Exception e) {            e.printStackTrace();        } finally {        }    }

效果图

转载于:https://www.cnblogs.com/liuquande/p/3156094.html

你可能感兴趣的文章
Oracle OCP 学习日志-使用转换函数和条件表达式-04
查看>>
C#:几种数据库的大数据批量插入(转)
查看>>
hdu 2196 computer 树状dp
查看>>
启用Servlet 3.0新特性——注解支持
查看>>
Python 类型的分类
查看>>
Stripies(POJ 1862 贪心)
查看>>
A Simple Math Problem(HDU 1757 构造矩阵)
查看>>
Eclipse使用External Tools定位class文件目录路径
查看>>
JS节流和防抖
查看>>
C#教程之打印和打印预览
查看>>
linux下解压命令大全
查看>>
C语言开发面试题
查看>>
VC++读写文件
查看>>
小程序-冒泡事件
查看>>
myEclipse快捷键
查看>>
数据结构练习(36)二叉树两结点的最低共同父结点
查看>>
WCF编程系列(一)初识WCF
查看>>
python远程控制电脑
查看>>
android之实现底部TabHost
查看>>
解除与设置计算机锁定
查看>>