上一篇博客把仓库管理系统的主页面实现了,主要是一些按钮布局的实现,这一篇博客才是真正涉及到仓库管理系统货物类型管理的实现功能。
首先是货物类型表的建立。
CREATE TABLE t_goodsType( id INT PRIMARY KEY AUTO_INCREMENT, goodsTypeName VARCHAR(20) NOT NULL, goodsTypeDesc VARCHAR(20) NOT NULL ); INSERT INTO t_goodsType VALUES(NULL, '水果类', '有关的各种水果'), (NULL, '书籍类', '有关的各种书籍'), (NULL, '手机类', '有关的各种手机');
后台数据库的表建立好后,接下来就是用Java页面和windowbuilder 其功能了。
首先在model包对数据库中仓库货物类别表的字段进行封装和对父类的构造方法进行多态的实现。
package com.panli.model; /** * GoodsType实体类 * @author Peter * */ public class GoodsType { private int id; private String goodsTypeName; private String goodsTypeDesc; /** *继承自父类的构造方法 */ public GoodsType() { super(); // TODO Auto-generated constructor stub } /** * 一个参数的构造方法 * @param goodsTypeName */ public GoodsType(String goodsTypeName) { super(); this.goodsTypeName = goodsTypeName; } /** * 一个参数的构造方法 * @param id */ public GoodsType(int id) { super(); this.id = id; } /** * 2个参数的构造方法 * @param goodsTypeName * @param goodsTypeDesc */ public GoodsType(String goodsTypeName, String goodsTypeDesc) { super(); this.goodsTypeName = goodsTypeName; this.goodsTypeDesc = goodsTypeDesc; } /** * 3个参数构造方法 * @param id * @param goodsTypeName * @param goodsTypeDesc */ public GoodsType(int id, String goodsTypeName, String goodsTypeDesc) { super(); this.id = id; this.goodsTypeName = goodsTypeName; this.goodsTypeDesc = goodsTypeDesc; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGoodsTypeName() { return goodsTypeName; } public void setGoodsTypeName(String goodsTypeName) { this.goodsTypeName = goodsTypeName; } public String getGoodsTypeDesc() { return goodsTypeDesc; } public void setGoodsTypeDesc(String goodsTypeDesc) { this.goodsTypeDesc = goodsTypeDesc; } }
然后是在dao层对数据库仓库类别表进行增删改查操作控制
package com.panli.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.panli.model.GoodsType; import com.panli.util.StringUtil; /** * 对GoodsType操作进行控制的类 * @author Peter * */ public class GoodsTypeDao { /** * 货物类别删除事件 * @param conn * @param goodsType * @return * @throws SQLException */ public static int deleteGoodsType(Connection conn, GoodsType goodsType) throws SQLException{ String sql = "delete from t_goodsType where id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, goodsType.getId()); return pstmt.executeUpdate(); } /** * 货物类别更新事件 * @param conn * @param goodsType * @return * @throws Exception */ public static int updateGoodsType(Connection conn, GoodsType goodsType) throws Exception{ String sql = " update t_goodsType set goodsTypeName=?, goodsTypeDesc=? where id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, goodsType.getGoodsTypeName()); pstmt.setString(2, goodsType.getGoodsTypeDesc()); pstmt.setInt(3, goodsType.getId()); return pstmt.executeUpdate(); } /** * 货物列表查询事件 * @param conn * @param goodsType * @return * @throws Exception */ public static ResultSet listGoodsType(Connection conn, GoodsType goodsType) throws Exception{ StringBuffer sb = new StringBuffer("select * from t_goodsType"); if(StringUtil.isNotEmpty(goodsType.getGoodsTypeName())){ sb.append(" and goodsTypeName like '%"+goodsType.getGoodsTypeName()+"%' "); } PreparedStatement pstmt = conn.prepareStatement(sb.toString().replaceFirst("and", "where")); return pstmt.executeQuery(); } /** *货物类别添加事件 * @param conn * @param goodsType * @return * @throws SQLException */ public static int addGoodsType(Connection conn, GoodsType goodsType) throws SQLException{ String sql = "insert into t_goodsType values(null, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, goodsType.getGoodsTypeName()); pstmt.setString(2, goodsType.getGoodsTypeDesc()); return pstmt.executeUpdate(); } }
最后是在视图层进行显示:
1:操作主页面显示
package com.panli.view; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.Toolkit; import javax.swing.JDesktopPane; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * 主页面视图层 * @author Peter * */ public class MainFrm extends JFrame { private JPanel contentPane; private JDesktopPane table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainFrm frame = new MainFrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MainFrm() { setTitle("\u4ED3\u5E93\u7BA1\u7406\u7CFB\u7EDF\u4E3B\u9875\u9762"); setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrm.class.getResource("/images/goods_logo.png"))); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 519, 348); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("\u4ED3\u5E93\u7BA1\u7406\u7CFB\u7EDF"); menu.setIcon(new ImageIcon(MainFrm.class.getResource("/images/manager.png"))); menuBar.add(menu); JMenu menu_2 = new JMenu("\u8D27\u7269\u7C7B\u578B\u7BA1\u7406"); menu_2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/goodmanager.png"))); menu.add(menu_2); JMenuItem menuItem_2 = new JMenuItem("\u8D27\u7269\u7C7B\u578B\u6DFB\u52A0"); menuItem_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { GoodsTypeAddInterFrm goodsTypeAddInterFrm = new GoodsTypeAddInterFrm(); goodsTypeAddInterFrm.setVisible(true); table.add(goodsTypeAddInterFrm); } }); menuItem_2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/add.png"))); menu_2.add(menuItem_2); JMenuItem menuItem_3 = new JMenuItem("\u8D27\u7269\u7C7B\u578B\u4FEE\u6539"); menuItem_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { GoodsTypeManagerInterFrm goodsTypeManagerInterFrm = new GoodsTypeManagerInterFrm(); goodsTypeManagerInterFrm.setVisible(true); table.add(goodsTypeManagerInterFrm); } }); menuItem_3.setIcon(new ImageIcon(MainFrm.class.getResource("/images/modify.png"))); menu_2.add(menuItem_3); JMenu menu_3 = new JMenu("\u8D27\u7269\u7269\u54C1\u7BA1\u7406"); menu_3.setIcon(new ImageIcon(MainFrm.class.getResource("/images/goods.png"))); menu.add(menu_3); JMenuItem menuItem_4 = new JMenuItem("\u8D27\u7269\u7269\u54C1\u6DFB\u52A0"); menuItem_4.setIcon(new ImageIcon(MainFrm.class.getResource("/images/add.png"))); menu_3.add(menuItem_4); JMenuItem menuItem_5 = new JMenuItem("\u8D27\u7269\u7269\u54C1\u4FEE\u6539"); menuItem_5.setIcon(new ImageIcon(MainFrm.class.getResource("/images/modify.png"))); menu_3.add(menuItem_5); JMenuItem menuItem_1 = new JMenuItem("\u5B89\u5168\u9000\u51FA"); menuItem_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exitSystemActionPerformed(e); } }); menuItem_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/exit.png"))); menu.add(menuItem_1); JMenu menu_1 = new JMenu("\u8054\u7CFB\u6211\u4EEC"); menu_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/contact.png"))); menuBar.add(menu_1); JMenuItem menuItem = new JMenuItem("\u8054\u7CFB\u65B9\u5F0F"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { MyContactInterFrm myContactInterFrm = new MyContactInterFrm(); myContactInterFrm.setVisible(true); table.add(myContactInterFrm); } }); menuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/images/phnoe.png"))); menu_1.add(menuItem); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); table = new JDesktopPane(); contentPane.add(table, BorderLayout.CENTER); //最大化处理 this.setExtendedState(JFrame.MAXIMIZED_BOTH); } /** * 安全退出系统 * @param e */ private void exitSystemActionPerformed(ActionEvent e) { // TODO Auto-generated method stub int n = JOptionPane.showConfirmDialog(null, "你确定要离开系统么"); if(n == 0){ dispose(); return; } } }
2:货物类别增加显示
package com.panli.view; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.SQLException; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.LayoutStyle.ComponentPlacement; import com.panli.dao.GoodsTypeDao; import com.panli.model.GoodsType; import com.panli.util.DbUtil; import com.panli.util.StringUtil; /** * goodsType视图层货物类别添加 * @author Peter * */ public class GoodsTypeAddInterFrm extends JInternalFrame { private JTextField goodsTypeNameTxt; private JTextArea goodsTypeDescTxt; private DbUtil dbUtil = new DbUtil(); private GoodsTypeDao goodsTypeDao = new GoodsTypeDao(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { GoodsTypeAddInterFrm frame = new GoodsTypeAddInterFrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public GoodsTypeAddInterFrm() { setClosable(true); setIconifiable(true); setTitle("\u8D27\u7269\u7C7B\u522B\u6DFB\u52A0\u754C\u9762"); setBounds(100, 100, 528, 392); JLabel lblNewLabel = new JLabel("\u8D27\u7269\u7C7B\u522B\u540D\u79F0\uFF1A"); lblNewLabel.setIcon(new ImageIcon(GoodsTypeAddInterFrm.class.getResource("/images/goods.png"))); JLabel label = new JLabel("\u8D27\u7269\u7C7B\u522B\u63CF\u8FF0\uFF1A"); label.setIcon(new ImageIcon(GoodsTypeAddInterFrm.class.getResource("/images/goods.png"))); goodsTypeNameTxt = new JTextField(); goodsTypeNameTxt.setColumns(10); goodsTypeDescTxt = new JTextArea(); JButton button = new JButton("\u6DFB\u52A0"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { goodsTypeAddActionPerformed(e); } }); button.setIcon(new ImageIcon(GoodsTypeAddInterFrm.class.getResource("/images/add.png"))); JButton button_1 = new JButton("\u91CD\u7F6E"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { goodsTypeResetValueActionPerformed(e); } }); button_1.setIcon(new ImageIcon(GoodsTypeAddInterFrm.class.getResource("/images/reset.png"))); GroupLayout groupLayout = new GroupLayout(getContentPane()); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGap(68) .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addComponent(label) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(goodsTypeDescTxt)) .addGroup(groupLayout.createSequentialGroup() .addComponent(lblNewLabel) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(goodsTypeNameTxt, GroupLayout.PREFERRED_SIZE, 212, GroupLayout.PREFERRED_SIZE))) .addContainerGap(144, Short.MAX_VALUE)) .addGroup(groupLayout.createSequentialGroup() .addGap(91) .addComponent(button) .addGap(153) .addComponent(button_1) .addContainerGap(154, Short.MAX_VALUE)) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGap(47) .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel) .addComponent(goodsTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(35) .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) .addComponent(label) .addComponent(goodsTypeDescTxt, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED, 88, Short.MAX_VALUE) .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) .addComponent(button) .addComponent(button_1)) .addGap(44)) ); getContentPane().setLayout(groupLayout); } /** * 货物类型添加事件 * @param e */ private void goodsTypeAddActionPerformed(ActionEvent e) { // TODO Auto-generated method stub String goodsTypeName = this.goodsTypeNameTxt.getText(); String goodsTypeDesc = this.goodsTypeDescTxt.getText(); if(StringUtil.isEmpty(goodsTypeName)){ JOptionPane.showMessageDialog(null, "货物类型名称不能为空!"); return; } if(StringUtil.isEmpty(goodsTypeDesc)){ JOptionPane.showMessageDialog(null, "货物类型描述不能为空!"); return; } GoodsType goodsType = new GoodsType(goodsTypeName, goodsTypeDesc); Connection conn = null; try { conn = dbUtil.getCon(); int result = goodsTypeDao.addGoodsType(conn, goodsType); if(result==1){ JOptionPane.showMessageDialog(null, "货物类别添加成功!"); this.resetValue();//添加成功,重置表单 }else{ JOptionPane.showMessageDialog(null, "货物类别添加失败!"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); JOptionPane.showMessageDialog(null, "货物类别添加失败!"); }finally{ try { dbUtil.close(conn); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } /** * 重置事件 * @param e */ private void goodsTypeResetValueActionPerformed(ActionEvent e) { // TODO Auto-generated method stub this.resetValue(); } /** * 重置表单 */ private void resetValue(){ this.goodsTypeNameTxt.setText(""); this.goodsTypeDescTxt.setText(""); } }
3:货物类别管理显示
package com.panli.view; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.border.TitledBorder; import javax.swing.table.DefaultTableModel; import com.panli.dao.GoodsTypeDao; import com.panli.model.GoodsType; import com.panli.util.DbUtil; import com.panli.util.StringUtil; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; /** * 货物类别管理视图层 * @author Peter * */ public class GoodsTypeManagerInterFrm extends JInternalFrame { private JTable goodsTypeTable; private JTextField s_goodsTypeNameTxt; private JTextField goodsTypeIdTxt; private JTextField goodsTypeNameTxt; private JTextArea goodsTypeDescTxt; private static DbUtil dbUtil = new DbUtil(); private static GoodsTypeDao goodsTypeDao = new GoodsTypeDao(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { GoodsTypeManagerInterFrm frame = new GoodsTypeManagerInterFrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public GoodsTypeManagerInterFrm() { setIconifiable(true); setClosable(true); setTitle("\u8D27\u7269\u7C7B\u578B\u7EF4\u62A4\u9875\u9762"); setBounds(100, 100, 548, 475); JScrollPane scrollPane = new JScrollPane(); JLabel label = new JLabel("\u8D27\u7269\u7C7B\u578B\u540D\u79F0\uFF1A"); s_goodsTypeNameTxt = new JTextField(); s_goodsTypeNameTxt.setColumns(10); JButton button = new JButton("\u641C\u7D22"); button.setIcon(new ImageIcon(GoodsTypeManagerInterFrm.class.getResource("/images/Search.png"))); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { searchbookTypeActionPerformed(e); } }); JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GroupLayout groupLayout = new GroupLayout(getContentPane()); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGap(58) .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false) .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup() .addComponent(label) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(s_goodsTypeNameTxt, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE) .addGap(54) .addComponent(button)) .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE) .addComponent(panel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)) .addContainerGap(64, Short.MAX_VALUE)) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGap(32) .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) .addComponent(label) .addComponent(s_goodsTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(button)) .addGap(26) .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE) .addComponent(panel, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); JLabel label_1 = new JLabel("\u8D27\u7269\u7C7B\u522B\u7F16\u53F7\uFF1A"); goodsTypeIdTxt = new JTextField(); goodsTypeIdTxt.setEditable(false); goodsTypeIdTxt.setColumns(10); JLabel label_2 = new JLabel("\u8D27\u7269\u7C7B\u522B\u540D\u79F0\uFF1A"); goodsTypeNameTxt = new JTextField(); goodsTypeNameTxt.setColumns(10); JLabel label_3 = new JLabel("\u8D27\u7269\u7C7B\u522B\u63CF\u8FF0\uFF1A"); goodsTypeDescTxt = new JTextArea(); JButton btnNewButton = new JButton("\u4FEE\u6539"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { updateGoodsTypeActionPerformed(arg0); } }); btnNewButton.setIcon(new ImageIcon(GoodsTypeManagerInterFrm.class.getResource("/images/modify.png"))); JButton btnNewButton_1 = new JButton("\u5220\u9664"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { deleteGoodsTypeActionPerformed(arg0); } }); btnNewButton_1.setIcon(new ImageIcon(GoodsTypeManagerInterFrm.class.getResource("/images/delete.png"))); JButton btnNewButton_2 = new JButton("\u91CD\u7F6E"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { goodsTypeResetActionPerformed(e); } }); btnNewButton_2.setIcon(new ImageIcon(GoodsTypeManagerInterFrm.class.getResource("/images/reset.png"))); GroupLayout gl_panel = new GroupLayout(panel); gl_panel.setHorizontalGroup( gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addGap(19) .addGroup(gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addComponent(label_3) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(goodsTypeDescTxt)) .addGroup(gl_panel.createSequentialGroup() .addComponent(label_1) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(goodsTypeIdTxt, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE) .addGap(14) .addComponent(label_2) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(goodsTypeNameTxt, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))) .addContainerGap(34, Short.MAX_VALUE)) .addGroup(gl_panel.createSequentialGroup() .addGap(46) .addComponent(btnNewButton) .addPreferredGap(ComponentPlacement.RELATED, 45, Short.MAX_VALUE) .addComponent(btnNewButton_1) .addGap(29) .addComponent(btnNewButton_2) .addGap(54)) ); gl_panel.setVerticalGroup( gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addContainerGap() .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE) .addComponent(label_1) .addComponent(goodsTypeIdTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label_2) .addComponent(goodsTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_panel.createParallelGroup(Alignment.LEADING) .addComponent(label_3) .addComponent(goodsTypeDescTxt, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)) .addGap(18) .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton) .addComponent(btnNewButton_2) .addComponent(btnNewButton_1))) ); panel.setLayout(gl_panel); goodsTypeTable = new JTable(); goodsTypeTable.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { clickMouseGoodsTypeTable(arg0); } }); goodsTypeTable.setModel(new DefaultTableModel( new Object[][] { }, new String[] { "\u8D27\u7269\u7C7B\u578B\u7F16\u53F7", "\u8D27\u7269\u7C7B\u578B\u540D\u79F0", "\u8D27\u7269\u7C7B\u578B\u63CF\u8FF0" } ) { boolean[] columnEditables = new boolean[] { false, false, false }; public boolean isCellEditable(int row, int column) { return columnEditables[column]; } }); goodsTypeTable.getColumnModel().getColumn(0).setPreferredWidth(96); goodsTypeTable.getColumnModel().getColumn(1).setPreferredWidth(124); scrollPane.setViewportView(goodsTypeTable); getContentPane().setLayout(groupLayout); //填充表单 this.fillGoodsTypeTable(new GoodsType()); } /** * 货物类别删除事件 * @param arg0 */ private void deleteGoodsTypeActionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String goodsTypeId = goodsTypeIdTxt.getText(); if(StringUtil.isEmpty(goodsTypeId)){ JOptionPane.showMessageDialog(null, "请选择要删除的记录"); return; } int n = JOptionPane.showConfirmDialog(null, "确定要删除此货物类别记录么?"); if(n==0){ Connection conn = null; try { conn = dbUtil.getCon(); GoodsType goodsType = new GoodsType(Integer.parseInt(goodsTypeId)); int result = goodsTypeDao.deleteGoodsType(conn, goodsType); if(result==1){ JOptionPane.showMessageDialog(null, "货物类别记录删除成功!"); this.fillGoodsTypeTable(new GoodsType()); this.resetValue(); }else{ JOptionPane.showMessageDialog(null, "货物类别记录删除失败!"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "货物类别记录删除失败!"); }finally{ try { dbUtil.close(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 货物类别更新事件 * @param arg0 */ private void updateGoodsTypeActionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String goodsTypeId = goodsTypeIdTxt.getText(); String goodsTypeName = goodsTypeNameTxt.getText(); String goodsTypeDesc = goodsTypeDescTxt.getText(); if(StringUtil.isEmpty(goodsTypeId)){ JOptionPane.showMessageDialog(null, "请选择要修改的记录!"); return; } if(StringUtil.isEmpty(goodsTypeName)){ JOptionPane.showMessageDialog(null, "货物类别名称不能为空!"); return; } if(StringUtil.isEmpty(goodsTypeDesc)){ JOptionPane.showMessageDialog(null, "货物描述不能为空!"); return; } GoodsType goodsType = new GoodsType(Integer.parseInt(goodsTypeId), goodsTypeName, goodsTypeDesc); Connection conn = null; try { conn = dbUtil.getCon(); int result = goodsTypeDao.updateGoodsType(conn, goodsType); if(result==1){ JOptionPane.showMessageDialog(null, "货物类别修改成功!"); this.fillGoodsTypeTable(new GoodsType()); }else{ JOptionPane.showMessageDialog(null, "货物类别修改失败!"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "货物类别修改失败!"); }finally{ try { dbUtil.close(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 查询表单 * @param e */ private void searchbookTypeActionPerformed(ActionEvent e) { // TODO Auto-generated method stub String goodsTypeName = s_goodsTypeNameTxt.getText(); GoodsType goodsType = new GoodsType(goodsTypeName); this.fillGoodsTypeTable(goodsType); } /** * 货物类型重置事件 * @param e */ private void goodsTypeResetActionPerformed(ActionEvent e) { // TODO Auto-generated method stub this.resetValue(); } /** * 重置表单 */ private void resetValue(){ this.goodsTypeIdTxt.setText(""); this.goodsTypeNameTxt.setText(""); this.goodsTypeDescTxt.setText(""); } //鼠标点击事件 private void clickMouseGoodsTypeTable(MouseEvent arg0) { // TODO Auto-generated method stub int row = goodsTypeTable.getSelectedRow(); goodsTypeIdTxt.setText((String)goodsTypeTable.getValueAt(row, 0)); goodsTypeNameTxt.setText((String)goodsTypeTable.getValueAt(row, 1)); goodsTypeDescTxt.setText((String)goodsTypeTable.getValueAt(row, 2)); } /** * 填充表单事件 * @param goodsType */ private void fillGoodsTypeTable(GoodsType goodsType){ DefaultTableModel dtm = (DefaultTableModel)goodsTypeTable.getModel(); dtm.setRowCount(0); Connection conn = null; ResultSet rs = null; try { conn = DbUtil.getCon(); rs = goodsTypeDao.listGoodsType(conn, goodsType); while(rs.next()){ Vector v = new Vector(); v.add(rs.getString("id")); v.add(rs.getString("goodsTypeName")); v.add(rs.getString("goodsTypeDesc")); dtm.addRow(v); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dbUtil.close(conn, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
图片:
货物类别添加
货物类别维护