/**
*
*/
package org.pan.util; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import org.pan.contents.Contents;
import org.pan.db.DBConnectionManager;
import org.pan.io.ResUserIO;
/**
* @author admin
*
*/
public class PagerTools
{
/**
*
* @param currentPageNumber
* @return
* @throws Exception
*/
public static List<ResUserIO> getResultSetByPageNumber(Integer currentPageNumber) throws Exception
{
DBConnectionManager dbMan = new DBConnectionManager();
ResultSet fenyeData =null;
Statement stmt = null;
Integer totleDataNumber = null;
List<ResUserIO> resInfoList = null;
try
{
stmt = dbMan.getStatement(); totleDataNumber = getAllDataNumber(stmt);
String pageNumbers = ConfigTools.getInstance().getConfigByConfigName("PAGENUMBERS");
Integer pageNumber = Integer.valueOf(pageNumbers); if(Contents.NO_DATA_IN_DATEBASE.equals(totleDataNumber))
{
return null;
} boolean isFullPage = totleDataNumber>=currentPageNumber*pageNumber ? true:false; if(isFullPage)
{
Integer upLimit = (currentPageNumber-1)*pageNumber+1;
Integer downLimit = currentPageNumber*pageNumber;
String querySql = new StringBuffer().append("select username,phonumber,address from(select rownum r ,t1.username,t1.phonumber,address from tc_re_user t1 where rownum <= ").append(downLimit).append(")t2 where t2.r>=").append(upLimit).toString();
fenyeData = stmt.executeQuery(querySql);
}
else
{
Integer upLimit = (currentPageNumber-1)*pageNumber;
String querySql = new StringBuffer().append("select username,phonumber,address from(select rownum r ,t1.username,t1.phonumber,address from tc_re_user t1 where rownum <= ").append(totleDataNumber).append(")t2 where t2.r>").append(upLimit).toString();
fenyeData = stmt.executeQuery(querySql);
} if(null != fenyeData)
{
resInfoList = new ArrayList<ResUserIO>();
ResUserIO resInfo = null;
while(fenyeData.next())
{
resInfo = new ResUserIO();
resInfo.setUserName(fenyeData.getString("username"));
resInfo.setPhoNumber(fenyeData.getString("phonumber"));
resInfo.setAddress(fenyeData.getString("address"));
resInfoList.add(resInfo);
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
return resInfoList;
} public static Integer getTotlePage(Integer dateNumbers ,Integer eachPageNumber)
{
return dateNumbers%eachPageNumber==0 ? dateNumbers/eachPageNumber : dateNumbers/eachPageNumber+1;
} public static Integer getAllDataNumber(Statement stmt) throws SQLException
{
ResultSet resultSet = stmt.executeQuery("select count(*) from tc_re_user");
Integer totleDataNumber = 0;
if(null != resultSet)
{
while(resultSet.next())
{
totleDataNumber = resultSet.getInt(1);
}
}
return totleDataNumber;
}
}