人员管理系统——java语言

来源:互联网 发布:光大证券软件下载 编辑:程序博客网 时间:2024/05/27 20:50
package contacts;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Main extends JFrame implements ActionListener{/** *  *//**Create the main UI*/private static final long serialVersionUID = 1L;JLabel label = new JLabel("用户:",JLabel.RIGHT);JLabel label1 = new JLabel("密码:",JLabel.RIGHT);JTextField jtfuser = new JTextField(10);JPasswordField jtfpassword = new JPasswordField(10);JButton button = new JButton("登录");JButton button1 = new JButton("退出");public Main(){setTitle("人员管理系统(登录)");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);Container componentPane = this.getContentPane();setLayout(new FlowLayout());componentPane.add(label);componentPane.add(jtfuser);componentPane.add(label1);componentPane.add(jtfpassword);componentPane.add(button);componentPane.add(button1);setBounds(476,227,212,150);setVisible(true);button1.addActionListener(this);button.addActionListener(this);}public static void main(String[] args) {// TODO Auto-generated method stubMain main = new Main();}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getActionCommand()=="退出"){System.exit(0);}if(e.getActionCommand() == "登录"){new Person();this.dispose();}}}-------------------------package contacts;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.ResultSet;import java.sql.SQLException;public class Person extends JFrame implements ActionListener{/** *  */private static final long serialVersionUID = 1L;SQLOperator sqlOperator = new SQLOperator();JMenuBar jMenuBar = new JMenuBar();JMenu personMenu = new JMenu("人员管理");JMenu systemMenu = new JMenu("系统");JMenuItem addItem = new JMenuItem("人员增加");JMenuItem deleteItem = new JMenuItem("人员删除");JMenuItem updateItem = new JMenuItem("人员更新");JMenuItem switchItem = new JMenuItem("切换用户");JMenuItem exitItem = new JMenuItem("退出");JLabel label = new JLabel("人员管理系统",JLabel.CENTER);JLabel jlname = new JLabel("姓名:");JTextField jtfname = new JTextField("请输入姓名",8);JButton button = new JButton("查询");JButton button1 = new JButton("查询全部");JTable table = new JTable();JScrollPane jScrollPane = new JScrollPane(table);public Person(){setTitle("人员管理系统");this.setResizable(false);Container contentPane = this.getContentPane();personMenu.add(addItem);personMenu.add(deleteItem);personMenu.add(updateItem);systemMenu.add(switchItem);systemMenu.add(exitItem);jMenuBar.add(personMenu);jMenuBar.add(systemMenu);setJMenuBar(jMenuBar);label.setFont(new Font("楷体",Font.BOLD,30));JPanel panel = new JPanel();panel.setLayout(new FlowLayout());panel.add(jlname);panel.add(jtfname);panel.add(button);panel.add(button1);panel.setBackground(new Color(58,104,167));contentPane.add(label,BorderLayout.NORTH);contentPane.add(panel,BorderLayout.CENTER);contentPane.add(jScrollPane,BorderLayout.SOUTH);setBounds(199,77,819,589);setVisible(true);addItem.addActionListener(this);deleteItem.addActionListener(this);updateItem.addActionListener(this);switchItem.addActionListener(this);exitItem.addActionListener(this);jtfname.addActionListener(this);button.addActionListener(this);button1.addActionListener(this);}public void actionPerformed(ActionEvent e){if(e.getSource() == exitItem){System.exit(0);}if(e.getSource() == switchItem){this.dispose();new Main();}if(e.getSource() == jtfname || e.getSource() == button){String name = jtfname.getText();String sql = "select * from Contacts where name = ?";String[] names = {name};ResultSet rs = sqlOperator.query(sql, names);try {Utilities.displayResultSet(table, rs);table.validate();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}if(e.getSource() == button1){String sql = "select * from Contacts";ResultSet rs = sqlOperator.query(sql);try{Utilities.displayResultSet(table, rs);table.validate();}catch(SQLException e1){e1.printStackTrace();}}if(e.getSource() == addItem){AddContacts addContacts = new AddContacts();addContacts.setBounds(344,203,307, 345);addContacts.setVisible(true);}if(e.getSource() == deleteItem){String name;//这里有Bugif((name = JOptionPane.showInputDialog(Person.this, "请输入要删除人员姓名:", "人员管理系统", JOptionPane.INFORMATION_MESSAGE)) == null)return;if(name.equals("")){JOptionPane.showMessageDialog(Person.this, "输入的不能为空", "ERROR",JOptionPane.ERROR_MESSAGE);return;}String sql = "delete from Contacts where name = ?";String[] names = {name};int i = sqlOperator.update(sql, names);if(i==1){JOptionPane.showMessageDialog(Person.this, "成功删除人员"+name, "人员管理系统(删除)", JOptionPane.INFORMATION_MESSAGE);}else{JOptionPane.showMessageDialog(Person.this, "要删除的人员不存在", "ERROR", JOptionPane.ERROR_MESSAGE);}}}}-----------package contacts;import javax.swing.*;import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.*;public class AddContacts extends JFrame implements ActionListener{SQLOperator sqlOperator = new SQLOperator();JLabel label = new JLabel("人员管理系统",JLabel.CENTER);JLabel jlname = new JLabel("姓名:",JLabel.RIGHT);JLabel jlage = new JLabel("年龄:",JLabel.RIGHT);JLabel jltel = new JLabel("电话:",JLabel.RIGHT);JLabel jlqq = new JLabel("QQ:",JLabel.RIGHT);JTextField jtfname = new JTextField(6);JTextField jtfage = new JTextField(6);JTextField jtftel = new JTextField(11);JTextField jtfqq = new JTextField(12);JButton button = new JButton("确定");JButton button1 = new JButton("取消");Font font = new Font("楷体",Font.BOLD,30);public AddContacts(){setTitle("添加");this.setResizable(false);Container contentPane = this.getContentPane();jtfname.setFont(font);jtfage.setFont(font);label.setFont(new Font("楷体",Font.BOLD,35));JPanel panel = new JPanel();panel.setLayout(new GridLayout(4,2));panel.add(jlname);panel.add(jtfname);panel.add(jlage);panel.add(jtfage);panel.add(jltel);panel.add(jtftel);panel.add(jlqq);panel.add(jtfqq);JPanel panel1 = new JPanel();panel1.setLayout(new FlowLayout());panel1.add(button);panel1.add(button1);contentPane.add(label,BorderLayout.NORTH);contentPane.add(panel,BorderLayout.CENTER);contentPane.add(panel1,BorderLayout.SOUTH);button.addActionListener(this);button1.addActionListener(this);jtfname.addActionListener(this);jtfage.addActionListener(this);jtftel.addActionListener(this);jtfqq.addActionListener(this);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource() == button || e.getSource()==jtfname || e.getSource()==jtfage || e.getSource()==jtftel || e.getSource() == jtfqq){String[] rows = {jtfname.getText().toString().trim(),jtfage.getText().toString().trim(),jtftel.getText().toString().trim(),jtfqq.getText().toString().trim()};for(int i = 0;i<rows.length;i++){if(rows[i].equals("")){JOptionPane.showMessageDialog(AddContacts.this,"必须填满","ERROR",JOptionPane.INFORMATION_MESSAGE);return;}}String sql = "insert into Contacts values(?,?,?,?)";int i = sqlOperator.update(sql, rows);if(i == 1){JOptionPane.showMessageDialog(AddContacts.this, "增加人员"+jtfname.getText()+"成功", "人员管理系统", JOptionPane.INFORMATION_MESSAGE);}else{JOptionPane.showMessageDialog(AddContacts.this, "添加失败");}}if(e.getSource() == button1){this.dispose();}}}··························package contacts;import java.awt.Component;import java.sql.*;import javax.swing.JOptionPane;public class DBConnection {public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";public static final String URL = "jdbc:sqlserver://LVXIANG\\SQLEXPRESS;DatabaseName=UserInfo";public static final String USER = "sa";public static final String PASSWORD = "*************";static{try{Class.forName(DRIVER);}catch(ClassNotFoundException e){JOptionPane.showMessageDialog((Component)null, "加载驱动失败","ERROR",JOptionPane.ERROR_MESSAGE);e.printStackTrace();}}public Connection getConnection(){Connection conn = null;try{conn = DriverManager.getConnection(URL, USER, PASSWORD);}catch(SQLException e){JOptionPane.showMessageDialog((Component)null, e.toString());}return conn;}public void close(Connection conn){if(conn != null){try{conn.close();}catch(SQLException e){e.printStackTrace();}}}public void close(PreparedStatement pstmt){if(pstmt != null){try{pstmt.close();}catch(SQLException e){e.printStackTrace();}}}public void close(ResultSet rs){if(rs != null){try{rs.close();}catch(SQLException e){e.printStackTrace();}}}}--------------------------package contacts;import java.sql.*;public class SQLOperator {DBConnection db = new DBConnection();public ResultSet query(String sql,Object[] objs){Connection conn = db.getConnection();ResultSet rs = null;PreparedStatement pstmt = null;try {pstmt = conn.prepareStatement(sql);for(int i = 0;i<objs.length;i++){pstmt.setString(i+1, (String)objs[i]);}rs = pstmt.executeQuery();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return rs;}public ResultSet query(String sql){Connection conn = db.getConnection();ResultSet rs = null;Statement stmt = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(sql);}catch(SQLException e){e.printStackTrace();}return rs;}public int update(String sql,Object[] objs){Connection conn = db.getConnection();PreparedStatement pstmt = null;int row = 0;try{pstmt = conn.prepareStatement(sql);for(int i = 0;i<objs.length;i++){pstmt.setString(i+1, (String)objs[i]);}row = pstmt.executeUpdate();}catch(SQLException e){e.printStackTrace();}finally{db.close(conn);db.close(pstmt);}return row;}}···········································package contacts;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.Vector;import javax.swing.JOptionPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel; public class Utilities {    /**     * 功能:实现结果集的表格显示     */    public static void displayResultSet(JTable table, ResultSet rs)            throws SQLException {        //       // rs.beforeFirst();// 指针移到第一条记录前面        boolean hasRecords = rs.next();         if (!hasRecords) { // 记录集为空,提示一条消息            JOptionPane.showMessageDialog(table, "无相关记录", "Check your input!",                    JOptionPane.ERROR_MESSAGE);            return;        }         Vector<String> columnHeads = new Vector<String>();// 用于存储表头字段(列名)        Vector<Vector> rows = new Vector<Vector>();// 用于存储记录行        try {            // 获取字段的名称            ResultSetMetaData rsmd = rs.getMetaData();            for (int i = 1; i <= rsmd.getColumnCount(); ++i)                columnHeads.addElement(rsmd.getColumnName(i));             do {// 获取记录集                rows.addElement(getNextRow(rs, rsmd));            } while (rs.next());            // 建立相应的TableModel,并将TableModel应用到Table中显示出来            DefaultTableModel model = new DefaultTableModel(rows, columnHeads);            table.setModel(model);            return;        } catch (SQLException exc) {            JOptionPane.showMessageDialog(table, exc.toString(),                    "Check your input!", JOptionPane.ERROR_MESSAGE);            return;        }    }     /**     * 被displayResultSet(JTable table, ResultSet rs)调用, 以Vector形式返回一个记录行     */    private static Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd)            throws SQLException {        Vector<String> currentRow = new Vector<String>();        for (int i = 1; i <= rsmd.getColumnCount(); ++i)            currentRow.addElement(rs.getString(i));        return currentRow; // 返回一条记录    } }


0 0
原创粉丝点击