小学期项目-基于swing人事管理系统
in 代码软件工程 with 0 comment

小学期项目-基于swing人事管理系统

in 代码软件工程 with 0 comment

简单概述

个人项目人事管理系统,针对部门和人员之间的管理。基础模块包括登录模块,管理模块。管理基础功能包括员工、部门增删改查。
技术:swing+mysql5.6.46
运行环境:Eclipse-Luna+jdk1.8

项目UI

主界面
员工信息表

项目结构

文件目录结构
SQL目录

部分代码

MainFrm.java

package view;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
 
import java.awt.Color;
 
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
 
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
 
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JTabbedPane;
 
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.util.Properties;
 
import javax.swing.SwingConstants;
 
/**
 * 主窗体类
 * @author 22219
 *
 */
public class MainFrm extends JFrame {
    /**
     * 版本串号serialVersionUID
     */
    private static final long serialVersionUID = 1L;
    public static JPanel contentPane;
    private static JTextField empNoTextField;
    private static JTextField empPositionTextField;
    private static JTextField empNameTextField;
    private static JTextField empSexTextField;
    private static JTextField empAgeTextField;
    private static JTextField idNumberTextField;
    private static JTextField depNameTextField;
    private static JTextField addressTextField;
    public static String userId;
 
    private static JTabbedPane tabbedPane;
    private static PanelDepartment panelDepartment;
    private static PanelEmployee panelEmployee;
    private static JPanel empInfo;
 
