public void colseAll(Connection con,PreparedStatement past,ResultSet rs){ 5 X/ E& A+ _+ f$ q1 q! a4 A
try { * l. q3 E( }: S ~$ Y
rs.close(); 4 w& M/ n; }* S N2 W
} catch (SQLException e) { + @3 W* W3 D. b2 u. k$ W
System.out.println(""); 3 i4 v, U4 M) |+ ]+ _3 d3 V$ K! b
e.printStackTrace();! x! @( V: g, F
} 7 R2 O- i0 ?) }( Z) m! F
try {- L1 [& {* C# e( x
past.close(); ! Q% e; V; [' p& ?' J3 d
} catch (SQLException e) {& p5 d! v" T+ s8 b
System.out.println("");9 c8 \* Q6 _8 {
e.printStackTrace();2 _3 f$ f6 \+ U: K
}% [$ n. C+ D8 [% \9 r a
try { 9 P4 i3 ?4 @" S+ T
con.close(); ) R' O4 g$ @5 |& u
} catch (SQLException e) {( J# d2 H+ x1 f
// TODO Auto-generated catch block 5 R2 h4 B) t' I
e.printStackTrace();4 ^( K$ S. e+ Z9 G" g4 {1 A
} [! ~: V% P, Q& V1 [
} 8 \2 F0 a- A, o$ l& S
9 F3 }+ k5 @( w; w
public int update(String sql,String[] getValues){ 5 `$ C8 C& d8 H7 l9 i0 Y! c: R
int i=0;" m6 T; e9 \0 u& s+ T9 M! ]
Connection con=null; 3 D% ^2 p2 `% @7 b
PreparedStatement past=null; : ?0 t) A+ m+ ]
con=getConnection(); ; R; p9 c1 A2 m" \4 F# D
try {# w! n& E; }1 M% y. J
past=con.prepareStatement(sql);3 b9 o: D0 a* c- b! L/ T: F
if(getValues!=null){3 l2 e# h' _, `0 L% w
for (int j = 0; j < getValues.length; j++) {, X: y9 `( q6 N; p. T
past.setString(j+1, getValues[j]);2 I9 |2 l; I3 b1 l' C5 m, Z& @
} $ N* X0 R" M- {/ d p6 Y' Z1 F
} ! k5 f' q V0 Q0 S: Z K2 |6 S
i=past.executeUpdate(); 1 c& E! R6 Z, }7 H1 W) K
} catch (SQLException e) {. [. B5 d/ R3 j1 k
// TODO Auto-generated catch block ; ]6 A" P, U* k
e.printStackTrace(); - [2 Q* T _5 N) B' ]% S6 a
}$ L2 T5 @% J" u q! w' H7 Z; G( i! d- G
return i; + _, z7 C! L2 Q% I+ M" o) s
}3 k# j' F y% M+ Z
}" g# c- ], A2 W d- b' x2 r
复制代码
package s2.jsp.zhangxiao.entity; - y9 F9 t& W( U9 T
" j8 P8 p% e8 T
import java.sql.Connection;* b4 o* o/ E: d% D# H ~+ b# h
import java.sql.PreparedStatement;- M7 k. v9 R6 \# Y2 K' L, m
import java.sql.ResultSet;6 t8 X( g! Q$ Q; J& w
import java.sql.SQLException; ; r6 t+ E K( [
import java.util.ArrayList; ! K) p \9 V4 b/ s
import java.util.List; 6 V. v7 t7 B. e6 ?3 U2 O0 n5 G
% H. j" w: `2 t X7 N3 _
import s2.jsp.zhangxiao.dao.News;9 u) Y* k7 ]% _8 L
! U7 J: h* y) @3 E7 O! K) P/ O6 H* t* F
public class NewsDao extends NewsBase {6 ~) Z6 O) m+ A+ F& |0 }" b
private int count =2;' H: p, y: L5 [7 U9 O7 O7 c
+ T0 ?8 L: |1 C' B
/**- w7 W8 w2 N9 u" E S" B8 q- d( F3 ]
* 查询总页数( ~3 g+ Q: S( P! S3 K
* @return , o. V9 r' {7 E; T$ v
*/ ; Q4 s( O* f% c+ ?
public int countPage(){, j J7 h% m. F1 P- l
int rows=0; " ? i, D9 C& N3 ]' |
Connection con=null; 4 V/ \0 H& G8 E7 |4 L
PreparedStatement past=null;$ D0 v0 o, _" _3 P. D& x% ^
ResultSet rs=null; 8 z: h+ m! O% F
con=getConnection(); - f- R2 F u( y- \
//查询所有的的页数 返回受影响的行数9 P( V' g# @- V& G( ?5 B
String sql="select count(*)as c from news";- m# _& z, U. S2 G1 O: J9 E
try {0 X) S% A0 p- e9 C8 U: l
past=con.prepareStatement(sql); 9 h& H2 t( k) g& |, p6 w
rs=past.executeQuery();6 V4 ` k' ~' L7 h* r7 @
if(rs.next()){/ ?1 V+ G6 x& N8 B
//将所有的行数保存到rows 变量里* M% g7 s5 `. I6 U
rows=rs.getInt("c");. ?- @5 ~; B" M2 r
//如果显示两页 count 代表显示的页数 2 F) K2 q. C- b$ q
if(rows%2==0){ 1 L$ m( j4 n- k+ I( Z
rows=rows/count; & G1 a5 V& W: I0 {+ i2 e3 I
! J9 j1 b8 b$ |8 u9 ^) v
}else{ : ^& s1 r" e+ ^$ s) g5 h
rows=rows/count+1; 8 ^1 E3 h* w, n) L
}1 I4 y. H) `5 N# g. C" U- ]) z) R
}8 u6 I. n% {4 M4 L4 i) h
} catch (SQLException e) {5 A. C6 a0 W- i4 C% n6 H" l V
System.out.println("查询总页数方法异常");) C& }) z6 V% J6 r; {2 q7 C
e.printStackTrace();/ r5 ^" v: W+ m
} 5 ^$ X7 k1 r; V9 H! C2 H
return rows; 2 W/ t; ^4 H- @3 X8 m; d
}, d1 i' f0 R4 t5 ?4 S3 o
/** / \1 A" [( }. H- |
* 根据id删除信息' U1 {( O5 Y, K3 u- G1 O
* @param id * h$ ]8 l$ k& A5 p& x2 |
* @return 6 n3 B3 _8 w( j, J1 `4 X) c5 ?5 Z
*/ B2 w6 K, `! w2 P! b: g# e2 O
public int deleteNewsId(int id){2 Q* Q5 Y1 [1 d( A [
String sql="delete news where id="+id; ' d- j3 |8 O' c5 M; J7 G
return update(sql,null); # ]9 }! ?1 h C) K
} : b0 M' X1 o3 y+ j/ M) ^- T7 g
/** * x. ?2 j+ X3 c; w( B
* 根据总页数分页" c/ I$ W, U: v9 X' p% w
* @param page( `" m' D6 A7 G3 E+ k0 x
* @return$ z; K/ u1 B& j Z
*/9 a1 ~* l3 C( [8 X3 r6 T- K; {; N
public List getPageList(int page){ + l9 |) P2 \. @4 E* A3 F
Connection con=null; w* g; q N$ i% A8 ~; N( b
PreparedStatement past=null; F1 w" D" ?0 g/ A+ b; ^
ResultSet rs=null;( L, u3 }) {' c6 ~5 ~- M
List list=new ArrayList();, H+ Y# n4 S3 E2 E7 }) p
con=getConnection();+ K6 \- \9 M" a
// # k$ W+ q7 {8 ?1 k" u H8 l& }: e
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"; $ {) i6 b5 L: r& Y7 T2 |- g