2013年5月9日 00:06:32

不能原谅俺又交了个没修改完善的论文上去了。。水货啊啊

感觉本周的话讲数据库查询算稍微轻松点吧。就是周四的课上完最后章后,数据库的课就结束了周五又该考试了呢。。这么说这周又可以多轻松一天了是吧~~

论文方面今天我问同学了,貌似所有人的8号限定全部交上去吧,那我都不知道我晚上回来才改好的指导老师还会不会给我们交了。。同学说论文方面也要打分的,,今天我瞅了下他的论文。有好些地方都格式不达到要求,,我似乎该松口气了么。。

五一到现在一直弄着论文问题,都感觉蹦得紧张西西的,如果真是8号上交学校的话,不管你写得好坏,这天截至都会收上去了吧?然后不知道要填个什么互联表。。还说要回去,。,。讨厌回去啊。,。反正最后这些过了接下来就差答辩就了事了吧~

好了论文事暂时可搁着了,想多了的话就是浪费时间了。接下来还是小谈学习吧。前几晚不是忘记写blog,是到晚上这个点了知道可以写的,不过实在是忙就没写了。本周初写了以个工资管理系统的小项目吧。主要是要会怎么链接数据库,并用java代码的ui界面操作数据。

(晚上关灯了不好笔记有点纠结啊,键盘倒是不用怎么看。。因为舍友都躺起了)

第一步建工资实体类,

package com.lovo.bean;

public class Salary {
	private int id;//编号
	private String name;//员工姓名
	private int base;//基本工资
	private int bonus;//奖金
	private int mulct;//罚金
	private int reals;//实际工资
	private String moneytime;//发薪时间


注意包名,然后用快捷生成get,set方法和有参无参构造方法(构造器)。

第二步就在SQLyog.exe里建表包含上述属性,实际工资无需建,然后适当添几条数据

CREATE TABLE money(
	id INT PRIMARY KEY AUTO_INCREMENT,
	guyname VARCHAR(20) NOT NULL,
	base INT, 
	bonus INT,
	mulct INT,
	moneytime VARCHAR(20)
	
)DEFAULT CHARSET =utf8;



然后把表save出个文件,丢到Eclipse所在工程里自己新建个放sql的文件夹;

第三步做接口,也是要注意包名,为的是规范。接口的目的是为了以后做项目的规范:

package com.lovo.dao;

import java.util.List;

import com.lovo.bean.Salary;

public interface ISalaryDao {
	public void add(Salary bean);
	
	void del(int id);
	void update(int id,int base,int bonus,int mulct);
	
