当前位置:文档之家› java简易员工管理系统

java简易员工管理系统

主要功能:用Swing制作管理界面,连接数据库,完成对数据库emp表进行查添改删功能。

查询:添加:修改:删除:以上功能在完成时如果在操作时发生输入格式错误,操作都不能完成,并且都会出现提示。

环境配置为oracle数据库recruit,表emp,ip127.0.0.1,用户名system,密码871108源代码:主界面:package exercise.emp;import java.awt.BorderLayout;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import ponentAdapter;import ponentEvent;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.Vector;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;public class UserManager extends JFrame implements ActionListener {private JPanel penter = new JPanel();private JPanel pcontrol = new JPanel();private JLabel lid = new JLabel("编号:");private JLabel lid2 = new JLabel("编号:");private JLabel lname = new JLabel("姓名:");private JLabel lname2 = new JLabel("姓名:");private JLabel ljob = new JLabel("职位:");private JLabel ljob2 = new JLabel("职位:");private JLabel lsalary = new JLabel("工资:");private JLabel lsalary2 = new JLabel("工资:");private JLabel ldeptid = new JLabel("部门编号:");private JLabel ldeptid2 = new JLabel("部门编号:");private JLabel lupdate2 = new JLabel("修改完后请点击'修改'");private JLabel limage = new JLabel(new ImageIcon(this.getClass() .getResource("emp.jpg")), JLabel.CENTER);private JTextField tid = new JTextField();private JTextField tid2 = new JTextField();private JTextField tname = new JTextField();private JTextField tname2 = new JTextField();private JTextField tjob = new JTextField();private JTextField tjob2 = new JTextField();private JTextField tsalary = new JTextField();private JTextField tsalary2 = new JTextField();private JTextField tdeptid = new JTextField();private JTextField tdeptid2 = new JTextField();private JButton bselect = new JButton("查询");private JButton binsert = new JButton("插入");private JButton bdelete = new JButton("删除");private JButton bupdate = new JButton("修改");private JButton bupdate2 = new JButton("修改");private JScrollPane simage = new JScrollPane(limage);private JScrollPane stable;private JTable ttable;private JFrame uf;UserModel um = null;WindowOperate ml = null;int id, salary, deptid;boolean isInsert, isUpdate, isDelete;String sid, name, job;Vector oneemp;public UserManager(){super("emp用户操作界面");lunachframe();ml = new WindowOperate();ml.getAddComponentListener(this, 300, 250);ml.getaddWindowListener(this);um = new UserModel();if (um.connect() == true){getdialog("数据库连接成功。

提示:查询仅需输入4位数字编号,查询所有人输入all,请勿输入其他内容!");} else{getdialog("数据库连接失败,请修改配置。

数据库(recruit),表(emp),ip(127.0.0.1),用户(system),密码(871108)。

");}}public void lunachframe(){setLayout(new BorderLayout());penter.setLayout(new GridLayout(5, 2, 10, 10));penter.add(lid);penter.add(tid);penter.add(lname);penter.add(tname);penter.add(ljob);penter.add(tjob);penter.add(lsalary);penter.add(tsalary);penter.add(ldeptid);penter.add(tdeptid);pcontrol.add(bselect);pcontrol.add(binsert);pcontrol.add(bdelete);pcontrol.add(bupdate);bselect.addActionListener(this);binsert.addActionListener(this);bdelete.addActionListener(this);bupdate.addActionListener(this);add(penter, BorderLayout.NORTH);add(simage, BorderLayout.CENTER);add(pcontrol, BorderLayout.SOUTH);setSize(300, 450);setLocation(450, 200);setVisible(true);}public void lunachupdate(){uf = new UpdateJFrame();uf.add(lid2);uf.add(tid2);uf.add(lname2);uf.add(tname2);uf.add(ljob2);uf.add(tjob2);uf.add(lsalary2);uf.add(tsalary2);uf.add(ldeptid2);uf.add(tdeptid2);uf.add(lupdate2);uf.add(bupdate2);bupdate2.addActionListener(this);ml.refreshWindow(uf);}public void gettable(){ttable = null;ttable = um.executeQuery(ttable, sid);if (ttable == null){getdialog("查无此人!提示:查询仅需输入4位数字编号,查询所有人输入all,请勿输入其他内容!");} else{if (stable != null){remove(stable);}stable = new JScrollPane(ttable);add(stable);ml.refreshWindow(this);}}public void getdialog(String message){JOptionPane.showMessageDialog(this, message);}public void actionPerformed(ActionEvent e){remove(simage);if (e.getSource() == bselect){System.out.println("查询请求:" + tid.getText());if ((tname.getText().equals("")) && (tjob.getText().equals(""))&& (tsalary.getText().equals(""))&& (tdeptid.getText().equals(""))){if (tid.getText().equals("")){getdialog("查询编号不能为空!提示:查询仅需输入4位数字编号,查询所有人输入all,请勿输入其他内容!");} else if (tid.getText().equals("all")){sid = "all";gettable();} else{sid = tid.getText();gettable();}} else{getdialog("提示:提示:查询仅需输入4位数字编号,查询所有人输入all,请勿输入其他内容!");}}else if (e.getSource() == binsert){System.out.println("添加请求:" + tid.getText());String error = "您的输入有误:";if ((tid.getText().length() > 0) && (tid.getText().length() < 5) && (IsOk.isNumber(tid.getText()) == true)){id = Integer.parseInt(tid.getText());} else{error += "编号需为1到4位的整数,";}if ((tname.getText().length() > 0)&& (tname.getText().length() < 10)){name = tname.getText();} else{error += "姓名需为1到10位的字符,";}if ((tjob.getText().length() > 0) && (tjob.getText().length() < 9)) {job = tjob.getText();} else{error += "职位需为1到9位的字符,";}if ((tsalary.getText().length() > 0)&& (tsalary.getText().length() < 8)&& (IsOk.isNumber(tsalary.getText()) == true)){salary = Integer.parseInt(tsalary.getText());} else{error += "工资需为1到7位的整数,";}if ((tdeptid.getText().length() > 0)&& (tdeptid.getText().length() < 3)&& (IsOk.isNumber(tdeptid.getText()) == true)){deptid = Integer.parseInt(tdeptid.getText());} else{error += "部门编号需为1到2位的整数,";}if (error.equals("您的输入有误:")){isInsert = um.executeInsert(id, name, job, salary, deptid);if (isInsert == false){getdialog("该编号已经存在,请重新输入!");} else{getdialog("添加成功!");}} else{error += "请按照格式输入!";getdialog(error);}}else if (e.getSource() == bdelete){System.out.println("删除请求:" + tid.getText());if ((tname.getText().equals("")) && (tjob.getText().equals("")) && (tsalary.getText().equals(""))&& (tdeptid.getText().equals(""))){if (tid.getText().equals("")){getdialog("删除编号不能为空!提示:删除仅需输入4位数字编号,请勿输入其他内容!");} else{sid = tid.getText();ttable = null;ttable = um.executeQuery(ttable, sid);if (ttable == null){getdialog("查无此人,无法进行删除!提示:删除仅需输入4位数字编号,请勿输入其他内容!");} else{id = Integer.parseInt(tid.getText());isDelete = um.executeDelete(id);if (isDelete == true){getdialog("删除成功!");} else{getdialog("删除失败!");}}}} else{getdialog("提示:删除仅需输入4位数字编号,请勿输入其他内容!"); }}else if (e.getSource() == bupdate){System.out.println("修改请求:" + tid.getText());if ((tname.getText().equals("")) && (tjob.getText().equals("")) && (tsalary.getText().equals(""))&& (tdeptid.getText().equals(""))){if (tid.getText().equals("")){getdialog("修改编号不能为空!提示:修改仅需输入4位数字编号,请勿输入其他内容!");} else{sid = tid.getText();ttable = null;ttable = um.executeQuery(ttable, sid);if (ttable == null){getdialog("查无此人,无法进行修改!提示:修改仅需输入4位数字编号,请勿输入其他内容!");} else{setVisible(false);oneemp = um.executeQuery(oneemp, sid);tid2.setText(oneemp.get(0).toString());tname2.setText(oneemp.get(1).toString());tjob2.setText(oneemp.get(2).toString());tsalary2.setText(oneemp.get(3).toString());tdeptid2.setText(oneemp.get(4).toString());tid2.setEditable(false);lunachupdate();ml.getaddWindowListener(uf, this);}}} else{getdialog("提示:修改仅需输入4位数字编号,请勿输入其他内容!"); }}else{System.out.println("修改:" + tid2.getText());String error = "您的输入有误:";id = Integer.parseInt(tid2.getText());if ((tname2.getText().length() > 0)&& (tname2.getText().length() < 10)){name = tname2.getText();} else{error += "姓名需为1到10位的字符,";}if ((tjob2.getText().length() > 0)&& (tjob2.getText().length() < 9)){job = tjob2.getText();} else{error += "职位需为1到9位的字符,";}if ((tsalary2.getText().length() > 0)&& (tsalary2.getText().length() < 8)&& (IsOk.isNumber(tsalary2.getText()) == true)){salary = Integer.parseInt(tsalary2.getText());} else{error += "工资需为1到7位的整数,";}if ((tdeptid2.getText().length() > 0)&& (tdeptid2.getText().length() < 3)&& (IsOk.isNumber(tdeptid2.getText()) == true)){deptid = Integer.parseInt(tdeptid2.getText());} else{error += "部门编号需为1到2位的整数,";}if (error.equals("您的输入有误:")){isUpdate = um.executeUpdate(id, name, job, salary, deptid); if (isUpdate == true){getdialog("修改成功!");uf.dispose();setVisible(true);} else{getdialog("修改失败!");}} else{error += "请按照格式输入!";getdialog(error);}}}public static void main(String args[]){UserManager user = new UserManager();}}数据模型:package exercise.emp;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import java.util.Vector;import javax.swing.JTable;public class UserModel{String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:recruit"; String user = "system";String password = "871108";Connection connection = null;Statement statement = null;ResultSet resultSet = null;PreparedStatement pstmt = null;JTable jtable = null;Vector rowDate = null;Vector columnNames = null;public boolean connect(){try{Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); connection = DriverManager.getConnection(dbUrl, user, password); System.out.println("连接数据库成功");statement = connection.createStatement();return true;} catch (Exception e){e.printStackTrace();return false;}}public boolean close(){try{resultSet.close();statement.close();connection.close();pstmt.close();return true;} catch (Exception e){e.printStackTrace();return false;}}public JTable executeQuery(JTable jtable, String str){this.jtable = jtable;String sql = "select * from emp";try{resultSet = statement.executeQuery(sql);columnNames = new Vector();columnNames.add("编号");columnNames.add("姓名");columnNames.add("职位");columnNames.add("工资");columnNames.add("部门编号");rowDate = new Vector();while (resultSet.next()){if (str.equals("all")){Vector row = new Vector();row.add(resultSet.getString(1));row.add(resultSet.getString(2));row.add(resultSet.getString(3));row.add(resultSet.getString(4));row.add(resultSet.getString(5));rowDate.add(row);jtable = new JTable(rowDate, columnNames); }if (str.equals(resultSet.getString(1))){Vector row = new Vector();row.add(resultSet.getString(1));row.add(resultSet.getString(2));row.add(resultSet.getString(3));row.add(resultSet.getString(4));row.add(resultSet.getString(5));rowDate.add(row);jtable = new JTable(rowDate, columnNames); }}} catch (SQLException e){e.printStackTrace();}return jtable;}public Vector executeQuery(Vector vector, String str){String sql = "select * from emp";vector = new Vector();try{resultSet = statement.executeQuery(sql);while (resultSet.next()){if (str.equals(resultSet.getString(1))){vector.add(resultSet.getString(1));vector.add(resultSet.getString(2));vector.add(resultSet.getString(3));vector.add(resultSet.getString(4));vector.add(resultSet.getString(5));}}} catch (SQLException e){e.printStackTrace();}return vector;}public boolean executeInsert(int i, String str1, String str2, int j, int k){try{pstmt = connection.prepareStatement("insert into emp values(?,?,?,?,?)");pstmt.setInt(1, i);pstmt.setString(2, str1);pstmt.setString(3, str2);pstmt.setInt(4, j);pstmt.setInt(5, k);pstmt.executeUpdate();return true;} catch (SQLException e){return false;}}public boolean executeUpdate(int id, String name, String job, int salary, int deptid){try{pstmt = connection.prepareStatement("update emp set ename=? ,job=? ,sal=? ,deptno=? where empno=?");pstmt.setString(1, name);pstmt.setString(2, job);pstmt.setInt(3, salary);pstmt.setInt(4, deptid);pstmt.setInt(5, id);pstmt.executeUpdate();return true;} catch (SQLException e){return false;}}public boolean executeDelete(int i){try{pstmt = connection.prepareStatement("delete emp where empno=?");pstmt.setInt(1, i);pstmt.executeUpdate();return true;} catch (SQLException e){return false;}}}修改界面:package exercise.emp;import java.awt.Container;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class UpdateJFrame extends JFrame{private JLabel lb = new JLabel();private JTextField tf = new JTextField();private JButton bt = new JButton();public UpdateJFrame(){super("用户修改数据中");setSize(300, 250);setLocation(450, 200);setLayout(new GridLayout(6, 2, 10, 10));setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE); WindowOperate ml = new WindowOperate();ml.getAddComponentListener(this, 300, 250); }public void add(JLabel lb){this.lb = lb;add(lb);}public void add(JTextField tf){this.tf = tf;add(tf);}public void add(JButton bt){this.bt = bt;add(bt);}}窗体控制:package exercise.emp;import ponentAdapter;import ponentEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import javax.swing.JFrame;public class WindowOperate{private JFrame jf;private JFrame jf2;private int limitewidth = 0;private int limiteheight = 0;public void getAddComponentListener(final JFrame jf, final int limitewidth,final int limiteheight){this.jf = jf;this.limitewidth = limitewidth;this.limiteheight = limiteheight;jf.addComponentListener(new ComponentAdapter(){public void componentResized(ComponentEvent e){int width = e.getComponent().getWidth();int height = e.getComponent().getHeight();if (width < limitewidth){width = limitewidth;}if (height < limiteheight){height = limiteheight;}jf.setSize(width, height);}});}public void getaddWindowListener(final JFrame jf){this.jf = jf;jf.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}public void getaddWindowListener(final JFrame jf, final JFrame jf2) {this.jf = jf;this.jf2 = jf2;jf.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){jf.setVisible(false);jf2.setVisible(true);}});}public void refreshWindow(final JFrame jf){jf.setVisible(false);jf.setVisible(true);}}数据判断:package exercise.emp;public class IsOk{public static boolean isNumber(String str){int i;try{i = Integer.parseInt(str);return true;} catch (NumberFormatException e){return false;} } }。

相关主题