这下面是我的MySQL的设计:
这是我的Java界面设计(毫无美感,将就着看吧)
注意:最好分管理员和非管理员。
注:我的表显示是另开了一个界面,本来是打算同一界面解决的,但是遇到了刷新不了数据的问题,只能放弃,至今依然没找到办法解决呢~~~
这是我的Java代码解释:
1、数据库连接
这个数据库连接方法可以去网上找呢。大概就是:在这个 C:\Program Files (x86)\Java\jre7\lib\ext 文件夹将mysql-connector-java-5.1.8-bin.jar 复制一份放入其中。在 Eclipse 中,鼠标选中项目工程,右键点击 Build Path,选择 Configure Build Path,会跳 出 一 个 属 性 框 图 。 选 择 Java Build Path 下 的 Libraries , 查 看 是 否 有mysql-connector-java-5.1.8-bin.jar。如果没有,点击Add External JARs,浏览到JDBC的MySQL驱动的 jar 包,点击确定,将其导入到项目中。
//数据库连接
public class DBhelper {public static final String url = "jdbc:mysql://localhost/personal_management"; public static final String name = "com.mysql.jdbc.Driver"; public static final String user = "root"; public static final String password = "admin"; public Connection conn = null; public PreparedStatement pst = null; public DBhelper(String sql) { try { Class.forName(name);conn = DriverManager.getConnection(url,user,password); pst = conn.prepareStatement(sql);} catch (Exception e) { e.printStackTrace(); } } public void close() { try { this.conn.close(); this.pst.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) {}
}
2、登录界面
//登录界面
public class register {public int usercode;public int passcode;public register() {JFrame register=new JFrame("人事管理系统登录");register.setSize(450, 345);register.setLocation(700, 300);register.setLayout(null);register.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JLabel username=new JLabel("账号");JLabel password=new JLabel("密码");JLabel tille=new JLabel("欢 迎 登 录");username.setFont(new Font("宋体",1,18));username.setForeground(Color.WHITE);password.setFont(new Font("宋体",1,18));password.setForeground(Color.WHITE);tille.setFont(new Font("华文行楷",1,30));tille.setForeground(new Color(200,150,100));JTextField t1=new JTextField(15);JPasswordField t2=new JPasswordField(15);JButton bt=new JButton("登录");bt.setFont(new Font("宋体",1,18));t2.setBounds(150, 150, 200, 30);t1.setBounds(150, 100, 200, 30);username.setBounds(75, 100, 50, 30);password.setBounds(75, 150, 50, 30);tille.setBounds(150, 50, 200, 30);bt.setSize(50, 20);bt.setBounds(170, 220,100,30);JLabel b=new JLabel(new ImageIcon("C:\\Users\\22592\\Pictures\\Camera Roll\\pp.jpg"));b.setBounds(0, 0, 450, 350);register.add(bt);register.add(t2);register.add(t1);register.add(username);register.add(password);register.add(tille); register.add(b);register.setVisible(true);t1.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) { String u =t1.getText();int i=Integer.valueOf(u).intValue();usercode=i;}});t2.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {String p = t2.getText();int j=Integer.valueOf(p).intValue();passcode=j;}});bt.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) { } @Overridepublic void mousePressed(MouseEvent e) {new userverify(usercode,passcode);} @Overridepublic void mouseExited(MouseEvent e) { } @Overridepublic void mouseEntered(MouseEvent e) { } @Overridepublic void mouseClicked(MouseEvent e) {}});}public static void main(String[] args) {new register();}
}
3、管理员界面
我就不把全部界面都拿出来,只拿了管理员界面作为例子,道理都是一样的。
//管理员界面
public class perframe extends JFrame{public perframe(int au) {this.setTitle("管理员");this.setSize(750, 500);this.setLocation(500, 250);this.setLayout(null);this.design(au);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}void design(int a) {JMenuBar mBar=new JMenuBar();this.setJMenuBar(mBar);JMenu mn1=new JMenu("信息");mn1.setFont(new Font("宋体", Font.PLAIN, 16));JMenu mn2=new JMenu("编辑");mn2.setFont(new Font("宋体", Font.PLAIN, 16));mBar.add(mn1);mBar.add(mn2);JMenuItem item2=new JMenuItem("加密");JMenuItem item3=new JMenuItem("退出");JMenuItem item4=new JMenuItem("信息查询修改");JMenuItem item5=new JMenuItem("增删员工信息");JMenuItem item6=new JMenuItem("人事变动记录");item2.setFont(new Font("宋体", Font.PLAIN, 16));item3.setFont(new Font("宋体", Font.PLAIN, 16));item4.setFont(new Font("宋体", Font.PLAIN, 16));item5.setFont(new Font("宋体", Font.PLAIN, 16));item6.setFont(new Font("宋体", Font.PLAIN, 16));mn1.addSeparator();mn1.add(item2);mn1.addSeparator();mn1.add(item3);mn2.add(item4);mn2.addSeparator();mn2.add(item5);mn2.addSeparator();mn2.add(item6);item2.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {new Encryption();}});item3.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});item4.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {new Chre(a);}});item5.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {new Staffadd();}});item6.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {new Change();}});}public static void main (String[] args) {}
}
4、登录验证
就是看权限然后打开不同的管理界面。
//验证账号֤
public class userverify {static String sql = null; static DBhelper db1 = null; static ResultSet ret = null; public userverify(int user,int pass) {sql = "select ID,PASSWD,AUTHORITY from person where ID="+user;//SQL语句db1 = new DBhelper(sql);//try { ret = db1.pst.executeQuery();while (ret.next()) { int u = ret.getInt(1); int p = ret.getInt(2); int authority=ret.getInt(3);if(u==user&&p==pass&&authority>=8) {new perframe(authority);//管理员} else if(u==user&&p==pass&&authority>=5&&authority<=7){new stafframe(authority);//中层}else if(u==user&&p==pass&&authority>0&&authority<5){new checkself(authority,u);//底层}else if(authority==0){fsta();}else {errorme();}}ret.close(); db1.close();} catch (SQLException e) { e.printStackTrace(); } }void fsta() {JOptionPane.showMessageDialog(null,"您并非公司员工,不可登录!","错误",JOptionPane.ERROR_MESSAGE);}void errorme() {JOptionPane.showMessageDialog(null,"账号或密码不正确!!!","错误",JOptionPane.ERROR_MESSAGE);}public static void main(String[] args) {}}
5、查询员工信息界面
//查询修改
public class Chre {public String seleitem;//修改选项public int sele=0;public int nsele;public String sele1=null;private JFrame chre;private JTable st;private JFrame tashow;private DefaultTableModel model;public String seleche;//查询选项private int che1;private String che2;public Chre(int au) {chre=new JFrame("查询及修改员工信息");chre.setSize(900, 300);chre.setLocation(450, 300);chre.setLayout(null);chre.setVisible(true);chre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//查询JPanel panel=new JPanel();JTextField t=new JTextField(20);JComboBox combox=new JComboBox();JLabel check=new JLabel("查询信息");check.setFont(new Font("宋体",1,18));check.setForeground(Color.black);combox.addItem("请选择查询条件");combox.addItem("员工号");combox.addItem("姓名");combox.addItem("性别");combox.addItem("所属部门");combox.addItem("职务");combox.addItem("受教育程度");combox.addItem("ְ专业技能");combox.addItem("当前状态");JButton bt1=new JButton("查询");JButton bt2=new JButton("查询全部员工的信息");panel.add(combox);panel.add(t);chre.add(panel);panel.setBounds(25, 100, 400, 50);check.setBounds(50, 50, 100, 30);bt1.setBounds(480, 100, 80, 30);bt2.setBounds(600, 100, 180, 30);//修改JLabel motify=new JLabel("修改员工信息");motify.setFont(new Font("宋体",1,18));motify.setForeground(Color.black);JLabel num=new JLabel("员工号");JPanel p1=new JPanel();JComboBox c1=new JComboBox();c1.addItem("请选择需要修改的条件");c1.addItem("用户权限");c1.addItem("姓名");c1.addItem("性别");c1.addItem("生日");c1.addItem("部门");c1.addItem("职务");c1.addItem("受教育程度");c1.addItem("专业技能");c1.addItem("地址");c1.addItem("电话");c1.addItem("电子邮件");c1.addItem("当前状态");c1.addItem("备注");JTextField t1=new JTextField(15);JTextField t2=new JTextField(15);JButton bt3=new JButton("修改");chre.add(bt3);p1.add(c1);p1.add(t2);chre.add(p1);chre.add(check);chre.add(motify);chre.add(num);chre.add(bt1);chre.add(bt2);chre.add(t1);p1.setBounds(275,200,400,30);motify.setBounds(50, 150, 200, 30);bt3.setBounds(675, 200, 100, 30);num.setBounds(50, 200, 50, 30); t1.setBounds(125, 200, 100, 30);//监听器combox.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {String i=(String) combox.getSelectedItem();if(i=="员工号") {seleche="ID";}else if(i=="姓名") {seleche="NAME";}else if(i=="性别") {seleche="SEX";}else if(i=="所属部门") {seleche="DEPARTMENT";}else if(i=="职务") {seleche="JOB";}else if(i=="受教育程度") {seleche="EDU_LEVELL";}else if(i=="专业技能") {seleche="SPCIALTY";}else if(i=="当前状态") {seleche ="STATE";} System.out.println(seleche);}});t.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {if(seleche=="ID"||seleche=="DEPARTMENT"||seleche=="EDU_LEVELL") {String u =t.getText();int i=Integer.valueOf(u).intValue();che1=i;System.out.println(i);}else {String u =t.getText().toString();che2=u;}System.out.println(che1+" "+che2);}});bt1.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {Tableshow t2=new Tableshow();t2.showpart(seleche, che2, che1);}});bt2.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {Tableshow t1=new Tableshow();t1.show(au);}});c1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String i=(String) c1.getSelectedItem();if(i=="用户权限") {seleitem="AUTHORITY";}else if(i=="姓名") {seleitem="NAME";}else if(i=="性别") {seleitem="SEX";}else if(i=="生日") {seleitem="BIRTHDAY";}else if(i=="部门") {seleitem="DEPARTMENT";}else if(i=="职务") {seleitem="JOB";}else if(i=="受教育程度") {seleitem="EDU_LEVELL";}else if(i=="专业技能") {seleitem="SPCIALTY";}else if(i=="地址") {seleitem="ADDRESS";}else if(i=="电话") {seleitem="TEL";}else if(i=="电子邮件") {seleitem="EMAIL";}else if(i=="当前状态") {seleitem="STATE";}else if(i=="备注") {seleitem="REMAPK";}}});bt3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Motify motify=new Motify();int j=motify.chaauth(nsele);if(j>=au) {JOptionPane.showMessageDialog(null,"权限不足!!!","错误",JOptionPane.ERROR_MESSAGE);}else {motify.mo(nsele,seleitem, sele, sele1);}}});t1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t1.getText();int i=Integer.valueOf(u).intValue();nsele=i;}});t2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(seleitem=="AUTHORITY"||seleitem=="BIRTHDAY"||seleitem=="DEPARMENT"||seleitem=="EDU_LEVELL"||seleitem=="TEL") {String u =t2.getText();int i=Integer.valueOf(u).intValue();sele=i;}else {String u =t2.getText().toString();sele1=u;}}});}public void tabshow() {tashow=new JFrame("查询及修改员工信息");tashow.setSize(1000, 700);tashow.setLocation(400, 50);tashow.setLayout(null);tashow.setVisible(true);tashow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);JLabel tille=new JLabel("员工信息表");tille.setFont(new Font("宋体",1,18));tille.setForeground(Color.BLACK);Object[][] data= null;//数据st=new JTable();model=new DefaultTableModel();st.setModel(model);JScrollPane sp=new JScrollPane(st);sp.setBounds(50, 50, 900, 400);tille.setBounds(400, 25, 100, 30);st.setBounds( 10, 10, 900, 400);chre.add(tille); chre.add(sp);}public static void main(String[] args) {}}class Motify{static String sql=null;static DBhelper db1=null;static DBhelper db2=null;static ResultSet ret=null;void mo(int n,String name,int i,String j) { try {if(i==0) {sql="update person set "+name+"='"+j+"' where ID='"+n+"'";}else if(j==null) {sql="update person set "+name+"='"+i+"' where ID='"+n+"'";}db1=new DBhelper(sql);db1.pst.executeUpdate(sql);JOptionPane.showMessageDialog(null, "修改信息成功!!!", "",JOptionPane.PLAIN_MESSAGE);db1.close();} catch (SQLException e) {e.printStackTrace();}}public int chaauth(int id) {int i=0;sql="select AUTHORITY from person where ID="+id;try {db2=new DBhelper(sql);ret=db2.pst.executeQuery();while(ret.next()) {i=ret.getInt("AUTHORITY");}db2.close();ret.close();} catch (SQLException e) {e.printStackTrace();}return i;}
}
6、人事变动界面
//人事变动
public class Change {public int a,b,c;public String d;public int delere;JFrame change;public Change() {JFrame change=new JFrame("人事变更记录");change.setSize(750, 400);change.setLocation(500, 300);change.setLayout(null);change.setVisible(true);change.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//表JButton bt3=new JButton("查询人事变更表");change.add(bt3);bt3.setBounds(450, 100, 150, 30);//删除JLabel dele=new JLabel("删除变更记录");dele.setFont(new Font("宋体", 1, 18));dele.setForeground(Color.black);JLabel recnum1=new JLabel("记录编号");JTextField t=new JTextField(15);JButton bt1=new JButton("删除");change.add(dele);change.add(recnum1);change.add(t);change.add(bt1);dele.setBounds(50, 50, 180, 30);recnum1.setBounds(50, 100, 100, 30);t.setBounds(125,100,100,30);bt1.setBounds(300, 100, 100, 30);//添加JLabel recnum=new JLabel("记录编号");JLabel id=new JLabel("员工号");JLabel num=new JLabel("变更代码");JLabel record=new JLabel("详细");JTextField t1=new JTextField(15);JTextField t2=new JTextField(15);JTextField t3=new JTextField(15);JTextField t4=new JTextField(15);JLabel wri=new JLabel("添加变更记录");wri.setFont(new Font("宋体", 1, 18));wri.setForeground(Color.black);JButton bt2=new JButton("添加");change.add(wri);change.add(recnum);change.add(id);change.add(num);change.add(record);change.add(t1);change.add(t2);change.add(t3);change.add(t4);change.add(bt2);wri.setBounds(50, 150, 150, 30);bt2.setBounds(525, 250, 100, 30);recnum.setBounds(50, 200 ,80, 30);t1.setBounds(125, 200, 100, 30);id.setBounds(300, 200, 80, 30);t2.setBounds(375, 200, 100, 30);num.setBounds(50, 250, 80, 30);t3.setBounds(125, 250, 100, 30);record.setBounds(300, 250, 80, 30);t4.setBounds(375, 250, 100, 30);//监听器bt3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubTableshow t1=new Tableshow();t1.changetable();}});t.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t.getText();int i=Integer.valueOf(u).intValue();delere=i;}});bt1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Chaper c2=new Chaper();c2.dele(delere);}});t1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t1.getText();int i=Integer.valueOf(u).intValue();a=i;}});t2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t2.getText();int i=Integer.valueOf(u).intValue();b=i;}});t3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t3.getText();int i=Integer.valueOf(u).intValue();c=i;}});t4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t4.getText().toString();d=u;}});bt2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Chaper chaper=new Chaper();chaper.addper(a, b, c, d);}});}public static void main(String[] args) {}
}class Chaper{static String sql=null;static DBhelper db1=null;static DBhelper db2=null;static ResultSet ret=null;void addper(int i,int j,int k,String l) { try {sql="insert into personnel values('"+i+"','"+j+"','"+k+"','"+l+"')";db1=new DBhelper(sql);db1.pst.executeUpdate();JOptionPane.showMessageDialog(null, "添加信息成功!!!", "",JOptionPane.PLAIN_MESSAGE);db1.close();} catch (SQLException e) {e.printStackTrace();}}void dele(int i) {try {sql="delete from personnel where ID='"+i+"'";db2=new DBhelper(sql);db2.pst.executeUpdate(sql);JOptionPane.showMessageDialog(null, "删除信息成功!!!", "",JOptionPane.PLAIN_MESSAGE);db2.close();} catch (SQLException e) {e.printStackTrace();}}
}
7、加密界面
//加密
public class Encryption {public int user;public int op;public int np;public Encryption() {JFrame en=new JFrame("密码修改");en.setSize(450, 325);en.setLocation(650, 300);en.setLayout(null);en.setVisible(true);en.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);JLabel id=new JLabel("账号");JLabel oripass=new JLabel("原密码");JLabel nowpass=new JLabel("新密码");id.setFont(new Font("宋体",1,18));id.setForeground(Color.BLACK);oripass.setFont(new Font("宋体",1,18));oripass.setForeground(Color.BLACK);nowpass.setFont(new Font("宋体",1,18));nowpass.setForeground(Color.black);JTextField t1=new JTextField(15);JTextField t2=new JTextField(15);JTextField t3=new JTextField(15);JButton bt1=new JButton("确定"); bt1.setFont(new Font("宋体",1,16));en.setBackground(new Color(225, 200, 150));id.setBounds(100, 50, 100, 30);oripass.setBounds(100, 100, 100, 30);nowpass.setBounds(100, 150, 100, 30);t1.setBounds(175, 50, 150, 30);t2.setBounds(175, 100, 150, 30);t3.setBounds(175, 150, 150, 30);bt1.setBounds(175, 200, 75, 30);en.add(t1);en.add(oripass);en.add(id);en.add(nowpass);en.add(t2);en.add(t3);en.add(bt1);t1.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) { String u =t1.getText();int i=Integer.valueOf(u).intValue();user=i;}});t2.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {String u =t2.getText();int i=Integer.valueOf(u).intValue();op=i;}});t3.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {String u =t3.getText();int i=Integer.valueOf(u).intValue();np=i;}});bt1.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {Verify verify=new Verify();verify.ver(user, op, np);}});}public static void main(String[] args) {}}class Verify{static String sql=null;static DBhelper db1=null;static DBhelper db2=null;static ResultSet ret=null;void ver(int id,int op,int np) {sql="select ID,PASSWD from person where ID="+id;db1=new DBhelper(sql);try {ret=db1.pst.executeQuery();while(ret.next()) {int i=ret.getInt(1);int p=ret.getInt(2);System.out.println(i+" "+p+" "+op);if(i==id&&p==op) {sql="update person set PASSWD="+np+" where ID="+id;db2=new DBhelper(sql);int ret=db2.pst.executeUpdate(sql);JOptionPane.showMessageDialog(null, "密码修改成功!!!", "成功",JOptionPane.PLAIN_MESSAGE);}else {JOptionPane.showMessageDialog(null,"原密码错误!!!","错误",JOptionPane.ERROR_MESSAGE);}}ret.close();db1.close();}catch (SQLException e) {e.printStackTrace();}}
}
8、增删员工界面
//增添
public class Staffadd {public int user;public int id1;public int pass1;public int auth1;public String name1;public String sex1;public int bir1;public int depar1;public String job1;public int edu1;public String spca1;public String address1;public int tel1;public String email1;public String state1;public String rema1;public Staffadd() {JFrame staffd=new JFrame("添加或删除员工信息");staffd.setSize(900, 475);staffd.setLocation(420, 260);staffd.setLayout(null);staffd.setVisible(true);staffd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//删除JLabel dele=new JLabel("删除员工信息");dele.setFont(new Font("宋体",1,18));dele.setForeground(Color.black);JLabel num=new JLabel("员工号");JTextField t=new JTextField(15);JButton bt1=new JButton("删除信息");staffd.add(dele);staffd.add(num);staffd.add(t);staffd.add(bt1);dele.setBounds(50, 50, 150, 30);num.setBounds(50, 100, 50, 30);t.setBounds(125, 100, 100, 30);bt1.setBounds(325, 100, 100, 30);//添加JLabel add=new JLabel("添加员工信息");add.setFont(new Font("宋体",1,18));add.setForeground(Color.black);JLabel id=new JLabel("员工号");JLabel pass=new JLabel("密码");JLabel auth=new JLabel("权限");JLabel name=new JLabel("姓名");JLabel sex=new JLabel("性别");JLabel bir=new JLabel("生日");JLabel depar=new JLabel("所属部门");JLabel job=new JLabel("ְ职务");JLabel edu=new JLabel("受教育程度");JLabel skill=new JLabel("专业技能");JLabel address=new JLabel("地址");JLabel tel=new JLabel("电话");JLabel email=new JLabel("电子邮件");JLabel state=new JLabel("当前状态");JLabel rem=new JLabel("备注");JButton bt2=new JButton("添加信息");JTextField t1=new JTextField(15);JTextField t2=new JTextField(15);JTextField t3=new JTextField(15);JTextField t4=new JTextField(15);JTextField t5=new JTextField(15);JTextField t6=new JTextField(15);JTextField t7=new JTextField(15);JTextField t8=new JTextField(15);JTextField t9=new JTextField(15);JTextField t10=new JTextField(15);JTextField t11=new JTextField(15);JTextField t12=new JTextField(15);JTextField t13=new JTextField(15);JTextField t14=new JTextField(15);JTextField t15=new JTextField(15);staffd.add(add);staffd.add(id);staffd.add(pass);staffd.add(bir);staffd.add(auth);staffd.add(rem);staffd.add(name); staffd.add(sex); staffd.add(auth); staffd.add(bt2);staffd.add(depar);staffd.add(job);staffd.add(edu);staffd.add(skill);staffd.add(address);staffd.add(tel);staffd.add(email);staffd.add(state);staffd.add(t1);staffd.add(t2);staffd.add(t15);staffd.add(t3);staffd.add(t4);staffd.add(t5); staffd.add(t6); staffd.add(t7); staffd.add(t8);staffd.add(t9);staffd.add(t10);staffd.add(t11);staffd.add(t12);staffd.add(t13);staffd.add(t14);add.setBounds(50, 150, 150, 30);bt2.setBounds(720, 350, 100, 30);id.setBounds(50, 200, 50, 30); t1.setBounds(125, 200, 100, 30);pass.setBounds(250, 200, 50, 30); t2.setBounds(325, 200, 100, 30);auth.setBounds(475, 200, 70, 30); t3.setBounds(530, 200, 100, 30);name.setBounds(670, 200, 50, 30); t4.setBounds(720, 200, 100, 30);sex.setBounds(50, 250, 50, 30); t5.setBounds(125,250,100,30);bir.setBounds(250, 250, 50, 30); t6.setBounds(325, 250, 100, 30);depar.setBounds(475, 250, 80, 30); t7.setBounds(530, 250, 100, 30);job.setBounds(670, 250, 50, 30); t8.setBounds(720, 250, 100, 30);edu.setBounds(50, 300, 80, 30); t9.setBounds(125, 300, 100, 30);skill.setBounds(250, 300, 80, 30); t10.setBounds(325, 300, 100, 30);address.setBounds(475, 300, 50, 30); t11.setBounds(530, 300, 100, 30);tel.setBounds(670, 300, 50, 30); t12.setBounds(720, 300, 100, 30);email.setBounds(50, 350, 80, 30); t13.setBounds(125, 350, 100, 30);state.setBounds(250, 350, 80, 30); t14.setBounds(325, 350, 100, 30);rem.setBounds(475, 350, 50, 30); t15.setBounds(530, 350, 100, 30);t.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {String u =t.getText();int i=Integer.valueOf(u).intValue();user=i; }});bt1.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {Delesta delesta=new Delesta();delesta.dele(user);}});t1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t1.getText();int i=Integer.valueOf(u).intValue();id1=i;}});t2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t2.getText();int i=Integer.valueOf(u).intValue();pass1=i;}});t3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t3.getText();int i=Integer.valueOf(u).intValue();auth1=i;}});t4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t4.getText().toString();name1=u;}});t5.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t5.getText().toString();sex1=u;}});t6.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t6.getText();int i=Integer.valueOf(u).intValue();bir1=i;}});t7.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t7.getText();int i=Integer.valueOf(u).intValue();depar1=i;}});t8.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t8.getText();job1=u;}});t9.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t9.getText();int i=Integer.valueOf(u).intValue();edu1=i;}});t10.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t10.getText();spca1=u;}});t11.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t11.getText();address1=u;}});t12.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t12.getText();int i=Integer.valueOf(u).intValue();tel1=i;}});t13.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t13.getText();email1=u;}});t14.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t14.getText();state1=u;}});t15.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String u =t15.getText();rema1=u;}});bt2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Delesta d2=new Delesta();d2.add(id1, pass1, auth1, name1, sex1, bir1, depar1, job1, edu1, spca1, address1, tel1, email1, state1, rema1);}});}public static void main(String[] args) {}}class Delesta{static String sql=null;static DBhelper db1=null;static DBhelper db2=null;void dele(int u) {try {sql="delete from person where ID="+u;db1=new DBhelper(sql);int i=db1.pst.executeUpdate(sql);if(i==1) {JOptionPane.showMessageDialog(null, "删除信息成功!!!", "",JOptionPane.PLAIN_MESSAGE);}db1.close();} catch (SQLException e) {e.printStackTrace();}}void add(int i,int p,int a,String n,String s,int b,int d,String j,int ed,String sp,String ad,int t,String em,String st,String re) {try {sql="insert into person values('"+i+"','"+p+"','"+a+"','"+n+"','"+s+"','"+b+"','"+d+"','"+j+"','"+ed+"','"+sp+"','"+ad+"','"+t+"','"+em+"','"+st+"','"+re+"')";db2=new DBhelper(sql);db2.pst.executeUpdate(sql); JOptionPane.showMessageDialog(null, "添加信息成功!!!", "",JOptionPane.PLAIN_MESSAGE);db2.close();} catch (SQLException e) {e.printStackTrace();}}
}
10、表显示
public class Tableshow {private JTable st;private JFrame tashow;private DefaultTableModel model;public Tableshow() {tashow=new JFrame("员工信息表");tashow.setSize(1000,550);tashow.setLocation(400, 200);tashow.setLayout(null);tashow.setVisible(true);tashow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);JLabel tille=new JLabel("员工信息表");tille.setFont(new Font("宋体",1,18));tille.setForeground(Color.BLACK);Object[][] data= null;//数据st=new JTable();model=new DefaultTableModel();st.setModel(model);JScrollPane sp=new JScrollPane(st);sp.setBounds(50, 50, 900, 400);tille.setBounds(400, 25, 100, 30);st.setBounds( 10, 10, 900, 400);tashow.add(tille); tashow.add(sp);}//显示权限比你低的员工信息public void show(int au) {String sql=null;DBhelper db2=null;ResultSet ret=null;sql="select * from person where AUTHORITY<="+au;db2=new DBhelper(sql);try {ret=db2.pst.executeQuery();ResultSetMetaData rsmd=(ResultSetMetaData) ret.getMetaData();int count =rsmd.getColumnCount();for(int i=1;i<=count;i++) {model.addColumn(rsmd.getColumnName(i));}Object[] row=new Object[count];while(ret.next()) {for(int i=0;i<count;i++) {if(i==3||i==4||i==7||i==9||i==10||i>=12) {row[i]=ret.getString(i+1);}else {row[i]=ret.getInt(i+1);}}model.addRow(row);}ret.close();db2.close();} catch (SQLException e1) {e1.printStackTrace();}}//部分显示信息public void showpart(String n,String c2,int c1) {String sql=null;DBhelper db2=null;ResultSet ret=null;if(c2==null) {sql="select * from person where "+n+"="+c1;}else {sql="select * from person where "+n+"='"+c2+"'";}db2=new DBhelper(sql);try {ret=db2.pst.executeQuery();ResultSetMetaData rsmd=(ResultSetMetaData) ret.getMetaData();int count =rsmd.getColumnCount();for(int i=1;i<=count;i++) {model.addColumn(rsmd.getColumnName(i));}Object[] row=new Object[count];while(ret.next()) {for(int i=0;i<count;i++) {if(i==3||i==4||i==7||i==9||i==10||i>=12) {row[i]=ret.getString(i+1);}else {row[i]=ret.getInt(i+1);}}model.addRow(row);}ret.close();db2.close();} catch (SQLException e1) {e1.printStackTrace();JOptionPane.showMessageDialog(null,"错误!!!","错误",JOptionPane.ERROR_MESSAGE);}st.updateUI();}
//人事记录显示public void changetable() {JLabel annotion=new JLabel("注释:变更代码为0代表新员工加入,1代表职务变动,2代表辞退。");annotion.setFont(new Font("宋体", 1, 14));annotion.setForeground(Color.RED);annotion.setBounds(75, 450, 600, 50);tashow.add(annotion);String sql=null;DBhelper db2=null;ResultSet ret=null;sql="select * from personnel";db2=new DBhelper(sql);try {ret=db2.pst.executeQuery();ResultSetMetaData rsmd=(ResultSetMetaData) ret.getMetaData();int count =rsmd.getColumnCount();for(int i=1;i<=count;i++) {model.addColumn(rsmd.getColumnName(i));}Object[] row=new Object[count];while(ret.next()) {for(int i=0;i<count;i++) {if(i==3) {row[i]=ret.getString(4);}else {row[i]=ret.getInt(i+1);}}model.addRow(row);}ret.close();db2.close();} catch (SQLException e1) {e1.printStackTrace();}}
//显示自己的信息public void self(int id) {String sql=null;DBhelper db2=null;ResultSet ret=null;sql="select ID,NAME,SEX,BIRTHDAY,DEPARTMENT,JOB,EDU_LEVEL,SPCIALTY,ADDRESS,TEL,EMAIL from person where ID="+id;db2=new DBhelper(sql);try {ret=db2.pst.executeQuery();ResultSetMetaData rsmd=(ResultSetMetaData) ret.getMetaData();int count =rsmd.getColumnCount();for(int i=1;i<=count;i++) {model.addColumn(rsmd.getColumnName(i));}Object[] row=new Object[count];while(ret.next()) {for(int i=0;i<count;i++) {if(i==1||i==2||i==5||i==7||i==8||i==10) {row[i]=ret.getString(i+1);}else {row[i]=ret.getInt(i+1);}}}model.addRow(row);ret.close();db2.close();} catch (SQLException e) {// TODO: handle exceptione.printStackTrace();}}public static void main(String[] args) {}}
资源:https://download.csdn.net/download/catkin_isme/11249423