一:起始提交页面choiceDatabase.java
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="UTF-8"%>
<html>
<body bgcolor=pink>
<TABLE>
<FORM action="helpReadRecord" method="post" name="form">
oracle数据库jh,表名是student
<input type="hidden" value="jh" name="databaseName">
<Br>输入每页显示的记录数:
<input type="text" value="2" name="pageSize" size=6>
<input type="hidden" value="student" name="tableName">
<input type="submit" value="读取" name="submit">
</FORM>
</body>
</html>
二:显示页面
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@page import="flower.grass.ShowByPage"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.Vector"%>
<html>
<body bgcolor=yellow><FONT size=3>
<jsp:useBean id="database1" type="flower.grass.ShowByPage" scope="session"/>
您查询的数据库:<jsp:getProperty name="database1" property="databaseName"/>
<%String m=(String)session.getAttribute("databaseName");%><%=m %>
查询的表:<jsp:getProperty name="database1" property="tableName"/>
<%String n=(String)session.getAttribute("databaseName");%><%=n%>
<Br>记录分<jsp:getProperty name="database1" property="pageAllCount"/>页
每页最多显示<jsp:getProperty name="database1" property="pageSize"/>条记录
目前显示第<jsp:getProperty name="database1" property="showPage"/>页
<br><%Vector b=(Vector)session.getAttribute("list_room"); %><%=b%><%b.clear();%>
<TABLE>
<TR><TD>
<FORM action="helpReadRecord" method="post" name="form">
<input type="hidden" value="previousPage" name="whichPage">
<input type="submit" value="上一页" name="submit">
</FORM>
</TD>
<TD>
<FORM action="helpReadRecord" method="post" name="form">
<input type="hidden" value="nextPage" name="whichPage">
<input type="submit" value="下一页" name="submit">
</FORM>
</TD>
</TR>
</TABLE>
</FONT>
</body>
</html>
三:javaBean ShowByPage.java
package flower.grass;
import java.sql.*;
import java.util.Vector;
import com.sun.corba.se.pept.transport.Connection;
public class ShowByPage{
static int pageSize=2;//每页显示的记录数
int pageAllCount=0;//分页后的总页数
int rowCount=0;//总行数
int showPage=1;//当前显示页
String tableName="";
String databaseName="";
ResultSet rs;
Vector result;
Vector pageCld;
Vector v=new Vector();
public void setPageSize(int mm) {
// TODO Auto-generated method stub
pageSize=mm;
}
public int getPageSize() {
// TODO Auto-generated method stub
return pageSize;
}
public int getPageAllCount() {
// TODO Auto-generated method stub
return pageAllCount;
}
public void setPageAllCount(int n) {
// TODO Auto-generated method stub
pageAllCount=n;
}
public int getShowPage() {
// TODO Auto-generated method stub
return showPage;
}
public void setShowPage(int n) {
// TODO Auto-generated method stub
showPage=n;
}
public void setTableName(String s) {
// TODO Auto-generated method stub
tableName=s.trim();
}
public String getTableName(){
return tableName;
}
public void setDatabaseName(String s) {
// TODO Auto-generated method stub
databaseName=s.trim();
}
public String getDatabaseName(){
return databaseName;
}
public int getRowCount() {
// TODO Auto-generated method stub
return rowCount;
}
public void setRowCount(int s) {
// TODO Auto-generated method stub
rowCount=s;
}
public void setResult(Vector v) {
result = v;
}
public Vector getResult(){
return result;
}
public void setPageCld(Vector v) {
pageCld = v;
}
public Vector getPageCld(){
return pageCld;
}
}
四servlet HandleDatabase.java
package rain.snow;
import flower.grass.ShowByPage;
import java.io.IOException;
import java.sql.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleDatabase extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
int RowCount=0;
ResultSet rs=null;
Connection con;
ShowByPage databaseBean=null;
String databaseName;
String tableName;
Vector v=new Vector();
public void init(ServletConfig config)throws ServletException{
super.init(config);
try{Class.forName("oracle.jdbc.driver.OracleDriver");}
catch(Exception e){}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
ShowByPage databaseBean=null;
HttpSession session=request.getSession(true);
try{ databaseBean=(ShowByPage)session.getAttribute("database1");
if(databaseBean==null){
databaseBean=new ShowByPage();
session.setAttribute("database1", databaseBean);
}
}
catch(Exception exp){
databaseBean=new ShowByPage();
session.setAttribute("database1", databaseBean);
}
//String databaseName=request.getParameter("databaseName");
//String tableName=request.getParameter("tableName");
//String ps=request.getParameter("pageSize");
String databaseName=(String)session.getAttribute("databaseName");
if(databaseName==null){
session.setAttribute("databaseName", "jh");
}
String tableName=(String)session.getAttribute("tableName");
if(tableName==null){
session.setAttribute("tableName", "student");
}
String ps=(String)session.getAttribute("pageSize");
if(ps==null){
session.setAttribute("ps", "2");
}
if(ps!=null){
try{
int mm=Integer.parseInt(ps);
databaseBean.setPageSize(mm);
}
catch(NumberFormatException exp){databaseBean.setPageSize(1);}
}
boolean boo=databaseName!=null&&tableName!=null&&databaseName.length()>0&&tableName.length()>0;
//System.out.println("databaseName="+databaseName+"tableName="+tableName+"databaseName.length()="+databaseName.length()+"tableName.length()="+tableName.length());
System.out.println("boo="+boo);
if(boo){
String uri="jdbc:oracle:thin:@127.0.0.1:1521:"+databaseName;
try{//获取总行数,总页数
int showPage=databaseBean.getShowPage();
int pageSize=databaseBean.getPageSize();
con=DriverManager.getConnection(uri,"jh","jh");
Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rst=sql.executeQuery("SELECT * FROM "+tableName);
if(rst.next()){
rst.last();
RowCount=rst.getRow();
}
int n=pageSize;
int pageAllCount=((RowCount%n)==0)?(RowCount/n):(RowCount/n+1);
databaseBean.setPageAllCount(pageAllCount);
//System.out.print(pageAllCount);
//判断为哪一页
String whichPage=request.getParameter("whichPage");
if(whichPage==null||whichPage.length()==0){
showPage=1;
System.out.println("showPage="+showPage);
databaseBean.setShowPage(showPage);
}
else if(whichPage.equals("nextPage")){
showPage++;
if(showPage>databaseBean.getPageAllCount())
showPage=1;
System.out.println("showPage="+showPage);
databaseBean.setShowPage(showPage);
}
else if(whichPage.equals("previousPage")){
showPage--;
if(showPage<=0)
showPage=databaseBean.getPageAllCount();
System.out.println("showPage="+showPage);
databaseBean.setShowPage(showPage);
}
//取得要显示的结果集,并存放在vector中
ResultSet rs=sql.executeQuery("select * from (select rownum r,no ,name,math,chinese,english from " +
"(select no,name,math,chinese,english from student))" +
" where r between " +((showPage-1) * pageSize + 1)+ " and " +(showPage* pageSize));
System.out.println("showPage="+showPage);
int i=0;
while(rs.next()){
if(i<pageSize){
Object[] obj=new Object[5];
obj[0]=rs.getString("no");
obj[1]=rs.getString("name");
obj[2]=rs.getFloat("math");
obj[3]=rs.getFloat("chinese");
obj[4]=rs.getFloat("english");
for(int k=0;k<5;k++){
v.add(obj[k]);
}
}
i++;
}
session.setAttribute("result", v);
rs.close();
sql.close();
con.close();
}
catch(SQLException exp){}
}
RequestDispatcher dispatcher=request.getRequestDispatcher("/showRecord.jsp");
dispatcher.forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
}