`
l_rui_ci
  • 浏览: 40194 次
  • 性别: Icon_minigender_1
  • 来自: 桂林
社区版块
存档分类
最新评论

简单分页(只有第一页、上一页、下一页、末一页)

    博客分类:
  • JSP
阅读更多

DB.java代码:

 

package com.bjsxt.bbs;

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

public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root" , "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}

public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

public static int executeUpdate(Connection conn, String sql){
int ret = 0;
Statement stmt = null;
try {
stmt = conn.createStatement();
ret = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(stmt);
}
return ret ;
}

public static PreparedStatement prepareStmt(Connection conn, String sql){
PreparedStatement pStmt = null;
try {
pStmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pStmt;
}

public static PreparedStatement prepareStmt(Connection conn, String sql, int autoGeneratedKeys){
PreparedStatement pStmt = null;
try{
pStmt = conn.prepareStatement(sql, autoGeneratedKeys);
} catch(SQLException e) {
e.printStackTrace();
}
return pStmt;
}

public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}

public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}

public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}

 

Article.java代码:

package com.bjsxt.bbs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class Article {
private int id;
private int pid; //private Article parent
private int rootId;
private String title;
private String cont;
private Date pdate;
private boolean isLeaf;
private int grade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getRootId() {
return rootId;
}
public void setRootId(int rootId) {
this.rootId = rootId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public boolean isLeaf() {
return isLeaf;
}
public void setLeaf(boolean isLeaf) {
this.isLeaf = isLeaf;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}

public void initFromRs(ResultSet rs){
try {
setId(rs.getInt("id"));
setPid(rs.getInt("pid"));
setRootId(rs.getInt("rootid"));
setTitle(rs.getString("title"));
setLeaf(rs.getInt("isleaf") == 0 ? true : false);
setPdate(rs.getTimestamp("pdate"));
setCont(rs.getString("cont"));
setGrade(0);
} catch (SQLException e) {
e.printStackTrace();
}

}
}

分页article.jsp:

<%@ page pageEncoding ="GB18030"%>
<%@ page import="java.sql.*,com.bjsxt.bbs.*,java.util.*,java.io.*"%>

 

<%
//把每页的条目设置为4
final int PAGE_SIZE = 4 ;
int totalPages = 0;
String strPageNo = request.getParameter("pageNo");
int pageNo = 1;
if(strPageNo != null && !strPageNo.trim().equals("")){
try{
pageNo = Integer.parseInt(strPageNo);
} catch(NumberFormatException e) {
pageNo = 1;
}
}
if(pageNo < 1 ){
pageNo = 1;
}

List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();

Statement totalStmt = DB.createStmt(conn);
ResultSet rsCount = DB.executeQuery(totalStmt,"select count(*) from article where pid = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);

totalPages = (totalRecords + PAGE_SIZE -1)/PAGE_SIZE;
if(pageNo > totalPages) {
pageNo = totalPages;
}

Statement stmt = DB.createStmt(conn);
int startPages =(pageNo -1 ) * PAGE_SIZE;
String sql = "select * from article where pid = 0 order by pdate desc limit " + startPages + "," + PAGE_SIZE ;
ResultSet rs = DB.executeQuery(stmt,sql);
while(rs.next()){
Article a = new Article();
a.initFromRs(rs);
articles.add(a);
}

DB.close(rsCount);
DB.close(totalStmt);
DB.close(rs);
DB.close(stmt);
DB.close(conn);
%>

<span class="nobreak"> 页:
第<%=pageNo %>页&nbsp;共<%=totalPages %>页 [

<a href="articleFlat.jsp?pageNo=<%=1%>">第一页</a>|
<a href="articleFlat.jsp?pageNo=<%=pageNo - 1 %>">上一页</a>
<a>|</a>
<a href="articleFlat.jsp?pageNo=<%=pageNo +1 %>">下一页</a>|&nbsp;
<a href="articleFlat.jsp?pageNo=<%=totalPages %>">最末页</a> ]

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics