1.一个表对应一个类,一行对应一个对象,在java中常用的是javabean。javabean就是像豆子一样,只有属性和set、get方法。
package mypro01;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Employee {
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 hiredate=new SimpleDateFormat("yyyy-MM");
try {
this.hireDate = hiredate.parse(hireDate);
} catch (Exception 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 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;
}
}
package mypro01;
import java.util.ArrayList;
import java.util.List;
public class test01 {
public static void main(String[] args) {
Employee e=new Employee(001, "高棋", 3000, "项目部", "2007-01");
Employee e1=new Employee(002, "高棋1", 3000, "项目部1", "2003-02");
Employee e2=new Employee(001, "高棋2", 3000, "项目部1", "2005-04");
List<Employee> list= new ArrayList<Employee>();
list.add(e);
list.add(e1);
list.add(e2);
printAllEmployee(list);
}
public static void printAllEmployee(List<Employee> list) {
for(int i=0; i<list.size();i++) {
System.out.println(list.get(i).getHireDate());
}
}
}
结果:
Mon Jan 01 00:00:00 CST 2007 Sat Feb 01 00:00:00 CST 2003 Fri Apr 01 00:00:00 CST 2005
2.存储数据还有第二种方式,就是一个map对应一样数据,一个二维表中的数据,最后用list进行结合,这也是一种和上面不一样的思路。