一个热爱IT的少年正向您大步走来了

做工资管理系统的种种思考

#详细设计阶段
首先想要说明的是一般做这种系统,因为涉及到界面,用的大多数都是Java,这个阶段你要做的就是,根据总体设计里面谈到的模块,进行代码框架的架构,这个阶段你要做的就是要考虑你的输入是什么,输出是什么。我的设计大体的是这样的,需要有2个人机界面。
第一个界面就是我们说的登录模块,其中里面涉及到管理员登录和员工登录。对了我们这个系统就是针对一般小型公司的工资管理,它需要和其他的系统有连接,就是通过接口,比如(考勤系统等等)。本段程序最主要的算法是如何进行权限判断。下面简单描述该算法。在数据库里为每一个用户都配有一系列的字段,其中有一bool字段叫Controller,若为true,则代表该用户为管理员,若为false,该用户就是一普通用户。本程序的输入就两大类,用户名和密码。
输入项作以下限制。
(1)、用户:必须是数字、字母或则汉字组成,长度限制在6到12字符之间。
(2)、密码:必须是数字、字母组成,长度限制在6到12字符之间。
输出项:
登陆:根据数据库的记载,有三类输出结果,登陆成功显示管理员界面、登陆成功显示普通用户界面、登陆不成功显示错误信息。注册:只有注册成功与注册不成功两类返回结果。

第二个界面就是查询模块。因为我们这个系统主要针对的是公司里的员工,其中员工最想知道这个月的工资的详情,比如应发工资和实际工资。这里就简单介绍下, 应发工资(基本工资+加班费-缺勤费)、实发工资(应发工资-个人所得税-五险一金等)。
输入项:
本段程序只有一类输入数据,即自己的工号,所以只要正确输入自己的工号即可,没有特别的要求。
输出项:
显示我的基本信息,如姓名,工号,性别,年龄等。显示我的工作工资情况,如加班时长,部门基本工资,岗位津贴,医疗报销,个人所得税等。

第二个界面思想简单,没有特殊的算法,只是执行对数据库的查询。

 本人第一次写博客,也怪不好意思的,也许说的不太让大家理解,后期待我代码整理出来,一定给大家双手奉上,对你们以后做类似的系统有很好的启发。今天我在和那个代课老师交流时,他问我能不能在作出来的那个表中,添加一行其他信息。然后我的回答是直接在数据库中添加,他说不行,这样会导致前面的调用数据库中的语句发生改变。哎,做个系统不容易啊,我还要再次修改。
 下面给大家奉上,初期的Java代码。以后会再次修改。

这是用户界面

public class login1() {          //登陆界面
		setTitle("用户登录");
		setSize(WIDTH, HEIGHT);
	setLocation((screenWidth - WIDTH) / 2, (screenHeight - HEIGHT) / 2);			setLayout(new BorderLayout());
		JPanel check = new JPanel();
		check.setBackground(new Color(230,235,250));
		caozuo = new ButtonGroup();
		r1 = new JRadioButton("管理员", true);
		r1.setBackground(new Color(230,235,250));
		caozuo.add(r1);
		r2 = new JRadioButton("职工", false);
		caozuo.add(r2);
		r2.setBackground(new Color(230,235,250));
		check.add(r1);
		check.add(r2);
		add(check, BorderLayout.NORTH);
		JPanel p1 = new JPanel();
		p1.setLayout(new BorderLayout());
		add(p1, BorderLayout.CENTER);
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		p1.add(p2, BorderLayout.NORTH);
		p1.add(p3, BorderLayout.CENTER);
		p1.add(p4, BorderLayout.SOUTH);
		p2.setBackground(new Color(230,235,250));
		p3.setBackground(new Color(230,235,250));
		p4.setBackground(new Color(230,235,250));
		JLabel lno = new JLabel("用户名:");
		JLabel lpassword = new JLabel("密   码: ");
		tname = new JTextField(10);
		tpassword = new JPasswordField(10);
		JButton enter = new JButton("登录");
		JButton exit = new JButton("退出");
		p2.add(lno);
		p2.add(tname);
		p3.add(lpassword);
		p3.add(tpassword);
		p3.setPreferredSize(new Dimension(300, 30));
		p4.setPreferredSize(new Dimension(300, 50));
		p4.add(enter);      
		p4.add(exit);
		this.setVisible(true);
		enter.addActionListener(this);
		exit.addActionListener(this);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
public void actionPerformed(ActionEvent e) {  //验证登录信息正确性
		ResultSet rs = null;
		sqlconn sqlconn = new sqlconn();  //连接数据库
		name = tname.getText();
		String password = tpassword.getText();
		String sql = "";
		try {
			if (e.getActionCommand().equals("登录"))
			{
				if (password.length() != 0)
				{					
					if (r1.isSelected()) 
					{
						sql = "Select * from users where name='" +name+ "' and password='" + password + "'";									
						type=0;
						}
					else{
						sql = "Select * from employees where name='" + name+ "' and password='" + password + "'";									
						type=1;
						}
					rs = sqlconn.getRs(sql);
					if (rs.next()) 
					{
						if (type == 0)
						{
							mainFrame A = new mainFrame();
						     A.setVisible(true);
							}
						else if (type == 1)
						{
							mainFrame1 B=new mainFrame1();
							 B.setVisible(true);
						}
						this.dispose();
					} 
							else {		
JOptionPane.showMessageDialog(null,"用户名称及密码有误,请重新输入!");					
					tname.setText("");		
					tpassword.setText("");								
					 }						
					} 
				else
				{
	JOptionPane.showMessageDialog(null, "密码不能为空,请正确输入!");
				}
			}
		else 
			if (e.getActionCommand().equals("退出")) 
				{
				this.dispose();
				}
		} catch (SQLException ex) {
			System.out.println("sql error!");
		}
	}
	public static void main(String args[]) {
		login1 log= new login1();
			}
}

