java-新手随笔-关于Servlet+jdbc+jsp的分页样式

此前说明:数据库为MySQL 数据库为book 表为books

以下为实体类代码

package com.po;

public class Books {
private Integer id;
private String bookname;
private String bookauthor;
private int page;
private int size;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
private String bookprice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBookauthor() {
return bookauthor;
}
public void setBookauthor(String bookauthor) {
this.bookauthor = bookauthor;
}
public String getBookprice() {
return bookprice;
}
public void setBookprice(String bookprice) {
this.bookprice = bookprice;
};

}

注:可以放在com.po包下

接下来为jdbc封装方法

package com.util;

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;

import com.po.Books;


public class DBjdbc {
public Connection con = null;
public ResultSet res = null;
public Statement sta = null;
public PreparedStatement pst = null;

public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "root");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("连不上数据库");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}

// 查询数据库表中数据。解决:resulset结果集 关闭之后,数据不可使用
// 将结果集关闭之前,将数据导入到list集合里。
// 最后,使用者在调用这个方法时,得到一个有数据库数据的一个集合类list
public List<Books> select(String sql) {
res = this.getres(sql);
// -----------------------------@1.查询数据,返回一个结果集
List<Books> list = new ArrayList<Books>();// 上溯造型(向上转型)
try {
while (res.next()) {// 以下三行代码,完成的是,将一条数据存储成 一个对象
Books s = new Books();
s.setId(res.getInt("id"));
s.setBookname(res.getString("bookname"));
s.setBookauthor(res.getString("bookauthor"));
s.setBookprice(res.getString("bookprice"));

list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 以上循环代码完成的是:将resultset结果集里面的数据导入到集合类list中
this.close();
}
return list;
}

public int getCount(String sql) {
int i=0;
try{con=this.getConnection();

sta=con.createStatement();

ResultSet res=sta.executeQuery(sql);
res.next();
i=res.getInt(1);}catch (Exception e) {
e.printStackTrace();

// TODO: handle exception
}
return i;
}
// 查询 。返回结果集
public ResultSet getres(String sql) {
try {
con = this.getConnection();
sta = con.createStatement();
res = sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;

}

public void close() {
try {
if (res != null) {
res.close();
}
if (sta != null) {
sta.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

注:包为com.dao或者com.util

接下来为分页Servlet类

package servlet;

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

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

import com.po.Books;
import com.util.DBjdbc;

/**
* Servlet implementation class SelectServlet
*/
@WebServlet("/SelectServlet")
public class SelectServlet extends HttpServlet {


//查询出第一页的数据显示在页面上
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取当前页和每页显示的记录数
int page=Integer.parseInt(request.getParameter("page"));
int size=Integer.parseInt(request.getParameter("size"));
String sql="select * from books limit "+(page-1)*size+","+size;
System.out.println(sql);
DBjdbc db=new DBjdbc();
List<Books> list=db.select(sql);
String sql1="select count(*) from books";
int count=db.getCount(sql1);
int pages=count%size==0?count/size:count/size+1;
request.setAttribute("pages", pages);
request.setAttribute("list", list);
request.setAttribute("page", page);
request.setAttribute("size", size);
request.getRequestDispatcher("select.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

}

注:包为com.servlet

接下来是jsp页面

首页:可以设置为index.jsp 也可以自己命名然后自己在web.xml配置文件中配置

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="SelectServlet?page=1&&size=3">查询图书信息</a>

</body>
</html>

分页查询页面:select.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function test(){
var page=document.getElementById("page").value;
if(page>${pages}||page<=0){
alert("跳转不过去");

}else{
location.href="SelectServlet?page="+page+"&&size=${size}";

}
}

function shang(page){
/* alert(${pages});
*/ if(page==1){
alert("已经是第一页了");
}else{
location.href="SelectServlet?page=${page-1}&&size=${size}";
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
图书管理信息
<table>
<tr><th>书名</th><th>作者</th><th>价格</th></tr>
<c:forEach var="book" items="${list}">
<tr><td>${book.bookname}</td><td>${book.bookauthor}</td><td>${book.bookprice}</td></tr>
</c:forEach>
<tr><td>
<a href="#" onclick="shang(${page});">上一页</a>

</td>
<td>
<c:if test="${page<pages}">
<a href="SelectServlet?page=${page+1}&&size=${size}">下一页</a></c:if>
<c:if test="${page>=pages}">
<a href="" onclick="javaScript:alert('已经是最后一页了');return false;">下一页</a>
</c:if>
</td>
<td><input type="text" id="page"><a href="#" onClick="test()">跳转</a></td></tr>
</table>
</center>

</body>
</html>

注:这个分页查询页面的限制条件上一页用了js,下一页用了问号传值这两个刚接触java时会遇到的方法。

 此为简单页面

 如果有各种地方需要完善,恳请大佬指教 

猜你喜欢

转载自www.cnblogs.com/lyc6666/p/11503803.html
今日推荐