TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;* O; I# M6 I; s9 }
/**
6 N) m/ S+ R9 B * 实体类
6 H' w6 e6 m7 `" W3 H: R+ F * @author Administrator
2 z* Z9 |. R9 i$ u7 X *6 V' r' s$ U# c4 {: d O
*/1 q3 m) l1 q: z- ^
public class News {
3 } E, ~1 w3 G' P; _ // id, title, content, pubtime, author
* |5 E& V$ n) f. ] q private int id;
1 {" P8 @6 p" G6 U- W& `, U2 a private String title;! \& p' }0 J* G G* b
private String content;; ?* D2 Z, Q! J1 g7 w
private String pubtime;
B. N4 D* } q5 D private String author;) D% m* B0 I/ s0 |7 ?( i, X- U
/**
% r$ E1 [7 m9 d( z2 H; t * 构造函数! M2 ^( L! e; }: S' P2 r% M
* @param author 作者
8 C( N1 X B5 | * @param content 内容
+ f: I# i ~ J1 B * @param id 序号! k# f" e) R7 L5 |& {( Z
* @param pubtime 发布时间% H/ r* `% H3 Z. `7 ]* L
* @param title 标题% ^( V6 Y! z: i6 ?( B; F$ M6 f# `
*/' ^! f( s$ v. I$ S2 B
public News( int id,String title,String content, String pubtime,String author) {0 R4 |) _2 {1 W9 F! C1 a" X
. { H, H* w. {$ o* I this.author = author;3 {4 e2 @; f+ O M Q
this.content = content;- m2 {0 C! c+ t0 H3 E6 Y1 r
this.id = id;
7 Z: y, N" `6 K { this.pubtime = pubtime;, k( _! @/ O# ]" c% d1 y) b, g
this.title = title;
8 t. r f, x; H }3 P( `) s8 I9 r K! G* f
/**
?0 B$ G6 q2 g( u: x4 O7 | * 无参构造函数
! V8 M* J# K; r- S- n! O7 n */( V8 t; B5 K- D
public News() {
3 L. _0 W0 I, _0 r/ I; u+ x$ d( X super(); T2 m' y/ E) `& L; a: a
}% U; C+ o- {" _( k: @9 g2 i
/**' J. x2 S+ O0 |( m# S$ {! c0 G2 q
* get ,set ; T3 O9 W4 [% K: V; D2 J$ d9 {
* @return% r J8 T# h4 h" x+ Q
*/) `+ {5 M6 r2 j5 p2 q& b- Q
public int getId() {- G8 U- C q/ x" \/ T( b
return id;
' i# [/ @% `' T$ b# n$ r }( a" m P' e1 \9 ]+ q# L9 s+ ~1 k
public void setId(int id) {
7 p- G. H4 o9 @ @& o6 m8 O this.id = id; G% K- Y) X4 u& j
}1 q) C9 H- g' c! Z
public String getTitle() {7 K' E K* }3 M: j2 E( D
return title;' ?& @& q' w% m1 z4 P* ^( E/ C
}/ H& f- C; \8 x7 h" ?1 e
public void setTitle(String title) {) p& w& r8 Z- L& {& n
this.title = title;0 ?5 q9 m& X0 K) w9 w. t* H( R
}6 z: h: r) i" h+ A
public String getContent() {/ y9 |$ N- V8 t& s( f. I c
return content;# a6 g& W# s% \* q* |! o
}
( w# o- w" Z2 e2 t1 \/ _+ L public void setContent(String content) {
& [) M1 A3 S+ E$ H5 Z this.content = content;$ {7 b- n( ], n* u3 I* _
}
0 _9 j6 a7 S2 k public String getPubtime() {
% u! i. \ v# S' D5 Y% @: z return pubtime;) y( ^% c2 w* U- g1 _& t
}
% E- g1 w. f# h public void setPubtime(String pubtime) {( I# ?! C7 x3 f' Z3 Y
this.pubtime = pubtime;
! U; B( t4 A% K4 ]: f }
0 B$ S, y5 a: v public String getAuthor() {
$ \7 i% W. q1 h% f9 c# j return author;
/ T% _1 C" p r; A( t. B }$ J4 ~6 s3 f+ N: a7 t
public void setAuthor(String author) {3 _ j+ z& O. | b1 @8 U% K' B
this.author = author;. N$ Y% N+ w* m# ?. Z4 ]2 c O
}6 y, J8 u# ?$ K1 u! E" z
# H+ [. E9 H: X( u }
, j {" f! n; U0 a1 s* S- ^ 复制代码 package s2.jsp.zhangxiao.entity;+ O# O Y' h9 Q% N6 ?
import java .sql.PreparedStatement;" U' A8 i! `& M/ a
import java.sql.DriverManager;
0 Y. y8 L6 t& a! P) G2 Y) i7 c% @ import java.sql.Connection;
! X8 _* h1 m% L) \! ]% F import java.sql.ResultSet;( R* E; s- q k1 V) D5 J2 {
import java.sql.SQLException;+ x0 m$ s/ Y$ E5 q" E2 Q
3 K( B; Q6 J: v" Q. G5 l8 J
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
, x f- W% w$ l# B
8 _9 G2 ]2 B6 ]3 @9 F+ Y9 ? public class NewsBase {
* A1 [0 Z1 Q$ n! T5 [2 F 5 t* T- L9 l" t: N# I2 ]
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
! J P+ I! A0 @9 a private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";1 I9 I L* Y6 t2 k
private static final String DBNAME="sa";/ B: X2 \- b6 ~: _
private static final String DBPASS="1234";
3 @2 q1 {) a0 b' V' U
, v- d, y+ v ], t; D6 n public Connection getConnection(){
9 _2 ~6 {: x E Connection con=null;! A. c6 x. e) {/ C
try {# y. [4 ^. ], C" n3 @* ~4 x
Class.forName(DRIVER);
$ S3 D6 N0 _, v+ v# ] con=DriverManager.getConnection(URL, DBNAME, DBPASS); t% Q+ l8 l. x6 r
} catch (ClassNotFoundException e) {
8 S$ ~7 A4 `" N# `' \0 {0 q) i System.out.println("加载驱动异常");
- N5 w. M4 u( u, b p e.printStackTrace();6 [8 i% E+ E2 F, d. U
} catch (SQLException e) {9 x+ o- r/ c: H! K' e/ ~( O
System.out.println("数据库连接异常");
/ _# l" ?$ h2 G! g0 o e.printStackTrace();
/ j8 U& ~# z, j: e& Q }
. S+ R" K1 g) d' d% r& l& ~8 ` return con;# t8 J9 z$ Z4 t' B, i9 X1 o
}
* ^+ a8 W- J' C$ R
3 i: I$ U6 @! [5 f' @ public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
. l, r$ c" k/ y3 {+ D try {
" J2 H+ d$ n) N, |1 s+ k rs.close();
2 {8 L3 o# s% w } catch (SQLException e) {# B9 o% r2 m( g2 U9 Z) B4 C
System.out.println("");
4 s0 ]/ S; q5 L. x e.printStackTrace();- B4 V# x* \: [& F4 ]
}
( S3 l( R9 k, `- A4 G, e) N8 C try {1 L, P4 _4 y. |" h' o; g" Q/ a) D
past.close();' O8 R* Q: X" Z3 A
} catch (SQLException e) {
0 G( x: k' x" N& ?" J) V System.out.println("");& t8 q4 M" F t. _
e.printStackTrace();
3 _5 N, g6 w4 z. g7 s& y3 [ }
1 ~4 b" b" n0 E* P" r7 e; Y try {
" S9 a O/ ~9 F5 t3 l con.close();
1 {8 u# Z. D; Z5 W. G- Y/ N } catch (SQLException e) {; n2 T; I+ z$ E$ s
// TODO Auto-generated catch block
6 B' `& u! `8 T e.printStackTrace();6 u* L6 p, O1 V+ B+ r$ v4 e0 C& z2 v
}
+ k K+ A( z* {: t; X }1 D; I0 B* {* T B
" v. c. t9 E: M8 @7 U public int update(String sql,String[] getValues){7 ^8 u" S( r3 e- d" }, h: s
int i=0;( W9 _+ h/ z- U9 ~6 F% N7 \
Connection con=null;
( s# ]( b, b. Q. m5 W1 c$ L* ? PreparedStatement past=null;* }% P+ ?/ E7 \) p( u1 h
con=getConnection();, C' x# D0 X+ @$ Y
try {
" G v7 k9 Q0 x/ F past=con.prepareStatement(sql);
& q o; I- H& }" Y if(getValues!=null){9 \4 i( a) n9 W; F/ H. _# a7 i
for (int j = 0; j < getValues.length; j++) {4 @6 C; @0 F# A& T4 _/ {
past.setString(j+1, getValues[j]);4 B# p3 @' _5 ?' v9 D& T
}
$ o6 u+ O. n) a9 V8 T" Z }
7 w) n2 ~9 B3 A2 m% w; M i=past.executeUpdate();
/ B' T' R2 V f' K } catch (SQLException e) {% b% U: x4 c/ E( ?
// TODO Auto-generated catch block) g" ]( e0 n/ F
e.printStackTrace();% x) ~, J" h9 y- x" \! t/ W; y
}
5 C2 W# V# N; W* P return i;' ^/ E j% I# H
}- r0 N1 t* |: X$ ^
}1 l1 A& v, F' | z% g
复制代码 package s2.jsp.zhangxiao.entity;
. T2 @) }0 F! N; f7 {8 s l" H/ Q : M9 g# i& N$ E) r+ c( `! W1 b( L
import java.sql.Connection;
9 N6 t" K3 v9 e* k6 M. T import java.sql.PreparedStatement;2 G2 l# r& N4 \$ h
import java.sql.ResultSet;
; U4 q4 a5 I2 a: R' x7 D/ I6 F import java.sql.SQLException;3 T/ [* b4 @ y" k3 r
import java.util.ArrayList;+ k% z. y+ D1 e- r) ^6 p
import java.util.List;: h A) G* u6 P/ @7 \% _
4 z8 i% g- P9 |- C7 q8 }0 u, k
import s2.jsp.zhangxiao.dao.News;
* _6 U! x# j( r
" N- [6 F9 m4 U1 z4 m public class NewsDao extends NewsBase {$ P$ R( N- {" ?
private int count =2;
/ v. g2 I1 Q7 o0 k; a
( p5 v/ k$ w" ?: O /**2 D" W1 M4 F6 D) r J4 k+ R8 I8 Z
* 查询总页数
3 u8 A6 m/ I' j$ C7 r' D * @return. V# Y$ G% s: O( }* ?
*/4 X/ i, ?# W. C" S% R: I* G" G1 O
public int countPage(){
: x) `' `2 P3 G, p i4 s4 Q, S/ n0 `& n# p int rows=0;
6 U# X) I+ E" a5 ] Connection con=null;
9 Z; X' U7 M5 ~+ S. t PreparedStatement past=null;: N% N! {: m2 h# p/ E
ResultSet rs=null;- O+ C; g6 x) u6 {/ F# a' M
con=getConnection();
$ \2 a5 I/ U" ?5 b8 B //查询所有的的页数 返回受影响的行数0 n6 ^. s5 ]% D% Y. ~
String sql="select count(*)as c from news";/ z3 U4 v2 q1 r; \" f7 E8 ?
try {4 e1 ]; S2 G" W ^" x
past=con.prepareStatement(sql);
5 z" w$ B( c) X( _ rs=past.executeQuery();& ~6 `: X- ~# ]
if(rs.next()){
4 t% ]" r& \: p! i0 Q) K) Z //将所有的行数保存到rows 变量里- _3 ^0 N3 B. X! ^9 C3 a% }+ M: v- N
rows=rs.getInt("c");6 S5 ]% V' E c$ s- B
//如果显示两页 count 代表显示的页数
" ?' M% k& I4 T' ^: Q if(rows%2==0){' C( j k/ u* E$ A& H/ V
rows=rows/count;6 X1 G, D; q% x% K; |. ^; e! s
( e/ f7 G/ l; B. }( U6 V }else{4 O" d6 |- H0 e6 V( y
rows=rows/count+1;
1 W% {4 D/ }1 E: U+ o7 P) g7 U }
z8 E) m/ g% U1 V, _! v7 m+ U }
3 j5 ~8 F3 x$ y# H" a- G9 Q: E } catch (SQLException e) {
8 @8 Z; F4 U3 q" h9 N7 G System.out.println("查询总页数方法异常");
! H) Y, }% r* b+ v- g; V0 o% K, O e.printStackTrace();" e% v1 w- V4 g7 q9 ]: f
}
0 ]8 O" M$ P6 i+ J return rows;+ O9 n: N* i; x
}1 j* j) w, ~4 L' ]7 d
/**
+ w/ E1 A' x" U: n3 L: E+ z$ [- |$ r * 根据id删除信息6 S! O/ D$ N! X
* @param id
/ P7 h! n, Z: |. c1 g * @return
/ a: M; w1 K H) R */
* A: M q0 _+ I public int deleteNewsId(int id){
- X3 ?8 c* T P( f String sql="delete news where id="+id;% W* g# n% a, u/ H( n. |
return update(sql,null);( Z; ?! t9 F7 z1 k/ Q
}7 f% Y7 U! y7 a' @3 t# ?
/**& {. N6 a$ \& l" i
* 根据总页数分页
3 L! E1 e% w7 j' ~- `) c5 a: S: V, y8 Z * @param page2 G6 ^% u- r2 _# o Q. t
* @return2 |% M5 z+ @4 W1 u
*/
, h3 J7 [, x8 I1 r' s public List getPageList(int page){
) t6 B3 x/ n8 T4 o( T9 G$ C Connection con=null;
8 G! _/ t# i! b! C! n" k A PreparedStatement past=null;* U. B3 ?1 u7 b( _ w8 K) f1 P" d" Y
ResultSet rs=null;
1 y/ P: D1 K j4 C- \ List list=new ArrayList();) ]2 |3 o. ~/ \4 b: h* w) Z
con=getConnection();
" O. j& A% k1 ]" V2 [5 R /// _# X) L( J+ D
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";) [1 i" d2 d3 Z- ` H
try {
! O' k# i/ P! Q' z! a past=con.prepareStatement(sql);0 Z0 c q, _8 v
rs=past.executeQuery();/ U- @+ `/ h9 z/ @5 ^' h7 \
while(rs.next()){
+ d. ^6 z% C5 k1 p* M 1 n! R+ k" U4 A1 c6 D: S9 c) f
// id, title, content, pubtime, author
+ [9 q( ~7 ?- Z int id=rs.getInt("id");$ J$ [5 C! \4 p. {) M" t
String title=rs.getString("title");
, b. w" R0 r, v7 y7 u String content=rs.getString("content");
8 _: |# \9 y) D& U String pubtime=rs.getString("pubtime");
4 e0 e; O8 D" q" X String author=rs.getString("author");
1 s' p% |4 x# [9 P- @( `, y News news=new News(id, title,content,pubtime,author);8 J( f; B! G. [9 ~/ z
list.add(news);& l: s& ~' R: C9 e3 k Z4 L' W5 M
}9 r6 {2 C/ l+ F
} catch (SQLException e) {& a9 `! ^5 E% r( O. {
// TODO Auto-generated catch block
9 {# D- f0 E3 W7 K* A+ S. S e.printStackTrace();- n1 Z0 Y( Y6 v# P7 w6 ^) G
}
3 M6 G3 {# F' W3 B/ u return list;
" `& b* j, Q: D ?9 I
/ d1 f( V7 M# O5 k; J# W }
) s' l! s( K- ~3 d }$ E; D' h8 j' ^2 J8 h
复制代码 9 E# j' f, a+ A2 q/ c& ^
8 ~* o2 H: a; S( W) K
源代码以及数据库下载 : & A9 o# t$ u# F) G* ~) e/ ~
3 G/ m3 B( T- b
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :