Home > Web Front-end > JS Tutorial > body text

A brief analysis of jsp reading database to implement paging technology

高洛峰
Release: 2016-12-29 15:17:23
Original
1447 people have browsed it

This article introduces the use of javabean and jsp pages to realize paging display of data. The database used in the example is Mysql.

1. First look at javabean
Class name:
databaseBean.java:
The following is the code of databaseBean.java:

package database_basic; 
import java.sql.*; 
import java.util.*; 
public class databaseBean 
{ 
//这是默认的数据库连接方式 
private String DBLocation="jdbc:mysql://localhost/onestoptech?user=root&password=password&useUnicode=true&characterEncoding=GB2312"; 
private String DBDriver="org.gjt.mm.mysql.Driver"; 
private Connection conn=null; 
public databaseBean(){} 
//通过set方法可以灵活设置数据库的连接 
public void setDBLocation(String location){DBLocation=location;} 
public void setDBDriver(String driver){DBDriver=driver;} 
public void setconn(Connection conn){this.conn=conn;} 
public String getDBLocation(){return(DBLocation);} 
public String getDBDriver(){return(DBDriver);} 
public Connection getconn(){return(conn);} 
/*public String DBConnect(){} 
public String DBDisconnect(){} 
public ResultSet query(String sql){} 
public int getTotalPage(String sql,int pageSize){} 
public ResultSet getPagedRs(String sql,int pageSize,int pageNumber){} 
public String execute_sql(String sql){}*/ 
//建立连接 
public String DBConnect() 
{ 
String strExc="Success!";//strExc默认为Success,如果有例外抛出,即数据库连接不成功,则下面几个catch中被赋予其他抛出信息 
try 
{ 
Class.forName(DBDriver); 
conn=DriverManager.getConnection(DBLocation); 
} 
catch(ClassNotFoundException e) 
{ 
strExc="数据库驱动没有找到,错误提示:<br>" +e.toString(); 
} 
catch(SQLException e) 
{ 
strExc="sql语句错误,错误提示<br>" +e.toString(); 
} 
catch(Exception e) 
{ 
strExc="错误提示:<br>" +e.toString(); 
} 
return (strExc); 
}//then end of DBConnect 
//断开连接 
public String DBDisconnect() 
{ 
String strExc="Success!";//strExc默认为Success,如果有例外抛出,即数据库断开连接不成功,则下面几个catch中被赋予其他抛出信息 
try 
{ 
if(conn!=null)conn.close(); 
} 
catch(SQLException e) 
{ 
strExc=e.toString(); 
} 
return (strExc); 
} 
//通过传入sql语句来返回一个结果集 
public ResultSet query(String sql) throws SQLException,Exception 
{ 
ResultSet rs=null; 
if (conn==null) 
{ 
DBConnect(); 
} 
if (conn==null) 
{ 
rs=null; 
} 
else 
{ 
try 
{ 
Statement s=conn.createStatement(); 
rs=s.executeQuery(sql); 
} 
catch(SQLException e){throw new SQLException("Cound not execute query.");} 
catch(Exception e){throw new Exception("Cound not execute query.");} 
}//then end of if 
return(rs); 
}//then end of the function executeQuery 
//通过传入sql语句和pageSize(每页所显示的结果数目)计算并返回总共的页数 
public int getTotalPage(String sql,int pageSize) 
{ 
ResultSet rs=null; 
int totalRows=0; 
if (conn==null) 
{ 
DBConnect(); 
} 
if (conn==null) 
{ 
rs=null; 
} 
else 
try 
{ 
Statement s=conn.createStatement(); 
rs=s.executeQuery(sql);//通过传入的sql得到结果集 
while(rs.next()) 
totalRows++;//让rs一个个数,数完一遍,通过totalRows++也就计算出了返回结果集中总的条目数 
} 
catch(SQLException e){} 
rs=null; 
//由这个算法得出总页数(totalRows-1)/pageSize+1,并返回结果。totalRows是指返回结果集中的总的条目数,pageSize是指每页显示的条目数 
return((totalRows-1)/pageSize+1); 
} 
//通过传入sql语句,每页显示的条目数(pageSize)和页码,得到一个结果集 
public ResultSet getPagedRs(String sql,int pageSize,int pageNumber) 
{ 
ResultSet rs=null; 
int absoluteLocation; 
if (conn==null) 
{ 
DBConnect(); 
} 
if (conn==null) 
{ 
rs=null; 
} 
else 
try 
{ 
Statement s=conn.createStatement(); 
//pageSize*pageNumber每页显示的条目数乘以页码,计算出最后一行结果的编号,任何编号大于这个maxrows的结果都会被drop 
s.setMaxRows(pageSize*pageNumber); 
rs=s.executeQuery(sql); 
} 
catch(SQLException e){} 
//absoluteLocation=pageSize*(pageNumber-1)这个表达式计算出上一页最后一个结果的编号(如果有本页的话,上一页的显示的结果条目数肯定是pageSize) 
absoluteLocation=pageSize*(pageNumber-1); 
try 
{ 
//这个for循环的作用是让结果集rs定位到本页之前的最后一个结果处 
for(int i=0;i<absoluteLocation;i++) 
{ 
rs.next(); 
} 
} 
catch(SQLException e) { } 
//此时返回的结果集被两头一夹,就是该页(pageNumber)要显示的结果 
return(rs); 
} 
public String execute_sql(String sql){ 
String strExc; 
strExc="Success!"; 
if(conn!=null) 
{ 
try{ 
PreparedStatement update; 
update=conn.prepareStatement(sql); 
update.execute(); 
} 
catch(SQLException e) 
{ 
strExc=e.toString(); 
} 
catch(Exception e) 
{ 
strExc=e.toString(); 
} 
} 
else 
{ 
strExc="Connection Lost!"; 
} 
return(strExc); 
}//execute_sql
Copy after login

2. Analyze jsp page
Page name:
admin_show.jsp
Page code:

<%@ page errorPage="error.jsp"%> 
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.util.*"%> 
<%@ page import="java.sql.*"%> 
//导入database_basic包下面的databaseBean类,别名是basicDB 
<jsp:useBean id="basicDB" class="database_basic.databaseBean" scope="page"/> 
<% 
String sql; 
ResultSet rs; 
int id; 
String reply,Exc; 
Exc=basicDB.DBConnect();//建立连接,若成功,则返回Success!若失败,则返回相应出错信息 
if(!Exc.equals("Success!")) 
{ 
//basicDB.DBDisconnect(); 
throw new Exception(Exc); 
} 
int pageSize=10; //定义每页显示的数据条数 
int currentPage=1; //当前页(第一次显示的肯定是第一页啦!~),以后的“当前页”由下面出现的页面中的pages参数传入 
int allPage=-1; 
String pages=request.getParameter("pages");//取得页面中pages参数,此参数代表的页面的就是要显示的“当前页面” 
if(pages!=null) currentPage=Integer.valueOf(pages).intValue();//这是一个Integer型转int型的例子,第一次执行这个页面的时候,pages这个参数是null,currentPage=1;当再次执行这个页面的时候,参数pages将被赋值,currentPage=pages的int值 
sql="select * from gbook order by id desc";//这样返回的结果集会采用desc降序排列,好处是,显示在前面的是最新的信息 
allPage=basicDB.getTotalPage(sql,pageSize);//得到总页码数 
rs=basicDB.getPagedRs(sql,pageSize,currentPage);//得到当前页面要显示的结果集 
%> 
<table border="0" cellspacing="1" cellpadding="3" width="590" bgcolor="#ffffff"> 
<% 
while(rs.next()){ 
id=rs.getInt("id");//得到数据库(结果集)中id编号 
%> 
<tr bgcolor="#FF6600" style="color:white"> 
<td >Name:<%=rs.getString("leaver")%></td> 
<td >Time:<%=rs.getString("leave_date")%></td> 
<td >Email:<%=rs.getString("email")%></td> 
<td ><div style="width:150;overflow:hidden;">Home:<%=rs.getString("homepage")%></div></td> 
</tr> 
<tr bgcolor="#FFE3B9"> 
<td colspan=4><FONT COLOR="#FF6600">Content:</FONT><BR><BR><%=rs.getString("content")%> </td> 
</tr> 
<%}%> 
<tr><td height="1"></td></tr> 
<tr> 
<td colspan=4 align=right bgcolor="#FF6600" style="color:white;"> 
现在是第<%=currentPage%>页, 
<%if(currentPage>1){%> 
<!--如果不在第一页,则显示出“首页”链接--> 
<A HREF="admin_show.jsp?pages=<%=(currentPage-1)%>">首页</A> 
<%} 
for(int i=1;i<=allPage;i++) 
{ 
//显示出1、2、3、4……到最后一页的链接 
out.println("<a href=admin_show.jsp?pages="+i+">"+i+"</a>"); 
} 
%> 
<%if(currentPage<allPage){%> 
<!--如果不在最后一页,则显示出“末页”链接--> 
<A HREF="admin_show.jsp?pages=<%=(currentPage+1)%>">末页</A> 
<%}%></td> 
</tr> 
</table> 
}//then end of the class
Copy after login

3. Summary
In this program that implements paging display, there are several algorithms and implementation methods that are relatively fixed and classic. It should be inspiring to people who have never written a paging program.

For more related articles on jsp reading database to implement paging technology, please pay attention to the PHP Chinese website!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template