数据存储综合练习_javabean的介绍

如何存储如下表格中的数据

ID 姓名 薪水 单位 入职时间
0301 Matrix 33000 A 2014-02
0302 Tom 13000 B 2015-02
0303 Ada 23000 C 2013-02
0304 Jack 9000 D 2016-02

如何使用容器将这些数据存储起来?

Employee.java

package junit.matrix.collections;

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

/**
 * 雇员实体类对应数据库表雇员表
 * 
 * Employee<BR>
 * 创建人:Matrix <BR>
 * 时间:2016年2月25日-下午8:08:10 <BR>
 * 
 * @version 1.0.0
 *
 */
public class Employee {

    // 属性一般都私有
    // ID
    private int id;
    // 姓名
    private String name;
    // 薪水
    private int salary;
    // 单位
    private String department;
    // 入职
    private Date hireDate;

    public Employee(int id, String name, int salary, String department, String hireDate) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.department = department;
        DateFormat format = new SimpleDateFormat("yyyy-MM");
        try {
            this.hireDate = format.parse(hireDate);
        } catch (Exception e1) {
            e1.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 int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public Date getHireDate() {
        return hireDate;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }

}

Test01.java

package junit.matrix.collections;

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

public class Test01 {

    public static void main(String[] args) {

        // 一个对象代表一行记录!

        // Employee e = new Employee();
        // e.setId(0301);
        // e.setName("Matrix");
        // e.setDepartment("A");
        // e.setSalary(3000);
        // String strDate = "2015-10";
        // DateFormat format = new SimpleDateFormat("yyyy-MM");
        // try {
        // e.setHireDate(format.parse(strDate));
        // } catch (Exception e1) {
        // e1.printStackTrace();
        // }
        //
        // System.out.println(e.getDepartment());

        Employee e = new Employee(0301, "Matrix", 33000, "A", "2014-02");
        Employee e2 = new Employee(0302, "Tom", 13000, "B", "2015-02");
        Employee e3 = new Employee(0303, "Ada", 23000, "C", "2013-02");
        Employee e4 = new Employee(0304, "Jack", 9000, "D", "2016-02");

        List<Employee> list = new ArrayList<Employee>();
        list.add(e);
        list.add(e2);
        list.add(e3);
        list.add(e4);

        printEmpName(list);

    }

    public static void printEmpName(List<Employee> list) {
        for (int i = 0; i < list.size(); i++) {
            // 打印容器中所有雇员的名字
            System.out.println(list.get(i).getName());
        }
    }
}

运行结果:

Matrix
Tom
Ada
Jack

你可能感兴趣的:(javabean)