    /**
     * Create the frame.
     */
    public MainFrm(String userid) {
        userId = userid;
        setIconImage(Toolkit.getDefaultToolkit().getImage(
                MainFrm.class.getResource("/images/storage_128px.png")));
        setTitle("人事管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setBounds(100, 100, 1600, 800);
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
 
        JMenuBar menuBar = new JMenuBar();
        contentPane.add(menuBar, BorderLayout.NORTH);
        menuBar.setBackground(Color.WHITE);
 
        // 文件
        JMenu mnNewMenu = new JMenu("文件");
        mnNewMenu.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/base.png")));
        menuBar.add(mnNewMenu);
 
        // 文件->注销
        JMenuItem logoutMenuItem = new JMenuItem("注销");
        logoutMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        logoutMenuItem.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/password.png")));
        mnNewMenu.add(logoutMenuItem);
        logoutMenuItem.setMnemonic(KeyEvent.VK_A);
        logoutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
                Event.CTRL_MASK));
        logoutMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确认注销?", "提示",
                        JOptionPane.YES_NO_OPTION) == 0) {
                    dispose();
                    LogOnFrm frame = new LogOnFrm();
                    frame.setVisible(true);
                     
                }
            }
        });
 
        // 文件->退出
        JMenuItem mntmNewMenuItem_2 = new JMenuItem("退出");
        mntmNewMenuItem_2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        mntmNewMenuItem_2.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/exit.png")));
        mnNewMenu.add(mntmNewMenuItem_2);
        mntmNewMenuItem_2.setMnemonic(KeyEvent.VK_Q);
        mntmNewMenuItem_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
                Event.CTRL_MASK));
        mntmNewMenuItem_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确认退出?", "提示",
                        JOptionPane.YES_NO_OPTION) == 0) {
                    System.exit(0);
                }
            }
        });
 
        // 功能
        JMenu mnNewMenu_3 = new JMenu("功能");
        mnNewMenu_3.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/bookTypeManager.png")));
        menuBar.add(mnNewMenu_3);
 
        // 功能->部门管理
        JMenuItem mntmNewMenuItem_1 = new JMenuItem("部门管理");
        mntmNewMenuItem_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        mntmNewMenuItem_1.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/bookTypeManager.png")));
        mnNewMenu_3.add(mntmNewMenuItem_1);
        mntmNewMenuItem_1.setMnemonic(KeyEvent.VK_F2);
        mntmNewMenuItem_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,
                0));
        mntmNewMenuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setSelectedComponent(panelDepartment);
            }
        });
 
        // 功能->员工管理
        JMenuItem mntmNewMenuItem_3 = new JMenuItem("员工管理");
        mntmNewMenuItem_3.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        mntmNewMenuItem_3.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/userName.png")));
        mnNewMenu_3.add(mntmNewMenuItem_3);
        mntmNewMenuItem_3.setMnemonic(KeyEvent.VK_F3);
        mntmNewMenuItem_3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,
                0));
        mntmNewMenuItem_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setSelectedComponent(panelEmployee);
            }
        });
 
        // 功能->人员信息
        JMenuItem mntmNewMenuItem_4 = new JMenuItem("人员信息");
        mntmNewMenuItem_4.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        mntmNewMenuItem_4.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/userName.png")));
        mnNewMenu_3.add(mntmNewMenuItem_4);
        mntmNewMenuItem_4.setMnemonic(KeyEvent.VK_F4);
        mntmNewMenuItem_4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
                0));
        mntmNewMenuItem_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setSelectedComponent(empInfo);
            }
        });
 
        // 主题切换
        JMenu viewMenu = new JMenu("主题");
        viewMenu.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/view.png")));
        menuBar.add(viewMenu);
        ButtonGroup group = new ButtonGroup();
        JRadioButtonMenuItem v1 = new JRadioButtonMenuItem("Metal", true);
        v1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        v1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
        viewMenu.add(v1);
        v1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager
                            .setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(MainFrm.this);
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e1) {
                }
            }
        });
        JRadioButtonMenuItem v2 = new JRadioButtonMenuItem("Motif");
        v2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        v2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
        viewMenu.add(v2);
        v2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager
                            .setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(MainFrm.this);
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e1) {
                }
            }
        });
        JRadioButtonMenuItem v3 = new JRadioButtonMenuItem("Windows");
        v3.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        v3.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
        viewMenu.add(v3);
        v3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager
                            .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(MainFrm.this);
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e1) {
                }
            }
        });
        JRadioButtonMenuItem v4 = new JRadioButtonMenuItem("Nimbus");
        v4.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        v4.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
        viewMenu.add(v4);
        v4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager
                            .setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(MainFrm.this);
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e1) {
                }
            }
        });
        JRadioButtonMenuItem v5 = new JRadioButtonMenuItem("beautyeye");
        v5.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        v5.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
        viewMenu.add(v5);
        v5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
                    org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper
                            .launchBeautyEyeLNF();
                    UIManager.put("RootPane.setupButtonVisible", false);
                    SwingUtilities.updateComponentTreeUI(MainFrm.this);
                } catch (Exception e1) {
                }
            }
        });
        group.add(v1);
        group.add(v2);
        group.add(v3);
        group.add(v4);
        group.add(v5);
        viewMenu.add(v1);
        viewMenu.add(v2);
        viewMenu.add(v3);
        viewMenu.add(v4);
        viewMenu.add(v5);
 
        // 关于
        JMenu mnNewMenu_4 = new JMenu("关于");
        mnNewMenu_4.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/about.png")));
        menuBar.add(mnNewMenu_4);
 
        // 关于->帮助
        JMenuItem mntmNewMenuItem = new JMenuItem("帮助");
        mntmNewMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        mntmNewMenuItem.setIcon(new ImageIcon(MainFrm.class
                .getResource("/images/me.png")));
        mnNewMenu_4.add(mntmNewMenuItem);
        mntmNewMenuItem.setMnemonic(KeyEvent.VK_F1);
        mntmNewMenuItem.setAccelerator(KeyStroke
                .getKeyStroke(KeyEvent.VK_F1, 0));
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(MainFrm.this, "人事管理系统:\n"
                        + "在企业中,人事管理工作是非常重要的一项工作,它负责整个企业的日常人事安排,\n"
                        + "人员的人事管理等。高效的人事管理可以提高企业的市场竞争力,使企业具有更强的凝\n" + "聚力和活力。"
                        + "", "关于", 1);
            }
        });
 
        // 添加管理板块选项卡面板(PanelDep+PanelEmp+empInfo)
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        contentPane.add(tabbedPane, BorderLayout.CENTER);
 
        panelDepartment = new PanelDepartment("empsal", "dep");
        panelDepartment.setBorder(new EmptyBorder(5, 5, 5, 5));
        panelEmployee = new PanelEmployee("empsal", "emp");
        panelEmployee.setBorder(new EmptyBorder(5, 5, 5, 5));
        empInfo = new JPanel();
        empInfo.setBorder(new EmptyBorder(5, 5, 5, 5));
        empInfo.setLayout(new BorderLayout(0, 0));
        tabbedPane.addTab("部门管理", null, panelDepartment, null);
        tabbedPane.addTab("员工管理", null, panelEmployee, null);
        tabbedPane.addTab("人员信息", null, empInfo, null);
 
        // 个人信息选项卡
        JToolBar toolBar = new JToolBar();
        empInfo.add(toolBar, BorderLayout.NORTH);
 
        JButton btnNewButton = new JButton("\u4E0A\u4E00\u4E2A");
        btnNewButton.setIcon(new ImageIcon(EmpInfo.class
                .getResource("/images/NavBack.png")));
        toolBar.add(btnNewButton);
 
        JButton btnNewButton_1 = new JButton("\u4E0B\u4E00\u4E2A");
        btnNewButton_1.setIcon(new ImageIcon(EmpInfo.class
                .getResource("/images/NavForward.png")));
        toolBar.add(btnNewButton_1);
 
        JButton btnNewButton_5 = new JButton("\u589E\u52A0");
        btnNewButton_5.setIcon(new ImageIcon(EmpInfo.class
                .getResource("/images/add.png")));
        toolBar.add(btnNewButton_5);
 
        JButton btnNewButton_2 = new JButton("\u5BFC\u51FA");
        btnNewButton_2.setIcon(new ImageIcon(EmpInfo.class
                .getResource("/images/saveHS.png")));
        toolBar.add(btnNewButton_2);
 
        JButton btnNewButton_3 = new JButton("\u6253\u5370");
        btnNewButton_3.setIcon(new ImageIcon(EmpInfo.class
                .getResource("/images/PrintHS.png")));
        toolBar.add(btnNewButton_3);
 
        JButton btnNewButton_4 = new JButton("\u9000\u51FA");
        btnNewButton_4.setIcon(new ImageIcon(EmpInfo.class
                .getResource("/images/RestartHS.png")));
        toolBar.add(btnNewButton_4);
 
        JTabbedPane tabbedPane1 = new JTabbedPane(JTabbedPane.TOP);
        empInfo.add(tabbedPane1, BorderLayout.CENTER);
 
        JPanel fatherBaseInfoPanel = new JPanel();
        fatherBaseInfoPanel.setLayout(new BorderLayout());
        JPanel baseInfoPanel = new JPanel();
        fatherBaseInfoPanel.add(baseInfoPanel, BorderLayout.CENTER);
 
        tabbedPane1.addTab("基本信息", null, fatherBaseInfoPanel, null);
        baseInfoPanel.setLayout(new GridLayout(5, 4, 200, 100));
 
        JLabel empNoLabel = new JLabel("\u5458\u5DE5ID");
        empNoLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(empNoLabel);
 
        empNoTextField = new JTextField();
        baseInfoPanel.add(empNoTextField);
        empNoTextField.setColumns(10);
 
        JLabel empNameLabel = new JLabel("\u59D3\u540D");
        empNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(empNameLabel);
 
        empNameTextField = new JTextField();
        empNameTextField.setColumns(10);
        baseInfoPanel.add(empNameTextField);
 
        JLabel empAgeLabel = new JLabel("\u5E74\u9F84");
        empAgeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(empAgeLabel);
 
        empAgeTextField = new JTextField();
        empAgeTextField.setColumns(10);
        baseInfoPanel.add(empAgeTextField);
 
        JLabel empSexLabel = new JLabel("\u6027\u522B");
        empSexLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(empSexLabel);
 
        empSexTextField = new JTextField();
        empSexTextField.setColumns(10);
        baseInfoPanel.add(empSexTextField);
 
        JLabel idNumberLabel = new JLabel("\u8EAB\u4EFD\u8BC1\u53F7");
        idNumberLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(idNumberLabel);
 
        idNumberTextField = new JTextField();
        idNumberTextField.setColumns(10);
        baseInfoPanel.add(idNumberTextField);
 
        JLabel empPositionLabel = new JLabel("\u804C\u52A1");
        empPositionLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(empPositionLabel);
 
        empPositionTextField = new JTextField();
        empPositionTextField.setColumns(10);
        baseInfoPanel.add(empPositionTextField);
 
        JLabel lblNewLabel_2 = new JLabel("\u6240\u5C5E\u90E8\u95E8");
        lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(lblNewLabel_2);
 
        depNameTextField = new JTextField();
        depNameTextField.setColumns(10);
        baseInfoPanel.add(depNameTextField);
 
        JLabel addressLabel = new JLabel("\u5C45\u4F4F\u5730");
        addressLabel.setHorizontalAlignment(SwingConstants.CENTER);
        baseInfoPanel.add(addressLabel);
 
        addressTextField = new JTextField();
        addressTextField.setColumns(10);
        baseInfoPanel.add(addressTextField);
 
        JLabel lblNewLabel_3 = new JLabel(
                "     \u53CC\u51FB\u9009\u62E9\u7167\u7247");
        lblNewLabel_3.setBackground(Color.WHITE);
        fatherBaseInfoPanel.add(lblNewLabel_3, BorderLayout.EAST);
        lblNewLabel_3.setBorder(BorderFactory.createLineBorder(Color.black));
 
        JPanel panel_1 = new JPanel();
        tabbedPane1.addTab("社会关系", null, panel_1, null);
        panel_1.setLayout(null);
 
        JPanel panel_2 = new JPanel();
        tabbedPane1.addTab("工资晋升", null, panel_2, null);
        panel_2.setLayout(null);
 
        JPanel panel_3 = new JPanel();
        tabbedPane1.addTab("待开发", null, panel_3, null);
 
        JPanel panel_4 = new JPanel();
        tabbedPane1.addTab("待开发", null, panel_4, null);
 
        // 按钮监听
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                if (code == KeyEvent.VK_F5) {
                }
            }
 
        });
 
        // 关闭窗口监听
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确认退出?", "提示",
                        JOptionPane.YES_NO_OPTION) == 0) {
                    System.exit(0);
                }
 
            }
        });
    }
 
 
    /**
     * 测试主窗口
     */
    public static void main(String[] args) {
        // try{
        // BeautyEyeLNFHelper.frameBorderStyle =
        // BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
        // org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        // }catch(Exception e){}
        // UIManager.put("RootPane.setupButtonVisible", false);
        MainFrm frame = new MainFrm("NULL");
        frame.setVisible(true);
//      frame.printFrameAction();
    }
}

EmployeeDao.java

package dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import javax.swing.JOptionPane;
 
import util.DbUtil;
import view.MainFrm;
import model.Employee;
 
/**
 * 员工Dao类
 * 实现Employee的添、删、改、查数据库操作类EmployeeDao
 * @author 22219
 *
 */   
public class EmployeeDao {
 
    private EmployeeDao() {}
 
    /**
     * 添加员工
     * @param e
     */
    public static void addEmployee(Employee e) {
        Connection conn = null;
        try {
            conn = DbUtil.getCon();
            PreparedStatement ps = conn
                    .prepareStatement("INSERT INTO emp (empNo,empName,depName,empPosition,empSex,empAge,idNumber,address) VALUES (?,?,?,?,?,?,?,?)");
            ps.setString(1, e.getEmpNo());
            ps.setString(2, e.getEmpName());
            ps.setString(3, e.getDepName());
            ps.setString(4, e.getEmpPosition());
            ps.setString(5, e.getEmpSex());
            ps.setString(6, e.getEmpAge());
            ps.setString(7, e.getIdNumber());
            ps.setString(8, e.getAddress());
             
            ps.executeUpdate();
            ps.close();
        } catch (Exception ex) { 
            JOptionPane.showMessageDialog(MainFrm.contentPane,
                    "插入出错,请检查:\n1.员工id是否重复输入!\n2.部门名是否拼写错误!");
        } finally {
            DbUtil.closeCon(conn);
        }
    }
 