主界面:

class mainFrame extends Frame implements ActionListener{//实现功能的主界面
		Image img;
		//菜单条式菜单
	MenuBar mb=new MenuBar();
	Menu usersM=new Menu("用户管理");
	Menu employeeM=new Menu("人员管理");
	Menu dtmtM=new Menu("部门管理");
	Menu wageM=new Menu("工资管理");
	Menu helpM=new Menu("帮助");
		MenuItem uInfoMi =new MenuItem("用户信息");//下拉菜单中的选项
	MenuItem emQueryMi=new MenuItem("员工信息查询");
	MenuItem addEmMi=new MenuItem("添加员工信息");
	MenuItem wQueryMi=new MenuItem("工资查询");
	MenuItem addWageMi=new MenuItem("添加工资信息");
	MenuItem dtmtInfoMi=new MenuItem("部门信息");
	MenuItem addDtmtMi=new MenuItem("添加部门信息");
	MenuItem aboutMi=new MenuItem("关于...");
	MenuItem logoutMi=new MenuItem("注销");
		//弹出式菜单
	PopupMenu popM=new PopupMenu();
	MenuItem aveWageMi=new MenuItem("平均工资");
		public mainFrame(){
		setTitle("欢迎使用工资管理系统...");
		setSize(800,507);
		setLayout(null);
		setBackground(Color.LIGHT_GRAY);
		img=getToolkit().getImage(getClass().getResource("xy.jpg"));//设置主窗口中的图片 
		Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
  	Dimension frm=this.getSize();
  	setLocation((int)(scr.getWidth()-frm.getWidth())/2,
             (int)(scr.getHeight()-frm.getHeight())/2-40);
		mb.add(usersM);//加入选项
		mb.add(employeeM);
		mb.add(dtmtM);
		mb.add(wageM);
		mb.add(helpM);
		usersM.add(uInfoMi);
		employeeM.add(emQueryMi);
		employeeM.add(addEmMi);
		wageM.add(wQueryMi);
		wageM.add(addWageMi);
		dtmtM.add(dtmtInfoMi);
		dtmtM.add(addDtmtMi);
		helpM.add(aboutMi);
		helpM.add(logoutMi);
		popM.add(aveWageMi);
		aveWageMi.addActionListener(this);
		this.add(popM);
		this.addMouseListener(new MouseAdapter(){
			//鼠标按键松开事件弹出菜单
			public void mouseReleased(MouseEvent me){
				//检查鼠标事件是否由弹出菜单引发
				if(me.isPopupTrigger())
					//将弹出菜单显示在用户鼠标单击的位置
					popM.show((Component)me.getSource(),me.getX(),me.getY());
			}
		});
		usersM.addActionListener(this);//给下拉式菜单加入事件监听
		employeeM.addActionListener(this);
		wageM.addActionListener(this);
		dtmtM.addActionListener(this);
		helpM.addActionListener(this);
		this.setMenuBar(mb);
		setVisible(true);
		//添加窗口"关闭"按钮的关闭功能
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				confirmExit conE =new confirmExit();
			}
		});
	}
	public void paint(Graphics g){
  	g.setClip(new Rectangle2D.Float(0,0,800,600));
  g.drawImage(img,0,0,this);//将图片加入到窗口中
 	}
