文件大小:
軟件介紹
//實(shí)體類
package entity;
public class note {
PRivate int id;
private String title;
private String author;
private String content;
public note(){}
public note(String title,String author,String content)
{
this.title=title;
this.author=author;
this.content=content;
}
public note(int id,String title,String author,String content)
{
this.id=id;
this.title=title;
this.author=author;
this.content=content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
//連接數(shù)據(jù)庫的基類
package dao;
import java.sql.*;
public abstract class BaseJdbcDao {
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=notetest";
private static final String DBUSER="sa";
private static final String DBPASS="sa";
protected Connection conn=null;
protected Statement stmt=null;
protected PreparedStatement pstmt=null;
protected ResultSet rst=null;
public Connection getConn()
{
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
// System.out.println("連接成功");
}catch(ClassNotFoundException e)
{
System.out.println("沒有找到驅(qū)動");
e.getMessage();
}catch(SQLException e)
{
System.out.println("數(shù)據(jù)庫聯(lián)接失敗");
e.getMessage();
}finally
{
return conn;
}
}
public void CloseAll()
{
if(rst!=null)
{
try{
rst.close();
}catch(SQLException e)
{
e.toString();
}
}
if(pstmt!=null)
{
try{
pstmt.close();
}catch(SQLException e)
{
e.toString();
}
}
if(stmt!=null)
{
try{
stmt.close();
}catch(SQLException e)
{
e.toString();
}
}
if(conn!=null)
{
try{
conn.close();
}catch(SQLException e)
{
e.toString();
}
}
}
}
//業(yè)務(wù)類
package dao;
import java.sql.*;
import java.util.*;
import entity.note;
public class noteDao extends BaseJdbcDao{
int count=0;
//得到所有記錄數(shù)
public int getNoteCount()
{
String sql1="select count(*) from note";
int pageCount=0;
conn=super.getConn();
try{
pstmt=conn.prepareStatement(sql1);
rst=pstmt.executeQuery();
rst.next();
count=rst.getInt(1);
}catch(SQLException e)
{
e.toString();
}finally
{
super.CloseAll();
}
return count;
}
//分頁顯示
public List ShowNotesByPage(int page,int pageSize)
{
List listnote=new ArrayList();
note nn=null;
int preCount = pageSize*(page-1);
int pageCount=0;
String sql="select top "+pageSize+" * from note where id not in (select top "+preCount+" id from note order by id desc) order by id desc";
conn=super.getConn();
try{
if(count%pageSize==0){
pageCount=count/pageSize;
}
else
{
pageCount=count/pageSize+1;
}
pstmt=conn.prepareStatement(sql);
rst=pstmt.executeQuery();
while(rst.next())
{
nn=new note();
nn.setId(rst.getInt("id"));
nn.setTitle(rst.getString("title"));
nn.setAuthor(rst.getString("author"));
nn.setContent(rst.getString("content"));
listnote.add(nn);
}
}catch(SQLException e)
{
e.toString();
}finally
{
super.CloseAll();
}
return listnote;
}
}
//頁面中的代碼
<%@ page language="java" import="java.util.*,entity.*,dao.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My jsp showListNotes.jsp starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keyWords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/CSS" href="styles.css">
-->
</head>
<body>
<h1>所有留言</h1>
<%
List list=new ArrayList();
note nn=null;
noteDao notedao=new noteDao();
int count=notedao.getNoteCount();
int pageSize =5;
int currentPage = 1;
int pagecount;
//得到總共的頁數(shù)。
if(count%pageSize==0)
{
pagecount=count/pageSize;
}
else{
pagecount=count/pageSize+1;
}
String pager = request.getParameter("page");
if(pager!=null)
{
currentPage = Integer.parseInt(pager);
}
//給上一頁(prepage),下一頁(nextpage)賦值。保障傳遞的page不是-1,-2,等等不符合條件的值。
int prepage=currentPage;
int nextpage=currentPage;
if(currentPage>1)
{
prepage=currentPage-1;
}
if(currentPage<pagecount)
{
nextpage=currentPage+1;
}
list=notedao.ShowNotesByPage(currentPage,pageSize);
%>
<table border="1">
<tr>
<td>標(biāo)題</td>
<td>作者</td>
<td>內(nèi)容</td>
</tr>
<%
for(int i=0;i<list.size();i++)
{
nn=(note)list.get(i);
%>
<tr>
<td><%=nn.getTitle() %></td>
<td><%=nn.getAuthor() %></td>
<td><%=nn.getContent() %></td>
</tr>
<%
}
%>
</table>
//傳遞page參數(shù)
<a href="showListNotes.jsp?page=<%=prepage %>">上一頁</a>
<a href="showListNotes.jsp?page=<%=nextpage %>">下一頁</a>
</body>
版權(quán)聲明:
1 本站所有資源(含游戲)均是軟件作者、開發(fā)商投稿,任何涉及商業(yè)盈利目的均不得使用,否則產(chǎn)生的一切后果將由您自己承擔(dān)!
2 本站將不對任何資源負(fù)法律責(zé)任,所有資源請?jiān)谙螺d后24小時(shí)內(nèi)刪除。
3 若有關(guān)在線投稿、無法下載等問題,請與本站客服人員聯(lián)系。
4 如侵犯了您的版權(quán)、商標(biāo)等,請立刻聯(lián)系我們并具體說明情況后,本站將盡快處理刪除,聯(lián)系QQ:2499894784
- 千億體育手機(jī)版本v2.0.1 安卓版
- tplink物聯(lián)電腦版(原tplink安防) v2.12.17.
- Sandboxie Plus v1.9.8 / v5.64.8 開源電腦
- 字魂100號方方先鋒體字體包免費(fèi)版
- 奧維互動地圖奧維地圖PC破解版VIP V9.0.6
- 蘭博對戰(zhàn)平臺 V1.38.6 官方最新版 / 蘭博玩
- reWASD(Xbox One手柄映射工具) V6.0.1.5190
- mtool修改器 V2023.11 官方最新版 / mtool
- 115轉(zhuǎn)存助手ui優(yōu)化版腳本 V3.9.1 綠色免費(fèi)
- iSecure Center電腦客戶端 V1.5.0 官方版
點(diǎn)擊加載更多評論>>