The input box displays the news list by PAGE (1) and the News list by page.
Today, I learned how to use the paging technology in the news and publishing system to display pages by page. I feel that it is very difficult to do it at first, and I need to do it gradually and slowly. Step 1: determine the number of data displayed on each page. 2. Calculate the number of pages displayed. 3. Write an SQL statement. Step 1: connect to the database.
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. arrayList; import java. util. list;/*** database Connection help class * @ author zql_pc * Singleton design mode */public class DbHelp {// database Connection object private Connection conn = null; // database Connection help public Connection lianjie () {if (conn = null) {// Database Configuration String driver = "oracle. jdbc. driver. oracleDriver "; // driver String url =" jdbc: oracle: thin: @ localhost: 1521: NEWS "; // url String username =" epet "; // username String password = "123456"; // password // 1. load the driver Class try {Class. forName (driver); // 2. establish connection conn = DriverManager. getConnection (url, username, password);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace () ;}} return conn ;}}
Step 2: write news entity classes for encapsulation
/*** News entity class ** @ author zql_pc **/public class news {private int id; private int t_id; private String title; private String author; private String createdate; private String picpath; private String content; private String modifydate; private String summay; public int getId () {return id;} public void setId (int id) {this. id = id;} public int getT_id () {return t_id;} public void setT_id (int tId) {t_id = tId;} public String getTitle () {return title ;} public void setTitle (String title) {this. title = title;} public String getAuthor () {return author;} public void setAuthor (String author) {this. author = author;} public String getCreatedate () {return createdate;} public void setCreatedate (String createdate) {this. createdate = createdate;} public String getPicpath () {return picpath;} public void setPicpath (String picpath) {this. picpath = picpath;} public String getContent () {return content;} public void setContent (String content) {this. content = content;} public String getModifydate () {return modifydate;} public void setModifydate (String modifydate) {this. modifydate = modifydate;} public String getSummay () {return summay;} public void setSummay (String summay) {this. summay = summay ;}}
Step 3: Query news
Import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. arrayList; import java. util. list;/*** query news * @ author zql_pc **/public class SelectNews {DbHelp help = new DbHelp (); // query news count public int newsCount () throws SQLException {// query the news entry Connection con = help. lianjie (); // create a database operation object Statement st = co N. createStatement (); String SQL = "select count (*) from news"; // obtain the result set ResultSet rs = st.exe cuteQuery (SQL); // The number of news int newsCount = 0; while (rs. next () {newsCount = rs. getInt (1) ;}return newsCount;} // calculate the public int getPages (int newsSize) throws SQLException {int newsCount = newsCount (); int num = (newsCount % newsSize = 0 )? NewsCount/newsSize: newsCount/newsSize + 1; return num;} // query news public List <news> getList (int pageIndex, int newsSize) as required) throws SQLException {// calculate the upper and lower limits // page 1st 1-5 // 2 6-10 int up = newsSize * pageIndex; int down = newsSize * (pageIndex-1) + 1; // obtain the Connection con = help. lianjie (); String SQL = "select * from" + "(select n. *, rownum r from "+" (select * from news order by ncreatedate desc) N "+" where rownum <= ?) Where r> =? "; PreparedStatement pst = con. prepareStatement (SQL); pst. setInt (1, up); pst. setInt (2, down); // obtain the result set ResultSet rs = pst.exe cuteQuery (); // create a List of news objects containing the set <news> list = new ArrayList <news> (); while (rs. next () {// create a news object news = new news (); news. setTitle (rs. getString ("ntitle"); news. setCreatedate (rs. getString ("ncreatedate"); list. add (news) ;}return list ;}}
Step 4: display on page
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" tools. selectNews "%> <% @ page import =" tools. news "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">