    /**
     * 删除员工
     * @param id
     */
    public static void deleteEmployee(String id) {
        Connection conn = null;
        try {
            conn = DbUtil.getCon();
            PreparedStatement ps = conn
                    .prepareStatement("DELETE FROM emp WHERE empNo=?");
            ps.setString(1, id);
            ps.executeUpdate();
            ps.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DbUtil.closeCon(conn);
        }
    }
 
    /**
     * 修改员工信息
     * @param a
     */
    public static void updateEmployee(Employee a) {
        Connection conn = null;
        try {
            conn = DbUtil.getCon();
            String Sql = "UPDATE emp SET empNo=?,empName=?,depName=?,empPosition=?,empSex=?,empAge=?,idNumber=?,address=? WHERE empNo =?";
            PreparedStatement ps = conn.prepareStatement(Sql);
            ps.setString(1, a.getEmpNo());
            ps.setString(2, a.getEmpName());
            ps.setString(3, a.getDepName());
            ps.setString(4, a.getEmpPosition());
            ps.setString(5, a.getEmpSex());
            ps.setString(6, a.getEmpAge());
            ps.setString(7, a.getIdNumber());
            ps.setString(8, a.getAddress());
            ps.setString(9, a.getEmpNo());
 
            ps.executeUpdate();
            ps.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DbUtil.closeCon(conn);
        }
    }
 
