第四步:封装从数据库取出数据的方法(prisoner表)
package com.sc.dao;
import java.util.*;
import java.sql.*;
import com.sc.bean.prisoner;
import com.sc.tool.BaseDao;
/**
* prisoner
* @author ZhangShaoWen
*
*/
public class PrisonerDao {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public List getPrisoner(){
List list = new ArrayList();
conn = BaseDao.getConnection();
try {
//查询的sql语句
String sql = "select * from prisoner";
//查询前的准备
ps = conn.prepareStatement(sql);
//执行查询 返回结果集
rs = ps.executeQuery();
//
while(rs.next()){
prisoner prs = new prisoner();
prs.setId(rs.getInt("id"));
prs.setNumber(rs.getString("number"));
prs.setName(rs.getString("name"));
prs.setAge(rs.getInt("age"));
prs.setBirthday(rs.getString("birthday"));
prs.setSex(rs.getString("sex"));
prs.setNation(rs.getString("nation"));
prs.setAddress(rs.getString("address"));
prs.setPhone(rs.getString("phone"));
prs.setInTime(rs.getString("inTime"));
prs.setTime(rs.getInt("time"));
prs.setaNumber(rs.getInt("aNumber"));
prs.setOutTime(rs.getString("outTime"));
prs.setTtype(rs.getInt("Ttype"));
prs.setStation(rs.getInt("station"));
prs.setScore(rs.getInt("score"));
//将对象存放到list集合中
list.add(prs);
}
//关闭数据库资源
BaseDao.closeAll(conn, ps, rs);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//
public static void main(String[] args) {
List list = new PrisonerDao().getPrisoner();
//输出集合中的数据
for(Object ob:list){
prisoner prs = (prisoner)ob;
System.out.println(prs.getName());
}
}
}