	public List<Salary>  findAll();
	public List<Salary> findByName(String name);
	public List<Salary> findByTime(String moneytime); 
	
}




第四步做实现类,实现接口中的方法。但是有很多重用的代码和方法,所以可以先建个父类包括这些方法:

package com.lovo.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class BaseDao {

	protected Connection con;
	protected PreparedStatement ps;
	protected ResultSet rs;

	protected void setConnection() {//字符串里的东西说的是不用记,,拷贝就是~~
		try {
			Driver d = new Driver();
			this.con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/lovo?characterEncoding=utf-8",
					"root", "lovo");
			//PreparedStatement	ps=con.prepareStatement(arg0)
			System.out.println(con);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void closeConnection(){//分别关闭3个东西吧
		try {
			if(rs!=null){
			rs.close();	
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
			} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {//记住每写完一个步骤都要测试下完全正确否
		BaseDao bd = new BaseDao();
		bd.setConnection();
		System.out.println(bd.con);
	}

}


然后子类实现:

package com.lovo.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lovo.bean.Salary;
import com.lovo.dao.ISalaryDao;

/**
 * 实现类 implement:实现 接口(ISalaryDao) 继承于 BaseDao,可以重复使用父类中的方法
 * */
public class SalaryDaoImpl extends BaseDao implements ISalaryDao {

	@Override
	// 添加的具体实现方法
	public void add(Salary bean) {//传参Salary对象
		this.setConnection();
		try {
			this.ps = this.con
					.prepareStatement("INSERT INTO money (guyname,base,bonus,mulct,moneytime) VALUES (?,?,?,?,? )");
			this.ps.setString(1, bean.getName());//第一个问号的东西
			this.ps.setInt(2, bean.getBase());
			this.ps.setInt(3, bean.getBonus());
			this.ps.setInt(4, bean.getMulct());
			this.ps.setString(5, bean.getMoneytime());
			this.ps.executeUpdate();//更新数据

			this.closeConnection();
		} catch (SQLException e) {//catch 自动包围的东西,里面SQL三字母最好去掉
			e.printStackTrace();
		}

		this.closeConnection();
	}

	@Override
	public void del(int id) {
		this.setConnection();
		try {
			this.ps = this.con.prepareStatement("delete from money where id=?");
			this.ps.setInt(1, id);
			this.ps.executeUpdate();

			this.closeConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}

		this.closeConnection();
	}

	@Override //修改数据方法
	public void update(int id, int base, int bonus, int mulct) {
		this.setConnection();
		try {
			this.ps = this.con
					.prepareStatement("update money set base=?,bonus=?,mulct=? where id=?");
			this.ps.setInt(1, base);
			this.ps.setInt(2, bonus);
			this.ps.setInt(3, mulct);
			this.ps.setInt(4, id);

			this.ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.closeConnection();
	}

	@Override
	public List<Salary> findAll() {
		this.setConnection();

		List<Salary> list = new ArrayList<Salary>();
		try {
			this.ps = this.con
					.prepareStatement("SELECT m.*,base+bonus-mulct  reals FROM money m");
			this.rs = this.ps.executeQuery();
			while (rs.next()) {
				Salary s = new Salary();

				s.setId(rs.getInt("id"));
				s.setName(rs.getString("guyname"));
				s.setBase(rs.getInt("base"));
				s.setBonus(rs.getInt("bonus"));
				s.setMulct(rs.getInt("mulct"));
				s.setMoneytime(rs.getString("moneytime"));
				s.setReals(rs.getInt("reals"));

				list.add(s);
				// this.ps.executeUpdate();
			}
			this.closeConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	@Override
	public List<Salary> findByName(String name) {
		this.setConnection();

		List<Salary> list = new ArrayList<Salary>();
		try {
			this.ps = this.con
					.prepareStatement("SELECT m.*,base+bonus-mulct  reals FROM money m where guyname like ?");
			this.ps.setString(1, "%" + name + "%");
			this.rs = this.ps.executeQuery();
			while (rs.next()) {
				Salary s = new Salary();

				s.setId(rs.getInt("id"));
				s.setName(rs.getString("guyname"));
				s.setBase(rs.getInt("base"));
				s.setBonus(rs.getInt("bonus"));
				s.setMulct(rs.getInt("mulct"));
				s.setMoneytime(rs.getString("moneytime"));
				s.setReals(rs.getInt("reals"));

				list.add(s);
				// this.ps.executeUpdate();
			}
			this.closeConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	@Override
	public List<Salary> findByTime(String moneytime) {
		this.setConnection();

		List<Salary> list = new ArrayList<Salary>();
		try {
			this.ps = this.con
					.prepareStatement("SELECT m.*,base+bonus-mulct  reals FROM money m where moneytime=?");
			this.ps.setString(1, moneytime);
			this.rs = this.ps.executeQuery();
			while (rs.next()) {
				Salary s = new Salary();

				s.setId(rs.getInt("id"));
				s.setName(rs.getString("guyname"));
				s.setBase(rs.getInt("base"));
				s.setBonus(rs.getInt("bonus"));
				s.setMulct(rs.getInt("mulct"));
				s.setMoneytime(rs.getString("moneytime"));
				s.setReals(rs.getInt("reals"));

				list.add(s);
				// this.ps.executeUpdate();
			}
			this.closeConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	public static void main(String[] args) {//各种测试
		SalaryDaoImpl aa = new SalaryDaoImpl();
		// aa.add(new Salary("刘强",4000,1000,50,"2011-02"));
		// aa.del(2);
		// aa.update(3, 5000,8000, 200);

		List<Salary> list = aa.findByTime("2011-05");
		for (Salary s : list) {
			System.out.println(s.getName() + "  " + s.getBase() + "  "
					+ s.getMoneytime() + "  " + s.getReals());
		}

	}

}



接下来就是ui窗体和数据的放入操作界面了:

package com.lovo.frame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.lovo.bean.Salary;
import com.lovo.dao.ISalaryDao;
import com.lovo.dao.impl.SalaryDaoImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;

public class SalaryFrame extends JFrame{
	private LovoTable table=new LovoTable(this,
			new String[]{"员工姓名","基本工资","奖金","罚金","实际工资","发薪年月"},
			new String[]{"name","base","bonus","mulct","reals","moneytime"},"id");
	
	/**姓名文本框*/
	private LovoTxt nameTxt=new LovoTxt("员工姓名",20,300, this);
	/**奖金文本框*/
	private LovoTxt bonusTxt=new LovoTxt("奖金",20,350, this);
	/**发薪日期文本框*/
	private LovoTxt dateTxt=new LovoTxt("发薪日期",20,400, this);
	/**基本工资文本框*/
	private LovoTxt baseTxt=new LovoTxt("基本工资",250,300, this);
	/**罚金文本框*/
	private LovoTxt mulctTxt=new LovoTxt("罚金",250,350, this);
	/**添??下拉文本框*/
	private LovoComboBox box;
	/**接口指向具体实现类*/
	private ISalaryDao dao=new SalaryDaoImpl();
	
	private JTextField jtxt=new JTextField();
	
	public SalaryFrame(){
		this.setLayout(null);
		this.table.setSizeAndLocation(20, 20, 700, 280);
		List<Salary> list=dao.findAll();//把数据库丢进窗体的表
		table.updateLovoTable(dao.findAll());
		
		LovoButton addButton =new LovoButton("添加",80,500,this);
		LovoButton delButton =new LovoButton("删除",180,500,this);
		LovoButton updataButton =new LovoButton("修改",280,500,this);
		
		JPanel jp=new JPanel();//容器
		jp.setLayout(null);
		Border b=BorderFactory.createLineBorder(Color.blue);//需斟酌
		Border border=BorderFactory.createTitledBorder(b,"查询工资");
		jp.setBorder(border);
		jp.setBounds(470,320,300,180);
		this.add(jp);
		
		jtxt.setBounds(150,20,100,30);
		jp.add(jtxt);
		
		box=new LovoComboBox(new String[]{"员工姓名","发薪年月"},20,20,this);
		jp.add(box);
		LovoButton findButton =new LovoButton("查找",50,100,jp);
		//查找按鈕
		findButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				List<Salary> list=null;
				String item=box.getSelectedItem().toString();
				String value=jtxt.getText();
				if("员工姓名".equals(item)){
					list=dao.findByName(jtxt.getText());
				}
				else{ list=dao.findByTime(jtxt.getText());
				}
				table.updateLovoTable(list);
			}
			
		});
		
		//添加按钮实现
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Salary s=new Salary();
				s.setName(nameTxt.getText());
				s.setBase(Integer.parseInt(baseTxt.getText()));			
				s.setBonus(Integer.parseInt(bonusTxt.getText()));
				s.setMulct(Integer.parseInt(mulctTxt.getText()));
				s.setMoneytime(dateTxt.getText());
				dao.add(s);
				
				List<Salary> list=dao.findAll();
				
				table.updateLovoTable(dao.findAll());
				
			}
		});
		//删除按钮实现
		delButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int id=table.getKey();
				if(id==-1){
					JOptionPane.showMessageDialog(null, "选人哦快点");
				}
				dao.del(id);
				
				table.updateLovoTable(dao.findAll());
				
			}
		});
		
		//修改按钮实现
		updataButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int id = table.getKey();
				if(id==-1){
					JOptionPane.showMessageDialog(null, "点到人╮(╯_╰)╭");}
				
				dao.update( Integer.parseInt(baseTxt.getText()), 
						Integer.parseInt(bonusTxt.getText()),
						Integer.parseInt(mulctTxt.getText()),id);
					
				table.updateLovoTable(dao.findAll());
				System.out.println("草,东西呢?更新呢");
				
			}
		});
		
		this.setSize(800,600);
		this.setVisible(true);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
	}
	
	public static void main(String[] args) {
		SalaryFrame sf=new SalaryFrame();
		
	}
	
}



 



 还是不好意思发无节操的图图看看么

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值