javabean实现数据存储

本文介绍如何使用JavaBean实现对员工信息的存储,包括工号、姓名、部门、薪水及入职时间等字段,并展示了如何创建Employee类及其实例化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    通过javabean实现对如下一张表格中的数据进行存储
工号姓名部门薪水入职时间
001张三研发30002016-9
002李四   财务30002016-9
003王五采购30002016-9

首先实现一个员工类,类中实现员工属性,并加入setter和getter方法,添加构造器

实现一个Employee类如下:

/**
 * 
 */
package com.javabean;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
*@author 作者:sunlei
*@version 创建时间:2018年3月2日下午3:59:05
*说明
*/
public class Employee {
	private int id;
	private String name;
	private String department;
	private int salary;
	private Date entryDate;
	
	
	/**
	 * @param id
	 * @param name
	 * @param department
	 * @param salary
	 * @param entryDate
	 */
	public Employee(int id, String name, String department, int salary, String entryDate) {
		super();
		this.id = id;
		this.name = name;
		this.department = department;
		this.salary = salary;
		
		DateFormat format = new SimpleDateFormat("yyyy-MM");
		try {
			this.entryDate = format.parse(entryDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public Date getEntryDate() {
		return entryDate;
	}
	public void setEntryDate(Date entryDate) {
		this.entryDate = entryDate;
	}

}

注意Date的用法:

DateFormat format = new SimpleDateFormat("yyyy-MM");

Date time = format.parse(entryDate);

/**
 * 
 */
package com.javabean;

import java.util.ArrayList;
import java.util.List;

/**
*@author 作者:sunlei
*@version 创建时间:2018年3月2日下午4:05:02
*说明
*/
public class Test {
	
	public static void printname(List<Employee>list) {
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i).getName());
		}
	}

	public static void main(String[] args) {
		List<Employee> list = new ArrayList<Employee>();
		Employee e1 = new Employee(001,"张三","研发",3000,"2016-9");
		Employee e2 = new Employee(001,"李四","财务",3000,"2016-9");
		Employee e3 = new Employee(001,"王五","采购",3000,"2016-9");
		list.add(e1);
		list.add(e2);
		list.add(e3);
		printname(list);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值