public void actionPerformed(ActionEvent ae){//事件监听
	if(ae.getActionCommand().equals("用户信息"))
{
				userInfo user1=new userInfo();
				user1.show();
			}
			if(ae.getActionCommand().equals("员工信息查询")){
				uInfoQue uiq=new uInfoQue();
				uiq.show();
			}
			if(ae.getActionCommand().equals("添加员工信息")){
				addEmInfo aei=new addEmInfo();
				aei.show();
			}
			if(ae.getActionCommand().equals("工资查询")){
				wageQuery wq=new wageQuery();
				wq.show();
			}
			if(ae.getActionCommand().equals("添加工资信息")){
				addWage aw=new addWage();//实例化对应的对象,为函数调用建立接口
				aw.show();
			}
			if(ae.getActionCommand().equals("部门信息")){
				dtmtInfoQue diq=new dtmtInfoQue();
				diq.show();
			}
			if(ae.getActionCommand().equals("添加部门信息")){
				addDtmt ad=new addDtmt();
				ad.show();
			}
			if(ae.getActionCommand().equals("关于...")){
				about ab=new about();
				ab.show();
			}
			if(ae.getActionCommand().equals("注销")){
				logFrame logf1=new logFrame();

用户管理功能

 public userInfo(){
  	super("系统用户管理");
  	setSize(410,250);
  	setResizable(false);
  	this.setLocationRelativeTo(this.getParent());//设置显示的位置
  	setLayout(null);
  	    
  public void initChoice()
  { try{
  		conn=new sqlconn();//连接数据库
      sql="select name from users";//sql语句
    	rs=conn.getRs(sql);
    	while(rs.next()){
    	userChoice.addItem(rs.getString("name"));
    		      }
      conn.dbClose(rs);
      conn=null;
   	}
    catch(Exception e){}
  }
	public void actionPerformed(ActionEvent ee){
		if(ee.getActionCommand().equals("退出"))
    	dispose();
    if(ee.getActionCommand().equals("增加")){
    	sql="select *  from users where name='"+nameTxt.getText()+"'";
      conn=new sqlconn();
      try{
      	rs=conn.getRs(sql);
        if(rs.next()){
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"该用户已经存在!","警告",1);
          conn.dbClose(rs);
          conn=null;
          rs=null;
        }
       	else{
          try{
          	sql="insert into users values('"+nameTxt.getText()+"','"+pwdTxt.getText()+"')";
          	conn=new sqlconn();
          	conn.dbMod(sql);
            conn.dbClose(rs);
            JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"添加成功!","提示信息",1);
          }
          catch(Exception se){}
          userChoice.addItem(nameTxt.getText());
          nameTxt.setText("");
          pwdTxt.setText("");
          conn=null;
     		}
     	}catch(Exception a){}
  	}
		if(ee.getActionCommand().equals("修改")){
      
      sql="select * from users where name='"+nameTxt.getText()+"'";
      	conn=new sqlconn();
      	try{
      		rs=conn.getRs(sql);
        	if(!(rs.next())){
        		JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"用户不存在,修改失败!","警告",1);
          	conn.dbClose(rs);
          	sql="";
          	conn=null;
         	 	rs=null;
         	 	return ;
        	}
        }
 				catch(Exception exc){}
      
      try{
				sql="update users set password='"+pwdTxt.getText()+"'where name='"+nameTxt.getText()+"'";
      	conn=new sqlconn();
      	conn.dbMod(sql);
        conn.dbClose(rs);
        JOptionPane jop=new JOptionPane();
        jop.showMessageDialog(null,"修改成功!","提示信息",1);
      }
      catch(Exception se){}
      nameTxt.setText("");
      pwdTxt.setText("");
      conn=null;
   	}
		if(ee.getActionCommand().equals("删除")){
			JOptionPane jopask=new JOptionPane();
     	if(jopask.showConfirmDialog(null,"你确定要删除此记录吗?","请确认",1)==0){
     		
     		sql="select * from users where name='"+nameTxt.getText()+"'";
      	conn=new sqlconn();
      	try{
      		rs=conn.getRs(sql);
        	if(!(rs.next())){
        		JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"用户不存在,删除失败!","警告",1);
          	conn.dbClose(rs);
          	sql="";
          	conn=null;
         	 	rs=null;
         	 	return ;
        	}
        }
 				catch(Exception exc){}
     		
     		try{
     			conn=new sqlconn();
       	 	sql="delete from users where name='"+nameTxt.getText()+"'";
     			conn.dbMod(sql);
          conn.dbClose(rs);
          JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"删除成功!","提示信息",1);
        }
      	catch(Exception se){}
     		conn=null;
     		userChoice.remove(userChoice.getSelectedIndex());
     		nameTxt.setText("");
     		pwdTxt.setText("");
    	}
		}
	//把用户列表的第一用户名赋值给nameTxt文本框
	public void itemStateChanged(ItemEvent ie){
		nameTxt.setText(userChoice.getSelectedItem());
		try{
	  		conn=new sqlconn();//连接数据库
	      sql="select password from users";//sql语句
	    	rs=conn.getRs(sql);
	    	while(rs.next()){
	    		pwdTxt.setText(rs.getString("password"));
	      }
	      conn.dbClose(rs);
	      conn=null;
	   	}
	    catch(Exception e){}
	    

人员管理模块

public void initChoice()
  { try{
  		conn=new sqlconn();
      sql="select name from employees";
    	rs=conn.getRs(sql);
    	while(rs.next()){
    		userChoice.addItem(rs.getString("name"));
      }
      conn.dbClose(rs);
      conn=null;
   	}
    catch(Exception e){}
  }
  public void actionPerformed(ActionEvent ae){
  	if(ae.getActionCommand().equals("退出"))
  		dispose();
  	if(ae.getActionCommand().equals("查询")){
  		if(idTxt.getText().equals("")&& nameTxt.getText().equals("")){
  			JOptionPane jop=new JOptionPane();
        jop.showMessageDialog(null,"请输入你要查询既内容!","警告",0);
        dispose();
        uInfoQue uiq=new uInfoQue();
        return ;
  		}
  		//sql="select * from employees where id='"+idTxt.getText()+"' and name='"+nameTxt.getText()+"'";
  		if(!(idTxt.getText().equals(""))&&!(nameTxt.getText().equals("")))
  			sql="select * from employees where id='"+idTxt.getText()+"' and name='"+nameTxt.getText()+"'";
  			
  		else 	if(!(idTxt.getText().equals("")))
  						sql="select * from employees where id='"+idTxt.getText()+"'";
  					else
  						sql="select * from employees where name='"+nameTxt.getText()+"'";
  		conn=new sqlconn();
  		try{
      	rs=conn.getRs(sql);
        if(rs.next()){
        	idTxt.setText(rs.getString("id"));
        	nameTxt.setText(rs.getString("name"));
        	sSexLab.setText(rs.getString("sex"));
        	sAgeLab.setText(rs.getString("age"));
        	sPostLab.setText(rs.getString("post"));
        	sDtmtLab.setText(rs.getString("department"));
        	sWageLab.setText(rs.getString("wage"));
        	sWTypeLab.setText(rs.getString("wagetype"));
        	conn.dbClose(rs);
          conn=null;
          rs=null;
        }
        else {
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"该用户不存在!","警告",0);
        }
    	}
    	catch(Exception e){}
  	}
  }
  public void itemStateChanged(ItemEvent ie){
		nameTxt.setText(userChoice.getSelectedItem());
	}
}
添加员工信息的核心代码如下:
    public void initDtmtChoice(){
  	try{
  		conn=new sqlconn();
      sql="select dtmt_name from department";
    	rs=conn.getRs(sql);
    	while(rs.next()){
    		dtmtChoice.addItem(rs.getString("dtmt_name"));
      }
      conn.dbClose(rs);
      conn=null;
   	}
    catch(Exception e){}
  }
  
  public void initWTypeChoice(){
  	int i=1;
  	while(i<=8){
  		wTypeChoice.addItem(i+"级");
  		i++;
  	}
  }
    public void initIdChoice()
  { try{
  		conn=new sqlconn();
      sql="select id from employees";
    	rs=conn.getRs(sql);
    	while(rs.next()){
    		idChoice.addItem(rs.getString("id"));
      }
      conn.dbClose(rs);
      conn=null;
   	}
    catch(Exception e){}
  }
  public void actionPerformed(ActionEvent ae){
  	if(ae.getActionCommand().equals("退出"))
  		dispose();
  	if(ae.getActionCommand().equals("清空")){
  		idTxt.setText("");
    	nameTxt.setText("");
    	sexTxt.setText("");
    	ageTxt.setText("");
  	  postTxt.setText("");
  	  wageTxt.setText("");
  	}
  	if(ae.getActionCommand().equals("添加")){
    	sql="select * from employees where id='"+idTxt.getText()+"'";
      conn=new sqlconn();
      try{
      	rs=conn.getRs(sql);
        if(rs.next()){
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"该员工已经存在!","警告",1);
          conn.dbClose(rs);
          conn=null;
          rs=null;
        }
       	else{
          try{
          	sql="insert into employees values('"+idTxt.getText()+"','"+nameTxt.getText()+"','"+sexTxt.getText()+"','"
          	+ageTxt.getText()+"','"+postTxt.getText()+"','"+dtmtChoice.getSelectedItem()+"','"+wageTxt.getText()+"','"
          	+wTypeChoice.getSelectedItem()+"')";
          	conn=new sqlconn();
          	conn.dbMod(sql);
            conn.dbClose(rs);
            JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"添加成功!","提示信息",1);
          	sql="insert into wages values('"+idTxt.getText()+"','"+nameTxt.getText()+"','"+wTypeChoice.getSelectedItem()+"','','','','','')";
          	connT=new sqlconn();
          	connT.dbMod(sql);
          	connT.dbClose(rs);
          }
          catch(Exception se){}
          idChoice.addItem(idTxt.getText());
          idTxt.setText("");
          nameTxt.setText("");
          sexTxt.setText("");
          ageTxt.setText("");
          postTxt.setText("");
          //dtmtTxt.setText("");
          wageTxt.setText("");
          //wTypeTxt.setText("");
          conn=null;
          connT=null;
     		}
     	}catch(Exception a){}
  	}
		if(ae.getActionCommand().equals("修改")){
			sql="select * from wages where id='"+idTxt.getText()+"'";
      conn=new sqlconn();
      try{
      	rs=conn.getRs(sql);
        if(!(rs.next())){
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"用户不存在,修改失败!","警告",1);
          conn.dbClose(rs);
          sql=""7conn=null; rs=null; 	return ;
                	}
    	}
      catch(Exception exc){}
			
      try{
	sql="update employees set name='"+nameTxt.getText()+"',sex='"+sexTxt.getText()+"',age='"	+ageTxt.getText()+"',post='"+postTxt.getText()+"',department='"+dtmtChoice.getSelectedItem()	+"',wage='"+wageTxt.getText()+"',wagetype='"+wTypeChoice.getSelectedItem()+"'where id='"+idTxt.getText()+"'";
      	conn=new sqlconn();
      	conn.dbMod(sql);
        conn.dbClose(rs);
        JOptionPane jop=new JOptionPane();
        jop.showMessageDialog(null,"修改成功!","提示信息",1);
      }
      catch(Exception se){}
      idTxt.setText("");
      nameTxt.setText("");
      sexTxt.setText("");
      ageTxt.setText("");
      postTxt.setText("");
      //dtmtTxt.setText("");
      wageTxt.setText("");
      //wTypeTxt.setText("");
      conn=null;
   	}
		if(ae.getActionCommand().equals("删除")){
			JOptionPane jopask=new JOptionPane();
     	if(jopask.showConfirmDialog(null,"你确定要删除此记录吗?","请确认",1)==0){
     		try{
     			conn=new sqlconn();
       	 	sql="delete from employees where id='"+idTxt.getText()+"'";
     			conn.dbMod(sql);
          conn.dbClose(rs);
          JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"删除成功!","提示信息",1);
          sql="delete from wages where id='"+idTxt.getText()+"'";
          connT=new sqlconn();
          connT.dbMod(sql);
          connT.dbClose(rs);
        }
      	catch(Exception se){}
     		conn=null;
     		connT=null;
     		idChoice.remove(idChoice.getSelectedIndex());
     		idTxt.setText("");
        nameTxt.setText("");
        sexTxt.setText("");
        ageTxt.setText("");
        postTxt.setText("");
        //dtmtTxt.setText("");
        wageTxt.setText("");
        //wTypeTxt.setText("");
    	}} }
      public void itemStateChanged(ItemEvent ie){
  	String sqlTemp;
		ResultSet rsTemp=null;
		idTxt.setText(idChoice.getSelectedItem());
		sqlTemp="select * from employees where id='"+idTxt.getText()+"'";
		try{
			sqlconn sConn=new sqlconn();
			rsTemp=sConn.getRs(sqlTemp);
			if(rsTemp.next()){
				idTxt.setText(rsTemp.getString("id"));
				nameTxt.setText(rsTemp.getString("name"));
				sexTxt.setText(rsTemp.getString("sex"));
				ageTxt.setText(rsTemp.getString("age"));
				postTxt.setText(rsTemp.getString("post"));
				wageTxt.setText(rsTemp.getString("wage"));
				//fWageTxt.setText(rsTemp.getString("factwage"));
				dtmtChoice.select(rsTemp.getString("department"));
				wTypeChoice.select(rsTemp.getString("wagetype"));
				sConn.dbClose(rsTemp);
				sConn=null;
				rsTemp=null;
				sConn=null;
			}
		}
		catch(Exception e){
			System.out.println("列表连接数据库出错!");}}
			

