说明:这是一个实例,其中有些东西是可以抽取出来的,作为一个公共的方法,可是实现复用
public class TestService {
public static TestService test=null;
public static TestService getTestService(){
if(test==null){
test=new TestService();
}
return test;
}
public List getTest(int pageNow ,int pageSize){
String sql="select count(*) from user";
Connection conn=DB.getConn();
Statement stmt=DB.getStmt(conn);
ResultSet rs=DB.getRs(stmt, sql);
int num;
try {
rs.next();
num=rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
sql="select *from user limit "+(pageNow*pageSize-pageSize)+","+pageSize;
PreparedStatement pstmt=DB.getPstmt(conn, sql);
List list=new ArrayList<Test>();
try {
rs=pstmt.executeQuery(sql);
while(rs.next()){
Test t=new Test();
t.setId(rs.getInt("id"));
t.setAge(rs.getInt("age"));
t.setName(rs.getString("name"));
list.add(t);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}