查询Departments表中所有的信息(JDBC方法)--笔记

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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值