    /**
     * 根据员工号empNo查询
     * @param id
     * @return
     */
    public static Employee getEmployee(String id) {
        Employee e = null;
        Connection conn = null;
        try {
            conn = DbUtil.getCon();
            // 建立PreparedStatement对象用于预编译SQL语句
            PreparedStatement ps = conn
                    .prepareStatement("SELECT * FROM emp WHERE empno=?");
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery(); // 执行查询,返回结果集
            if (rs.next()) { // 因为管理员id是惟一的,所以只返回一个结果既可
                e = new Employee(rs.getString(1), rs.getString(2),
                        rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8));
            }
            ps.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DbUtil.closeCon(conn);
        }
        return e;
    }
 
    // 查询表中的所有记录(员工),返回迭代器对象
    public static Iterator<Employee> getAllEmployee() {
        List<Employee> l = new ArrayList<Employee>();// 数组列表类对象,大小可自动增加
        Connection conn = null;
        try {
            conn = DbUtil.getCon(); // 获得数据连接
            Statement stmt = conn.createStatement(); // 建立Statement用于执行SQL操作
            ResultSet rs = stmt.executeQuery("SELECT * FROM emp");// 执行查询,返回结果集
            while (rs.next()) {// 循环得到所有记录,并添加到数组列表中
                l.add(new Employee(rs.getString(1), rs.getString(2),
                        rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)));
            }
            stmt.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DbUtil.closeCon(conn);
        }
        return l.iterator();// 返回迭代器对象
    }
 
    /*
     * 主程序测试功能
     */
    public static void main(String[] args) {
//      Employee admin = new Employee();
//      EmployeeDao.addEmployee(admin);
        for (Iterator<Employee> it = EmployeeDao.getAllEmployee(); it.hasNext();) {
            Employee a1 = (Employee) it.next();
            System.out.println(a1.getEmpNo() + "\t" + a1.getEmpName() + "\t"
                    + a1.getDepName());
        }
    }
 
}
文章已浏览 60 本文由 YOYLING. 创作,采用 知识共享署名4.0 国际许可协议进行许可
转载前请署名 最后编辑时间为: Apr 28, 2020 at 10:37 pm

Responses
选择表情
Top