国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

輸入框實現新聞列表分頁顯示(一)

2019-11-15 00:12:09
字體:
來源:轉載
供稿:網友
輸入框實現新聞列表分頁顯示(一)今天學了新聞發布系統中的使用分頁技術實現頁面的分頁顯示,感覺開始很難做,漸漸的需要動手,慢慢透析。分頁顯示的步驟1、確定每頁顯示的數據數量2、計算顯示的頁數3、編寫SQL語句第一步:連接數據庫
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;/** * 數據庫連接幫助類 * @author zql_pc * 單例設計模式 */public class DbHelp {        //數據庫連接對象    private Connection conn = null;    //數據庫連接幫助    public Connection lianjie() {                if(conn==null){                    //數據庫配置            String driver = "Oracle.jdbc.driver.OracleDriver";//驅動            String url = "jdbc:oracle:thin:@localhost:1521:NEWS";//url            String username = "epet";//用戶名            String passWord = "123456";//密碼                        //1.加載驅動類            try {                Class.forName(driver);                //2.建立連接                conn = DriverManager.getConnection(url,username,password);            } catch (ClassNotFoundException e) {                e.printStackTrace();            } catch (SQLException e) {                e.printStackTrace();            }                    }                return conn;            }        }

第二步:編寫新聞實體類,進行封裝

/** * 新聞實體類 * @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;    }}

第三步:查詢新聞

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;/** * 查詢新聞 * @author zql_pc * */public class SelectNews {        DbHelp help = new DbHelp();        //查詢新聞數目    public int newsCount() throws SQLException{        //查詢新聞條目        Connection con = help.lianjie();                //創建數據庫操作對象        Statement st = con.createStatement();                String sql = "select count(*) from news";                //獲得結果集        ResultSet rs = st.executeQuery(sql);                //新聞數目        int newsCount = 0;                while(rs.next()){                        newsCount = rs.getInt(1);                    }                return newsCount;    }        //計算分頁    public int getPages(int newsSize) throws SQLException{                        int newsCount = newsCount();                int num = (newsCount%newsSize==0)?newsCount/newsSize:newsCount/newsSize+1;                return num;    }        //根據要求查詢新聞    public List<news> getList(int pageIndex,int newsSize) throws SQLException{                //計算上限和下限         //第1頁  1 - 5        //  2   6 - 10        int up = newsSize * pageIndex;                int down = newsSize * (pageIndex-1)+1;                //獲取數據庫連接對象        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);                //獲取結果集        ResultSet rs =  pst.executeQuery();                //創建集合裝新聞對象        List<news> list = new ArrayList<news>();                 while(rs.next()){                        //創建新聞對象            news news = new news();                        news.setTitle(rs.getString("ntitle"));            news.setCreatedate(rs.getString("ncreatedate"));                        list.add(news);        }                return list;    }}

第四步:在頁面分頁顯示

<%@ 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"><html>  <head>  </head>  <body>    <table align="center" border="0">                                <%            //展示新聞條數            int newsSize = 5;                    //實例化新聞幫助對象            SelectNews sn = new SelectNews();                        //獲取頁面數量            int pages = sn.getPages(newsSize);                        //當前頁碼            int pageIndex = 1;                        //獲取提交頁碼            String Pagetemp = request.getParameter("pageIndex");                        //判斷頁面是否是當前頁提交            if(Pagetemp!=null){                                pageIndex = Integer.parseInt(Pagetemp);                            }                        //=============查詢新聞            //獲取裝有新聞的集合            List<news> NewsList = sn.getList(pageIndex,newsSize);                        //遍歷打印            for(news temp : NewsList){                                                    %>            <tr>                <td><%=temp.getTitle() %></td>                <td><%=temp.getCreatedate() %></td>            </tr>                <%    } %>                <tr>            <td colspan="2">                <%if(!(pageIndex<=1)){ %>                    <a href="index.jsp?pageIndex=<%=1 %>">首頁</a>                    <a href="index.jsp?pageIndex=<%=pageIndex-1 %>">上一頁</a>                <%}                if(!(pageIndex>=pages)){                %>                    <a href="index.jsp?pageIndex=<%=pageIndex+1 %>">下一頁</a>                    <a href="index.jsp?pageIndex=<%=pages %>">尾頁</a>                <%} %>                                <form action="index.jsp" method="post">                    <input type="text" name="pageIndex" value="<%=pageIndex %>"/>                    <input type="submit" />                </form>                             </td>        </tr>    </table>              </body></html>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扶沟县| 拉萨市| 榆林市| 资阳市| 治县。| 博乐市| 阜新| 秦安县| 行唐县| 武强县| 博客| 南皮县| 叶城县| 常德市| 郸城县| 沙雅县| 红安县| 凉城县| 云阳县| 历史| 嘉善县| 介休市| 手游| 论坛| 三江| 正镶白旗| 灌云县| 武汉市| 武宣县| 富民县| 西乡县| 东安县| 马山县| 政和县| 富锦市| 襄樊市| 新余市| 仙桃市| 鸡泽县| 卢湾区| 北海市|