前端使用分页组件mricode+artTemplate
window.onload = function () {
$("#page").pagination({
pageIndex: 0,
pageSize: 5,
showInfo: true,
showJump: true,
showPageSizes: true,
remote: {
url: '/page.action',
success: function (data) {
var html = template('test', data);
document.getElementById('content').innerHTML = html;
}
}
});
}
ID姓名密码
{{each list as value i}}
{{value.id}}{{value.name}}{{value.pwd}}{{/each}}
数据库
drop database if exists jdbc;
CREATE DATABASE IF NOT EXISTS jdbc;
USE jdbc;
drop table if exists user ;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL ,
`pwd` varchar(50) DEFAULT NULL ,
PRIMARY KEY (`id`)
) ;
#数据初始化
insert into user(name,pwd) values ('a1' ,'123456' );
insert into user(name,pwd) values ('a2' ,'123456' );
insert into user(name,pwd) values ('a3' ,'123456' );
insert into user(name,pwd) values ('a21' ,'123456' );
insert into user(name,pwd) values ('a22' ,'123456' );
insert into user(name,pwd) values ('a23' ,'123456' );
insert into user(name,pwd) values ('a31' ,'123456' );
insert into user(name,pwd) values ('a32' ,'123456' );
insert into user(name,pwd) values ('a33' ,'123456' );
后端代码
实体类
User.java
public class User {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
PageDataModel.java
public class PageDataModel {
private int total;
private List list;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
DBUtils.java
package util;
import java.sql.*;
public class DBUtils {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
String url = "jdbc:mysql://localhost/jdbc?useUnicode=true&characterEncoding=UTF-8";
Connection conn = DriverManager.getConnection(url,"root","root");
return conn;
}
public static void close(Statement st,Connection conn){
try{
if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void close(ResultSet rs,Statement st,Connection conn){
try{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}finally{
try{
if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
pageServlet2.java
@WebServlet(name = "pageServlet2",urlPatterns = "/page.action")
public class pageServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strPageIndex = request.getParameter("pageIndex");
System.out.println(strPageIndex);
int nPageIndex = Integer.parseInt(strPageIndex);
String strPageSize = request.getParameter("pageSize");
System.out.println(strPageSize);
int nPageSize = Integer.parseInt(strPageSize);
IUserInfoService iuis = UserInfoServiceImpl.getInstance();
List lstUser = iuis.GetAllUsersPaged(nPageIndex,nPageSize);
int nCount = iuis.GetPagedDataCount();
PageDataModel p = new PageDataModel();
p.setTotal(nCount);
p.setList(lstUser);
String strJson = JSON.toJSONString(p);
response.getWriter().println(strJson);
}
}
IUserInfoService.java
public interface IUserInfoService {
List GetAllUsersPaged(int nPageIndex, int nPageSize);
int GetPagedDataCount();
}
UserInfoServiceImpl.java
package Service.ServiceImpl;
import Dao.DaoImpl.UserInfoDaoImpl;
import Dao.IUserInfoDao;
import Service.IUserInfoService;
import util.User;
import java.util.List;
public class UserInfoServiceImpl implements IUserInfoService {
private static UserInfoServiceImpl userInfoServiceImpl = new UserInfoServiceImpl();
private UserInfoServiceImpl()
{
;
}
public static UserInfoServiceImpl getInstance()
{
return userInfoServiceImpl;
}
@Override
public List GetAllUsersPaged(int nPageIndex, int nPageSize) {
IUserInfoDao iuid = new UserInfoDaoImpl();
List lst = iuid.GetAllUsersPaged(nPageIndex,nPageSize);
return lst;
}
@Override
public int GetPagedDataCount() {
IUserInfoDao iuid = new UserInfoDaoImpl();
int nCount = iuid.GetPagedDataCount();
return nCount;
}
}
IUserInfoDao.java
public interface IUserInfoDao {
List GetAllUsersPaged(int nPageIndex, int nPageSize);
int GetPagedDataCount();
}
UserInfoDaoImpl.java
package Dao.DaoImpl;
import Dao.IUserInfoDao;
import util.DBUtils;
import util.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserInfoDaoImpl implements IUserInfoDao {
@Override
public List GetAllUsersPaged(int nPageIndex, int nPageSize) {
Connection con = null;
PreparedStatement ps = null;
List lst = new ArrayList();
StringBuilder sb = new StringBuilder();
try {
con = DBUtils.getConnection();
sb.append("select * FROM USER where 1 = 1 ");
sb.append(" limit ?, ?");
ps = con.prepareStatement(sb.toString());
ps.setInt(1,nPageIndex*nPageSize);
ps.setInt(2,nPageSize);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
User ui = new User();
ui.setId(rs.getInt(1));
ui.setName(rs.getString(2));
ui.setPwd(rs.getString(3));
lst.add(ui);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.close(ps,con);
}
return lst;
}
@Override
public int GetPagedDataCount() {
Connection con = null;
PreparedStatement ps = null;
int nCount = 0;
StringBuilder sb = new StringBuilder();
try {
con = DBUtils.getConnection();
sb.append("select count(*) FROM user where 1 = 1 ");
ps = con.prepareStatement(sb.toString());
ResultSet rs = ps.executeQuery();
if(rs.next())
nCount = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.close(ps,con);
}
return nCount;
}
}