JSP+Servlet分页

package com.svse.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//连接数据库
public class DBhelp {

private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
public Connection getConn()
{
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String passwor="tiger";
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, passwor);
if(conn!=null)
{
System.err.println("连接数据库成功");
}
else
{
System.err.println("连接数据库失败");
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭连接,释放资源
public void closeConn(Connection conn,PreparedStatement pstm,ResultSet rs) throws Exception
{
if(rs!=null)
{
rs.close();
}
if(pstm!=null)
{
pstm.close();
}
if(conn!=null)
{
conn.close();

}
}
public static void main(String[] args) {

DBhelp db=new DBhelp();
System.err.println(db.getConn());
}
}
---------------------------------------------------
package com.svse.entity;

import java.util.Date;

public class Student {

private int stuid;
private String stuname;
private String stusex;
private String stubirth;
private String stuaddr;

public Student() {
// TODO Auto-generated constructor stub
}

public Student(String stuaddr, String stubirth, int stuid, String stuname,
String stusex) {
super();
this.stuaddr = stuaddr;
this.stubirth = stubirth;
this.stuid = stuid;
this.stuname = stuname;
this.stusex = stusex;
}

public int getStuid() {
return stuid;
}

public void setStuid(int stuid) {
this.stuid = stuid;
}

public String getStuname() {
return stuname;
}

public void setStuname(String stuname) {
this.stuname = stuname;
}

public String getStusex() {
return stusex;
}

public void setStusex(String stusex) {
this.stusex = stusex;
}

public String getStubirth() {
return stubirth;
}

public void setStubirth(String stubirth) {
this.stubirth = stubirth;
}

public String getStuaddr() {
return stuaddr;
}

public void setStuaddr(String stuaddr) {
this.stuaddr = stuaddr;
}




}
---------------------------------------------------------
package com.svse.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.svse.entity.Student;
import com.svse.util.DBhelp;

public class StudentDao {


private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
DBhelp db=new DBhelp();
//添加
public boolean add(Student stu)
{
boolean flag=false;;
String sql="insert into Student values(?,?,?,?,?)";
conn=db.getConn();
if(conn!=null)
{
try {
pstm=conn.prepareStatement(sql);
pstm.setInt(1, stu.getStuid());
pstm.setString(1, stu.getStuname());
pstm.setString(3, stu.getStusex());
pstm.setString(4,stu.getStubirth());
pstm.setString(5, stu.getStuaddr());
int i=pstm.executeUpdate();
if(i>0)
{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}

//获得总行数(方法一)
public int getTotalCount()
{
String sql="select count(*) from Student";
conn=db.getConn();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return -1;

}
/*//查询总行数(方法二)
public int getTotalCount()
{
int i=0;
String sql="select * from Student";
conn=db.getConn();
List list=new ArrayList();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();

while(rs.next())
{
Student stu=new Student();
stu.setStuid(rs.getInt(1));
stu.setStuname(rs.getString(2));
stu.setStusex(rs.getString(3));
stu.setStubirth(rs.getString(4));
stu.setStuaddr(rs.getString(5));
list.add(stu);
}
i=list.size();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}*/

//分页显示
public List findByPage(int pageindex)
{
System.err.println("111111111111"+pageindex);
String sql="select * from Student where rownum<=3 and stuid not in (select stuid from Student where rownum<="+3*(pageindex-1)+")";

conn=db.getConn();
List list=new ArrayList();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();

while(rs.next())
{
Student stu=new Student();
stu.setStuid(rs.getInt(1));
stu.setStuname(rs.getString(2));
stu.setStusex(rs.getString(3));
stu.setStubirth(rs.getString(4));
stu.setStuaddr(rs.getString(5));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;

}
}
------------------------------------------------
package com.svse.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.svse.dao.StudentDao;

public class StudentServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public StudentServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}

StudentDao studao=new StudentDao();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String method = request.getParameter("met");
if (method.equals("studentshow")) {
//显示所有并分页
this.findAll(request, response);
}
}
//添加
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");

}
//查询所有
public void findAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");

//当前页码
int pageIndex = 1;
// 最大页码
int lastPage = 0;
//获得最大行数
int i=studao.getTotalCount();
System.err.println("总行数"+i);
//在dao方法中设定每页显示5条数据
if(i%3==0)
{
lastPage=i/3;
}
else
{
lastPage=i/3+1;
}

//获得传如的pageindex
if(request.getParameter("pindex") == null)
{
//第一次进入pageIndex设为1
pageIndex = 1;
}
else
{
//以后进入,pageIndex从页面上接收
pageIndex = Integer.parseInt(request.getParameter("pindex"));
}
System.err.println("当前页"+pageIndex);
System.err.println("最大页数"+lastPage);
List list=studao.findByPage(pageIndex);
request.setAttribute("lists", list);
request.setAttribute("pageindex", pageIndex + "");
request.setAttribute("lastpage", lastPage + "");

System.err.println("-----------"+list.size());
request.getRequestDispatcher("/showslist.jsp").forward(request,
response);
}


public void init() throws ServletException {
// Put your code here
}

}
----------------------------------------------------------------
<table align="center" >
<tr>
<td>

<table border="0" align="center">
<tr>
<td>学生编号</td>
<td>姓名</td>
<td>性别</td>
<td>生日</td>
<td>地址</td>
</tr>

<c:forEach items="${lists}" var="student">
<tr>
<td align="center">${student.stuid}</td>
<td align="center">${student.stuname}</td>
<td align="center">${student.stusex}</td>
<td align="center">${student.stubirth}</td>
<td align="center">${student.stuaddr}</td>
</tr>
</c:forEach>
</table>

<p align="center">
<a href=studentServlet?met=studentshow&pindex=1>第一页</a>

<%
//获得当前页和最大页数
int pageIndex=Integer.parseInt((request.getAttribute("pageindex")).toString());
int lastPage=Integer.parseInt((request.getAttribute("lastpage")).toString());
//判断当前页是否大于1,如果大于1,说名不是第一页,可以点击上一页
if(pageIndex>1)
{
%>
<a href=studentServlet?met=studentshow&pindex=<%=pageIndex-1 %>>上一页</a>  
<%
}
else
{
%>
上一页  
<%
}
%>
<%
if(pageIndex<lastPage)
{
//判断当页是否小于最大页,如果当前页小于最大页数,说名不是最后一页,可以点击下一页
%>
<a href=studentServlet?met=studentshow&pindex=<%=pageIndex+1 %>>下一页</a>  
<%
}
else
{
%>
下一页  
<%
}
%>

<a href="studentServlet?met=studentshow&pindex=<%=lastPage%>">末 页</a>

</td>
</tr>
</table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值