public class NewsDao extends NewsBase {, o! } s0 c! H0 Z
private int count =2;8 R- g! o9 F) {2 k/ L
6 V5 V9 ?) N3 Z6 y/ S* g0 Y# E( b& S
/** 7 m# d: h6 ~' i' I- J
* 查询总页数 - O# z; k( J' B2 E
* @return1 b7 U |" j9 q. i+ x3 S W# c
*/) a! W; e6 W8 a# q
public int countPage(){ . s0 d' j$ S4 e* G" s8 f. n9 ]0 `
int rows=0; S1 o2 N: T4 ]6 _- P' ~
Connection con=null;8 z( U, {" q. n6 N1 D: B
PreparedStatement past=null; 9 x3 t7 S4 v$ I+ N
ResultSet rs=null;4 j7 p+ m2 T0 E6 J7 {# `
con=getConnection(); , w! \8 ~, ~( X$ H
//查询所有的的页数 返回受影响的行数5 W) e1 t/ `; e% L! T
String sql="select count(*)as c from news";9 C) R6 E. j3 N
try {2 v }5 H' y7 N2 h" n& x% u
past=con.prepareStatement(sql);1 K9 ]8 ]& j; U
rs=past.executeQuery();" c. s3 l' |0 d7 E5 [8 @
if(rs.next()){ 7 B8 t! a: _+ h* d
//将所有的行数保存到rows 变量里 * u7 E8 p" t3 v' C" h2 U
rows=rs.getInt("c"); 3 o. L4 E, t* b. a4 q: m% ?8 M
//如果显示两页 count 代表显示的页数 ' D% d- k& a, ^1 b5 P/ a! ?" _+ T
if(rows%2==0){ H9 H4 W& b+ Y5 J2 b
rows=rows/count; : L/ X4 c! y6 A3 w4 J
) ?5 g6 g$ L. g
}else{ 8 p6 _' Q6 G. a5 f
rows=rows/count+1; $ u+ j9 k9 c6 o+ k0 v
}7 C: [ K" g5 L, z0 Z
} , w" ^7 m4 R7 J* G. P# j
} catch (SQLException e) { - E/ v& y) y8 q' F( e2 h: m; U
System.out.println("查询总页数方法异常"); $ L3 [9 g2 O3 N) n4 y
e.printStackTrace(); & s, J E8 Q0 a4 ^
}3 ], U# O1 n5 {1 g* \% U
return rows;! `6 h2 r( d* M5 I( g# W
}& S( x. K; |3 ]
/**, ]) W! s% E, t7 G! {
* 根据id删除信息 0 p: T3 K S, {) h2 \: }. e5 r: ?; }
* @param id $ q5 Z3 S3 ^* }1 i2 K) p! H
* @return# Y+ _; _4 z+ n
*/ $ n7 t3 J' o: p
public int deleteNewsId(int id){0 C! v% C6 q' T1 n7 p$ i) H3 z% X+ t
String sql="delete news where id="+id; ( K0 y; b! P) }/ q. l4 g
return update(sql,null);& }5 `: V: T- c" O c% G/ A
}& K1 o$ K5 n, q
/** ) [0 C0 C" A5 V; ]2 C" \" y+ W( _0 y
* 根据总页数分页 7 b) t! }: Y& \ m' {0 B2 ?) a
* @param page, u% [. W( o: | z
* @return" C/ J# L& I8 n' g' ?% D
*/* s( ~' c7 Z) E+ Z0 K, F* J5 F
public List getPageList(int page){ 7 E) B1 M# ?% n/ B2 j0 F; P
Connection con=null;. g5 P3 C, R2 l" P7 S" f5 U
PreparedStatement past=null; 2 K' U, E; Q( v
ResultSet rs=null; ( v, l* m+ F+ L' j2 V9 O& Y. g0 m: y
List list=new ArrayList(); ) F% d% L# n J. U _
con=getConnection();% Q$ @ e, u; V# H' o
//7 c6 l* d0 [) K* H. t" T5 V' [( P8 H
String sql="select top "+count+" * from news where id not in (select top "+(page-1)*count+" id from news order by pubtime desc )order by pubtime desc"; ( P& t8 Y% h& I! y
try { 4 G- l; H$ h/ ~! R4 R2 P5 V' o
past=con.prepareStatement(sql);) j" \' C4 X! J0 g8 q, x
rs=past.executeQuery();1 m4 a5 ]. o* \; ^6 Y& f/ W
while(rs.next()){ - I9 ?! E- f. I. x
' v( z* h- A3 e# T& \* n
// id, title, content, pubtime, author/ F: E2 j5 K% o U# H
int id=rs.getInt("id");. h7 [! ^# m# k
String title=rs.getString("title"); 8 x. ?7 R# V+ J9 o. _
String content=rs.getString("content"); 2 a' R9 u! v1 L' U- J$ Z& g
String pubtime=rs.getString("pubtime");3 S. ?# P1 s! @, h' `
String author=rs.getString("author"); + I: a: I e; {: Z/ ?