public static List getAllDepartment(){
[color=red](1)创建List类的对象list,用来实现其Add方法[/color]
List list = new Arraylist();
[color=red](2)调用构建好的SQL和JAVA运行环境,并创建ResultSet类的对象rs来存储缓存表格[/color]
Connection con=DMmanager.getConnection();
PreparedStatement pst= null;
String str = "select * from department";
ResultSet rs =null;
pst = con.PrepareStatement(str);
rs = pst.executeQuery();
[color=red](3)利用一个while 将rs缓存表中的每一列Columns属性/ 通过 Department类对象dep的方法获取table中Columns的属性[/color]
while(rs.hasNext()){
Department dep = new Department();
dep.setId(rs.getInt("department_id"));
dep.setName(rs.getString("department_name"));
dep.setSalary(rs.getFlaot("salary"));
dep.setLocation_id(rs.getInt("location_id"));
[color=red](4) 将(3)的结果add到list中[/color]
list.add(dep);
}
rs.close();
pst.close();
con.close();
return list;
}
[color=red] (5)构建一个输出方法[/color]
public static void printAll(Department dep){
System.out.println(dep.getId());
System.out.println(dep.getName());
System.out.println(dep.getSalary());
System.out.println(dep.getLocation_id());
}
[color=red](6)运行主函数[/color]
public static void main(String[] args) {
List list=getAllDepartment();
iterator it =list.iterator();
while(it.hasNext()){
Department dep =new Department();
printAll(dep);
}
}
[color=red](1)创建List类的对象list,用来实现其Add方法[/color]
List list = new Arraylist();
[color=red](2)调用构建好的SQL和JAVA运行环境,并创建ResultSet类的对象rs来存储缓存表格[/color]
Connection con=DMmanager.getConnection();
PreparedStatement pst= null;
String str = "select * from department";
ResultSet rs =null;
pst = con.PrepareStatement(str);
rs = pst.executeQuery();
[color=red](3)利用一个while 将rs缓存表中的每一列Columns属性/ 通过 Department类对象dep的方法获取table中Columns的属性[/color]
while(rs.hasNext()){
Department dep = new Department();
dep.setId(rs.getInt("department_id"));
dep.setName(rs.getString("department_name"));
dep.setSalary(rs.getFlaot("salary"));
dep.setLocation_id(rs.getInt("location_id"));
[color=red](4) 将(3)的结果add到list中[/color]
list.add(dep);
}
rs.close();
pst.close();
con.close();
return list;
}
[color=red] (5)构建一个输出方法[/color]
public static void printAll(Department dep){
System.out.println(dep.getId());
System.out.println(dep.getName());
System.out.println(dep.getSalary());
System.out.println(dep.getLocation_id());
}
[color=red](6)运行主函数[/color]
public static void main(String[] args) {
List list=getAllDepartment();
iterator it =list.iterator();
while(it.hasNext()){
Department dep =new Department();
printAll(dep);
}
}