惯例,开头先给师傅打个广告。
多的不说,直接上代码和效果图。数据连接层代码
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 { } }
效果图