JSP 分页查询关键代码
1. 连接数据库的基类: 连接数据库的基类:
package com.bookmons; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class GetConnection { protected Connection conn = null;//连接字符串 protected PreparedStatement ps = null;//预编译并存储 SQL 指令 protected ResultSet rs = null;//查询结果集 private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//加载数据库驱动的字符串 private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=BOOKDB";//连接数据库的字 符串 private static final String USERNAME = "sa";//数据库用户名 private static final String PASSWORD = "accp";//数据库用户密码 /** * 获得数据库连接 * @return */ public Connection getConn() { Connection conn = null; try { Class.forName(DRIVER);//加载数据库驱动 conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);//连接数据库 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; }
/* * 释放资源 */ public void closeAll(){ try { if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ conn = null; ps = null; rs = null; } } }
2. 实体类代码: 实体类代码: 代码
package com.book.entity; public class Book { private int book_id; private String book_name; private String book_num; private String book_author; private double book_price; private String book_synopsis; private String book_publishTime;
public String getBook_author() { return book_author; } public void setBook_author(String book_author) { this.book_author = book_author; } public int getBook_id() { return book_id; } public void setBook_id(int book_id) { this.book_id = book_id; } public String getBook_name() { return book_name; } public void setBook_name(String book_name) { this.book_name = book_name; } public String getBook_num() { return book_num; } public void setBook_num(String book_num) { this.book_num = book_num; } public double getBook_price() { return book_price; } public void setBook_price(double book_price) { this.book_price = book_price; } public String getBook_publishTime() { return book_publishTime; }
public void setBook_publishTime(String book_publishTime) { this.book_publishTime = book_publishTime; } public String getBook_synopsis() { return book_synopsis; } public void setBook_synopsis(String book_synopsis) { this.book_synopsis = book_synopsis; } }
3. 分页查询代码: 分页查询代码:
package com.book.pagin; import java.
sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.bookmons.GetConnection; import com.book
.entity.Book; public class BookPaginQuery extends GetConnection { private final int PAGEROW = 8;//每页显示的行数 private int countRow;//总行数 private int countPage;//总页数 private int currentlyPage;//当前第几页 /** * 得到总页数 * @return */ public int getCountPage() { return countPage; }
/** * 设置总页数 * @param countPage */ public void setCountPage() { //通过总行数设置总页数 if (this.cou