以下操作是关于myeclipse7.0+数据库oracl+tomcat6.0+struts2.0实现的jsp的分页操作:
数据库名:student
属性名:
stu_id interger,
stuname varchar(200),
address varchar(200),
stuphone varchar(200)
//以下是链接数据库的类;
myconnection.java
package com.yourcompany.struts.fenye;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class myconnection{
private Connection conn ;
private Statement pstmt ;
private ResultSet rs ;
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver" ;
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl" ;
private static final String USERNAME = "scarlet";
private static final String PASSWORD = "1234";
private int count=0;
private Student student ;
//数据库连接
public synchronized Connection getConnection () {
try {
Class.forName (DRIVER);
conn = DriverManager.getConnection (URL,USERNAME,PASSWORD) ;
} catch (ClassNotFoundException e) {
e.printStackTrace () ;
return null ;
} catch (SQLException e) {
e.printStackTrace () ;
return null ;
}
return conn ;
}
}
//该类用于存储数据的bean,使其成为对象传入视图层;
Student.java
package com.yourcompany.struts.fenye;
public class Student {
private int stu_id ;
private String stuName ;
private String address ;
private String stuPhone ;
public Student () {
}
public int getStu_id () {
return stu_id ;
}
public void setStu_id (int stu_id) {
this.stu_id = stu_id ;
}
public String getStuName () {
return stuName ;
}
public void setStuName (String stuName) {
this.stuName = stuName ;
}
public String getAddress () {
return address ;
}
public void setAddress (String address) {
this.address = address ;
}
public String getStuPhone () {
return stuPhone ;
}
public void setStuPhone (String stuPhone) {
this.stuPhone = stuPhone ;
}
}
action类:用于处理操作;
StudentAction.java
package com.yourcompany.struts.fenye;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
private List<Student> students ;
private int pageNow = 1 ; //初始化为1,默认从第一页开始显示
private int pageSize = 5 ; //每页显示5条记录
private int k;//储存最大页面数
private int i;//从第i条查询数据
private int intRowCount;//总行数
private int intPageCount;//总页数
// private int tiao;//跳转页面
private StudentDaoImpl pageDAO = new StudentDaoImpl () ;
//获得对象,将数据封装到bean中(student中)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getIntRowCount() {
return intRowCount;
}
public void setIntRowCount(int intRowCount) {
this.intRowCount = intRowCount;
}
public int getIntPageCount() {
return intPageCount;
}
public void setIntPageCount(int intPageCount) {
this.intPageCount = intPageCount;
}
public int getK() {
return k;
}
public void setK(int k) {
this.k = k;
}
public void setTiao(int tiao) {
this.pageNow=tiao;
}
public String execute() throws Exception {
intRowCount=pageDAO.count();
k = (intRowCount + pageSize - 1) / pageSize;//计算出总页数
i=pageNow*pageSize-pageSize;
students = pageDAO.queryByPage(i, pageSize) ;//调用查询方法
return SUCCESS ;
}
}
//代理层中的dao接口类:
StudentDao.java
package com.yourcompany.struts.fenye;
public interface StudentDao{
public int count();
}
//代理层的dao的具体实现类:
StudentDaoImpl.java
package com.yourcompany.struts.fenye;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImpl implements StudentDao{
private Statement pstmt ;
private ResultSet rs ;
private Student student ;
myconnection con=new myconnection();
//分页查询
public List<Student> queryByPage (int i, int pageSize) {
List<Student> list = new ArrayList<Student> () ;
try {
if(con.getConnection()!=null){
pstmt = con.getConnection().createStatement();
String sql="select * from student";
rs = pstmt.executeQuery(sql) ;
int a=0;
while(rs.next()) {
a++;
if((a>i)&&(a<=i+pageSize)){
/*该 条件语句是判断查询的数据是否是在第几行到第几行的数据;其中pageSize是变量传入的数值是5,表示分页列表中的显示数据的条数;默认是5条数据;*/
student = new Student () ;
student.setStu_id (rs.getInt(1)) ;
student.setStuName (rs.getString(2)) ;
student.setAddress (rs.getString(3)) ;
student.setStuPhone (rs.getString(4)) ;
list.add (student) ;
}
}
}
} catch(SQLException e) {
e.printStackTrace() ;
}
return list ;
}
//查询出数据库中的数据的行数;
public int count() {
int intRowCount = 0;//总行数
Statement pstmt= null ;
String sql= null ;
ResultSet rs = null ;
sql = "select * from student order by stu_id asc";
try
{
pstmt = con.getConnection().createStatement();
rs = pstmt.executeQuery(sql) ;
while( rs.next()){
//游标指向第一行
intRowCount+=1;//取得总行数
}
}
catch(Exception e)
{
System.out.println(e) ;
}
return intRowCount;
}
}
本文介绍使用MyEclipse 7.0结合Oracle数据库、Tomcat 6.0及Struts 2.0进行JSP分页操作的方法。具体包括数据库连接、数据Bean封装、Action处理、DAO接口实现等关键步骤。