部门管理模块:

public void actionPerformed(ActionEvent ae){
  	if(ae.getActionCommand().equals("退出"))
  		dispose();
  	if(ae.getActionCommand().equals("查询")){
  		if(idTxt.getText()==""){
  			JOptionPane jop=new JOptionPane();
        jop.showMessageDialog(null,"请输入你要查询既内容!","警告",0);
        dispose();
        dtmtInfoQue diq=new dtmtInfoQue();
      }
      sql="select * from department where dtmt_id='"+idTxt.getText()+"'";
      conn=new sqlconn();
  		try{
      	rs=conn.getRs(sql);
        if(rs.next()){
        	idTxt.setText(rs.getString("dtmt_id"));
        	nameTxt.setText(rs.getString("dtmt_name"));
        	conn.dbClose(rs);
          conn=null;
          rs=null;
        }
        else{
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"该用户不存在!","警告",0);
        }
      }
      catch(Exception e){}
    }
  }
添加部门信息核心代码:
public void actionPerformed(ActionEvent ae){
  	if(ae.getActionCommand().equals("退出"))
  		dispose();
  	if(ae.getActionCommand().equals("清空")){
  		idTxt.setText("");
    	nameTxt.setText("");
    	sexTxt.setText("");
    	ageTxt.setText("");
  	  postTxt.setText("");
  	  wageTxt.setText("");
  	}
  	if(ae.getActionCommand().equals("添加")){
    	sql="select * from employees where id='"+idTxt.getText()+"'";
      conn=new sqlconn();
      try{
      	rs=conn.getRs(sql);
        if(rs.next()){
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"该员工已经存在!","警告",1);
          conn.dbClose(rs);
          conn=null;
          rs=null;
        }
       	else{
          try{
          	sql="insert into employees values('"+idTxt.getText()+"','"+nameTxt.getText()+"','"+sexTxt.getText()+"','"
          	+ageTxt.getText()+"','"+postTxt.getText()+"','"+dtmtChoice.getSelectedItem()+"','"+wageTxt.getText()+"','"
          	+wTypeChoice.getSelectedItem()+"')";
          	conn=new sqlconn();
          	conn.dbMod(sql);
            conn.dbClose(rs);
            JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"添加成功!","提示信息",1);
          	sql="insert into wages values('"+idTxt.getText()+"','"+nameTxt.getText()+"','"+wTypeChoice.getSelectedItem()+"','','','','','')";
          	connT=new sqlconn();
          	connT.dbMod(sql);
          	connT.dbClose(rs);
          }
          catch(Exception se){}
          idChoice.addItem(idTxt.getText());
          idTxt.setText("");
          nameTxt.setText("");
          sexTxt.setText("");
          ageTxt.setText("");
          postTxt.setText("");
          //dtmtTxt.setText("");
          wageTxt.setText("");
          //wTypeTxt.setText("");
          conn=null;
          connT=null;
     		}
     	}catch(Exception a){}
  	}
		if(ae.getActionCommand().equals("修改")){
			sql="select * from wages where id='"+idTxt.getText()+"'";
      conn=new sqlconn();
      try{
      	rs=conn.getRs(sql);
        if(!(rs.next())){
        	JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"用户不存在,修改失败!","警告",1);
          conn.dbClose(rs);
          sql="";
          conn=null;
         	rs=null;
         	return ;
       	}}
      catch(Exception exc){}
      try{
				sql="update employees set name='"+nameTxt.getText()+"',sex='"+sexTxt.getText()+"',age='"
				+ageTxt.getText()+"',post='"+postTxt.getText()+"',department='"+dtmtChoice.getSelectedItem()
				+"',wage='"+wageTxt.getText()+"',wagetype='"+wTypeChoice.getSelectedItem()+"'where id='"+idTxt.getText()+"'";
      	conn=new sqlconn();
      	conn.dbMod(sql);
        conn.dbClose(rs);
        JOptionPane jop=new JOptionPane();
        jop.showMessageDialog(null,"修改成功!","提示信息",1);
      }
      catch(Exception se){}
      idTxt.setText("");
      nameTxt.setText("");
      sexTxt.setText("");
      ageTxt.setText("");
      postTxt.setText("");
      //dtmtTxt.setText("");
      wageTxt.setText("");
      //wTypeTxt.setText("");
      conn=null;
   	}
		if(ae.getActionCommand().equals("删除")){
			JOptionPane jopask=new JOptionPane();
     	if(jopask.showConfirmDialog(null,"你确定要删除此记录吗?","请确认",1)==0){
     		try{
     			conn=new sqlconn();
       	 	sql="delete from employees where id='"+idTxt.getText()+"'";
     			conn.dbMod(sql);
          conn.dbClose(rs);
          JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"删除成功!","提示信息",1);
          sql="delete from wages where id='"+idTxt.getText()+"'";
          connT=new sqlconn();
          connT.dbMod(sql);
          connT.dbClose(rs);
        }
      	catch(Exception se){}
     		conn=null;
     		connT=null;
     		idChoice.remove(idChoice.getSelectedIndex());
     		idTxt.setText("");
        nameTxt.setText("");
        sexTxt.setText("");
        ageTxt.setText("");
        postTxt.setText("");
        wageTxt.setText("");
    	}
		}	
    public void itemStateChanged(ItemEvent ie){
  	String sqlTemp;
		ResultSet rsTemp=null;
		idTxt.setText(idChoice.getSelectedItem());
		sqlTemp="select * from employees where id='"+idTxt.getText()+"'";
		try{
			sqlconn sConn=new sqlconn();
			rsTemp=sConn.getRs(sqlTemp);
			if(rsTemp.next()){
				idTxt.setText(rsTemp.getString("id"));
				nameTxt.setText(rsTemp.getString("name"));
				sexTxt.setText(rsTemp.getString("sex"));
				ageTxt.setText(rsTemp.getString("age"));
				postTxt.setText(rsTemp.getString("post"));
				wageTxt.setText(rsTemp.getString("wage"));
				dtmtChoice.select(rsTemp.getString("department"));
				wTypeChoice.select(rsTemp.getString("wagetype"));
				sConn.dbClose(rsTemp);
				sConn=null;
				rsTemp=null;
				sConn=null;
			}
		}
		catch(Exception e){
			System.out.println("列表连接数据库出错!");
		}}}

