第一步:連接數據庫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>新聞熱點
疑難解答