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

This blog demonstrates how to store employee data using Java classes and ArrayList, including ID, name, salary, department, and hire date management.

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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MatrixSparse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值