工资管理模块:

public void actionPerformed(ActionEvent ae){
  	if(ae.getActionCommand().equals("退出"))
  		dispose();
  	if(ae.getActionCommand().equals("清空")){
    	bWageTxt.setText("");
    	bonusTxt.setText("");
    	mWageTxt.setText("");
    	fWageTxt.setText("");
  	}

		if(ae.getActionCommand().equals("修改")){//getActionCommand()用来获得与当前动作相关的命令字符串
      try{
          
        //测试部分 
      	sql="select * from wages where id='"+idTxt.getText()+"'";
      	conn=new sqlconn();
      	try{
      		rs=conn.getRs(sql);
        	if(!(rs.next())){
        		JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"用户不存在,修改失败!","警告",1);
          	conn.dbClose(rs);
          	sql="";
          	conn=null;
         	 	rs=null;
         	 	return ;
        	}
        }
        catch(Exception exc){}
        //以上是测试部分
        sql="update wages set name='"+nameTxt.getText()+"',wagetype='"+wTypeTxt.getText()+"',baswage='"
				+bWageTxt.getText()+"',bonus='"+bonusTxt.getText()+"',minwage='"+mWageTxt.getText()+"',factwage='"
				+fWageTxt.getText()+"',wagedate='"+wDateTxt.getText() +"'where id='"+idTxt.getText()+"'";
      	conn=new sqlconn();
      	conn.dbMod(sql);
        conn.dbClose(rs);
        JOptionPane jop=new JOptionPane();
        jop.showMessageDialog(null,"修改成功!","提示信息",1);
        //System.out.println(fWageTxt.getText());
        sql="update employees set wage='"+fWageTxt.getText()+"' where id='"+idTxt.getText()+"'";
        connT=new sqlconn();
        connT.dbMod(sql);
        connT.dbClose(rs);
      }
      catch(Exception se){
      	System.out.println("Question");
      }
      idTxt.setText("");
      nameTxt.setText("");
      wTypeTxt.setText("");
      bWageTxt.setText("");
      bonusTxt.setText("");
      mWageTxt.setText("");
      fWageTxt.setText("");
      wDateTxt.setText("");
      conn=null;
      connT=null;
		}
		if(ae.getActionCommand().equals("删除")){
			JOptionPane jopask=new JOptionPane();
     	if(jopask.showConfirmDialog(null,"你确定要删除此记录吗?","请确认",1)==0){
     		int i=0;
     		sql="select * from wages where id='"+idTxt.getText()+"'";
      	conn=new sqlconn();
      	try{
      		rs=conn.getRs(sql);
      		while(rs.next()){
      			i++;
      		}
      		if(i!=0&&i<2){
      			JOptionPane jopt=new JOptionPane();
      			jopt.showMessageDialog(null,"一个员工至少有一条工资记录,删除失败!","警告",1);
      		}
        	else if(!(rs.next())){
        		JOptionPane jop=new JOptionPane();
          	jop.showMessageDialog(null,"用户不存在,删除失败!","警告",1);
        	}
          conn.dbClose(rs);
          sql="";
          conn=null;
         	rs=null;
         	return ;
        }
        catch(Exception exc){}
     		try{
     			conn=new sqlconn();
       	 	sql="delete from wages where id='"+idTxt.getText()+"'";
     			conn.dbMod(sql);
          conn.dbClose(rs);
          JOptionPane jop=new JOptionPane();
          jop.showMessageDialog(null,"删除成功!","提示信息",1);
        }
      	catch(Exception se){}
     		conn=null;
     		idChoice.remove(idChoice.getSelectedIndex());
     		idTxt.setText("");
        nameTxt.setText("");
        wTypeTxt.setText("");
        bWageTxt.setText("");
        bonusTxt.setText("");
        mWageTxt.setText("");
        fWageTxt.setText("");
    	}
		}
	}
	
	public void itemStateChanged(ItemEvent ie){ //该方法在用户已选定或取消选定某项时调用.执行需要在已选定(或已取消选定)项时发生的操作
		String sqlTemp;
		ResultSet rsTemp=null;
		String t;
		idTxt.setText(idChoice.getSelectedItem());
		
		sqlTemp="select * from wages where id='"+idTxt.getText()+"'";
		try{
			sqlconn sConn=new sqlconn();
			rsTemp=sConn.getRs(sqlTemp);
			if(rsTemp.next()){
				nameTxt.setText(rsTemp.getString("name"));
				wTypeTxt.setText(rsTemp.getString("wagetype"));
				bWageTxt.setText(rsTemp.getString("baswage"));
				bonusTxt.setText(rsTemp.getString("bonus"));
				mWageTxt.setText(rsTemp.getString("minwage"));
				fWageTxt.setText(rsTemp.getString("factwage"));
				wDateTxt.setText(rsTemp.getString("wagedate"));
				System.out.println(rsTemp.getString("id"));
				sConn.dbClose(rsTemp);
				sConn=null;
				rsTemp=null;
				sConn=null;
			}
		}
		catch(SQLException se){
			System.out.println("列表连接数据库出错!");
		}
	}
	public void textValueChanged(TextEvent te){//该方法在文本的值已改变时调用
		String t1,t2,t3,fw;
		double td1,td2,td3,sum;
		t1=bWageTxt.getText();
		t2=bonusTxt.getText();
		t3=mWageTxt.getText();
		if(t1.equals(""))
			t1="0";
		if(t2.equals(""))
			t2="0";
		if(t3.equals(""))
			t3="0";
		try{
		td1=Double.parseDouble(t1);//返回一个新的 double 值,该值被初始化为用指定 String 表示的值,这与 Double 类的 valueOf 方法产生的值类似。
		td2=Double.parseDouble(t2);
		td3=Double.parseDouble(t3);
		sum=td1+td2-td3;
		fw=String.valueOf(sum);
		fWageTxt.setText(fw);
		//fWageTxt.setText("1");
		}
		catch(NumberFormatException dfe){}
	} } 		

 #  这里关于数据库如何和Java连接的问题,用到的是JDBC,相关内容后序给大家介绍啦。现在已经是凌晨32分了。以上有错误的请大佬为我指正。后序一定会把我每天的所学都会在这里给大家分享。还有现在大家用的基本数据库都是mysql,后面也会给出教程的,嘿嘿!## 标题
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值