package taotoa.montao.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import taotao.montao.dbcon.DBCon;
import taotao.montao.vo.DeptVO;
import taotao.montao.vo.PageVO;
public class TestDAO {
private DBCon db = null;
private PageVO page = new PageVO();
//获取总的信息数
public int getInfoCount()
{
int infoCount = 0;
//获取Connection连接
db = new DBCon();
Connection conn = db.getConnection();
//创建SQL语句
String strSql = "select * from dept";
//执行方法
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
while(rs.next())
{
infoCount++;
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return infoCount;
}
@SuppressWarnings("unchecked")
public List goupInfo(int nowpager,int pageContent,int pageSizeSize)
{
this.defaultInfo();
page.setNowPage(nowpager);
page.setPageSize(pageSizeSize);
page.setPageCount(pageContent);
List mmm = new ArrayList();
if(nowpager<=0)
{
page.setNowPage(1);
}
int startLine = (page.getNowPage()*page.getPageSize())-page.getPageSize(); //设置起始行
System.out.println("开始的数据: "+startLine);
//返回的结果集对象
List<DeptVO> list = new ArrayList<DeptVO>();
//获取Connection连接
db = new DBCon();
Connection conn = db.getConnection();
//创建SQL语句
String strSql = "select * from dept limit "+startLine+","+page.getPageSize();
System.out.println(strSql);
//执行方法
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
while(rs.next())
{
DeptVO dept = new DeptVO();
dept.setId(rs.getInt("id"));
dept.setName(rs.getString("name"));
dept.setEmail(rs.getString("email"));
dept.setTel(rs.getString("tel"));
dept.setAddress(rs.getString("address"));
list.add(dept);
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
mmm.add(0,list);
mmm.add(1,page);
return mmm;
}
@SuppressWarnings("unchecked")
public List setSize(int pagePageSize)
{
this.defaultInfo();
page.setPageSize(pagePageSize);
if(pagePageSize>=page.getInfoCount())
{
page.setPageCount(page.getInfoCount()/page.getPageSize());
}else
{
page.setPageCount(page.getInfoCount()/page.getPageSize()+1);
}
System.out.println("总的页数: "+page.getPageCount());
List mmm = new ArrayList();
//返回的结果集对象
List<DeptVO> list = new ArrayList<DeptVO>();
//获取Connection连接
db = new DBCon();
Connection conn = db.getConnection();
System.out.println(page.getPageSize());
//创建SQL语句
String strSql = "select * from dept limit 0,"+(page.getPageSize());
System.out.println(strSql);
//执行方法
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
while(rs.next())
{
DeptVO dept = new DeptVO();
dept.setId(rs.getInt("id"));
dept.setName(rs.getString("name"));
dept.setEmail(rs.getString("email"));
dept.setTel(rs.getString("tel"));
dept.setAddress(rs.getString("address"));
list.add(dept);
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
mmm.add(0,list);
mmm.add(1,page);
return mmm;
}
@SuppressWarnings("unchecked")
public List gotoPage(int nowpager,int pageContent,int pageSizeSize)
{
this.defaultInfo();
page.setNowPage(nowpager);
page.setPageSize(pageSizeSize);
page.setPageCount(pageContent);
List mmm = new ArrayList();
if(nowpager>page.getPageCount())
{
page.setNowPage(page.getPageCount()); //如果请求的页面大于总页数
}
int startLine = page.getNowPage()*page.getPageSize()-page.getPageSize(); //设置起始行
//返回的结果集对象
List<DeptVO> list = new ArrayList<DeptVO>();
//获取Connection连接
db = new DBCon();
Connection conn = db.getConnection();
//创建SQL语句
String strSql = "select * from dept limit "+startLine+","+page.getPageSize();
System.out.println(strSql);
//执行方法
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
while(rs.next())
{
DeptVO dept = new DeptVO();
dept.setId(rs.getInt("id"));
dept.setName(rs.getString("name"));
dept.setEmail(rs.getString("email"));
dept.setTel(rs.getString("tel"));
dept.setAddress(rs.getString("address"));
list.add(dept);
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
mmm.add(0,list);
mmm.add(1,page);
return mmm;
}
@SuppressWarnings("unchecked")
public List nextInfo(int nowpager,int pageContent,int pageSizeSize)
{
this.defaultInfo();
page.setNowPage(nowpager);
page.setPageSize(pageSizeSize);
page.setPageCount(pageContent);
List mmm = new ArrayList();
if(nowpager>page.getPageCount())
{
page.setNowPage(page.getPageCount()); //如果请求的页面大于总页数
}
int startLine = (page.getNowPage()-1)*page.getPageSize(); //设置起始行
//返回的结果集对象
List<DeptVO> list = new ArrayList<DeptVO>();
//获取Connection连接
db = new DBCon();
Connection conn = db.getConnection();
//创建SQL语句
String strSql = "select * from dept limit "+startLine+","+page.getPageSize();
System.out.println(strSql);
//执行方法
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
while(rs.next())
{
DeptVO dept = new DeptVO();
dept.setId(rs.getInt("id"));
dept.setName(rs.getString("name"));
dept.setEmail(rs.getString("email"));
dept.setTel(rs.getString("tel"));
dept.setAddress(rs.getString("address"));
list.add(dept);
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
mmm.add(0,list);
mmm.add(1,page);
return mmm;
}
//默认页面显示的数据
@SuppressWarnings("unchecked")
public List defaultInfo()
{
int nowPage = 1;
if (nowPage < 1)
{
nowPage =1;
}
//返回的结果集对象
List<DeptVO> list = new ArrayList<DeptVO>();
//设置页对象
page.setInfoCount(this.getInfoCount()); //设置记录总数
page.setNowPage(nowPage); //设置当前页数
page.setPageSize(5); //设置每页显示数
page.setPageCount(page.getInfoCount()/page.getPageSize()+1); //设置总页数
//获取Connection连接
db = new DBCon();
Connection conn = db.getConnection();
//创建SQL语句
String strSql = "select * from dept limit 0,"+(page.getPageSize());
//执行方法
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
while(rs.next())
{
DeptVO dept = new DeptVO();
dept.setId(rs.getInt("id"));
dept.setName(rs.getString("name"));
dept.setEmail(rs.getString("email"));
dept.setTel(rs.getString("tel"));
dept.setAddress(rs.getString("address"));
list.add(dept);
}
}catch(Exception ex)
{