mysql分页怎么实现 mysql分页实现原理( 八 )


displayMessage = "";
columnNameArray = null;
columnTypeArray = null;
currentPageRecordNum = 0;
columnCount = 0;
}
/**功能:数据库初始化操作,其它操作的前提 。
*
* @param conn:数据库连接;
* @param request:jsp页面request对象;
* @param querySQL:查询语句;
* @param pageSize:每页显示记录数;
* @param startPageNum:开始显示页码
*/
public void init(Connection conn, HttpServletRequest request, String querySQL, int pageSize, int startPageNum)
{
if(conn != null)
{
this.request = request;
this.sql = request.getParameter("querySQL");
this.userPara = request.getParameter("userPara");
if(sql == null || sql.equals(""))
{
sql = querySQL;
}
if(this.userPara == null)
{
this.userPara = "";
}
if(S.isContains(sql, "select;from", ";", true))
{
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData r *** d = rs.getMetaData();
columnCount = r *** d.getColumnCount();
columnNameArray = new String[columnCount];
columnTypeArray = new String[columnCount];
String columnName;
String value;
while(rs.next())
{
totalRecord++;
if(totalRecord == 1)
{
for(int i = 0; icolumnCount; i++)
{
columnNameArray[i] = r *** d.getColumnName(i + 1);
columnTypeArray[i] = r *** d.getColumnTypeName(i + 1);
}
}
}
rs.close();
在总记录数大于0的情况下进行下列操作
获取链接图象
if(totalRecord0pageSize0columnCount0startPageNum0)
{
获取总页数
totalPages = totalRecord / pageSize;
int tempNum = totalRecord % pageSize;
if(tempNum != 0)
{
totalPages++;
}
获得当前页页码
String currentPage = request.getParameter("currentPageNum");
currentPageNum = (currentPage == null || currentPage.equals(""))? startPageNum:Integer.parseInt(currentPage);
currentPageNum = (currentPageNumtotalPages)?totalPages:currentPageNum;
currentPageNum = (currentPageNum = 0)?1:currentPageNum;
获得当前页起始显示记录数
pageStartRecord = (currentPageNum - 1) * pageSize + 1;
pageStartRecord = (pageStartRecord = 0)?1:pageStartRecord;
pageStartRecord = (pageStartRecordtotalRecord)?totalRecord:pageStartRecord;
获得当前页显示记录数
if(currentPageNum * pageSizetotalRecord)
{
currentPageRecordNum = totalRecord - (currentPageNum - 1) * pageSize;
}
else
{
currentPageRecordNum = pageSize;
}
resultArray = new String[currentPageRecordNum][columnCount];
用于跳过前面不需显示的记录
int continueRowNum = 0;
用于跳过后面不再显示的记录
int breakRowNum = 0;
ResultSet rs2 = st.executeQuery(sql);
while(rs2.next())
{
跳过前面不需显示的记录
continueRowNum++;
if(continueRowNumpageStartRecord)
{
continue;
}
存取当前页需显示的记录到二维数组
for(int i = 0; icolumnCount; i++)
{
value = https://www.04ip.com/post/rs2.getString(columnNameArray[i]);
value = https://www.04ip.com/post/(value == null)?"":value.trim();
resultArray[breakRowNum][i] = value;
}
跳过后面不再显示的记录
breakRowNum++;
if(breakRowNum = currentPageRecordNum)
{
break;
}
}
rs2.close();
}
st.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
transferSQL(sql);
initSuccessful = true;
}
}
/**功能:数据库初始化操作,其它操作的前提,默认每页显示10条记录 。
*
* @param conn:数据库连接;

推荐阅读