TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao; q7 o) s1 h' D! D3 H2 W" {/ z
/**
+ w3 L8 R1 r: b9 F# x8 d/ a& l! h8 p * 实体类9 }$ `( v8 @) n6 J5 {0 @; ?
* @author Administrator9 Y" C' f/ B8 g% h% }8 T) g4 Q5 q
*
- T, j( ^% J- s- K */
+ A, F" o" [3 r, U. F6 j1 P public class News {5 ]4 G/ g( G& o4 K7 h+ i
// id, title, content, pubtime, author
( C! P; H. {0 Y6 o8 r) B. ` private int id;% n. M, S, V' R6 G" Z0 Y
private String title;
7 O5 l5 u7 h5 c$ r7 r private String content;
7 p( E6 I$ G$ |+ [2 i private String pubtime;2 [( G5 P1 s; ^# A9 R) N
private String author;. p8 i( i( h( ~1 D0 I ~6 w
/**
% x! I. D" W" X! y * 构造函数
& C+ d+ d5 y. c+ D( }* I6 z% a * @param author 作者, Q0 v& g( T$ ~7 ~+ v
* @param content 内容
7 \$ ^; H5 S& J. k * @param id 序号* k. M. o* i- K0 ~
* @param pubtime 发布时间
- K$ A) w& I) p! Q1 P9 n * @param title 标题, e" d0 w2 F) a' e2 j. J" x/ f
*/+ R" Z q5 J) l' i
public News( int id,String title,String content, String pubtime,String author) {
X( N2 _6 V4 \9 D- B+ F5 ~# ^+ F * }, P9 |, [6 }7 ~) c) Q! k7 Y+ z
this.author = author;
( ~6 S& Q- h+ F# F. l4 P this.content = content;9 z6 V& t% Q" p& a
this.id = id;2 d/ N6 P3 h% S. _, w; d9 P
this.pubtime = pubtime;: I3 m' h6 n0 ^" \9 H# N* w; V
this.title = title;! E5 B( W, M0 a g+ \
}
( {! m! a+ q" e: u1 X3 ^5 H /**( V3 r9 A9 m* G
* 无参构造函数
& g7 ~; p8 ~# h- k1 J8 g/ l */+ ^6 v8 `" c; _5 p! G
public News() {
% D/ I2 r2 }& f5 c) E- G super();
a- @# J0 {# t! i, W }4 t: T, Y3 }+ N; p, ]% T& l
/**) I* b" X0 j, o: ^7 y; J! E! U
* get ,set
1 K3 u' r) r) f+ `9 J" D" @5 | * @return9 s2 e) q) v# g- r4 u6 @6 f
*/
" M1 ]- u! j% } public int getId() { D4 e& U2 k. A
return id;( U9 q7 D$ v- F. |# D
}0 e& {4 d" r7 b. y0 t: q. T
public void setId(int id) {
2 g3 T+ n" C# E! E' D5 S4 L this.id = id;
- M1 O- ` I) a# N4 U }: u r* e/ v8 Y! G
public String getTitle() {
: ]+ {1 v7 N3 Q+ e( K+ C8 Y. q return title;5 i/ x; d- c& } u" t8 G& K
}
, a# T3 m! y5 f* n% o6 ?# e0 g" R public void setTitle(String title) {8 C' M& N( ?. C4 r/ ~* u3 \
this.title = title;/ }& m& I$ t1 t. W3 ` u6 A
}
0 e* ?0 _% \9 k public String getContent() {0 _7 a+ ?4 j2 r J
return content;
* j" b0 W* k' P& O }- z" N8 _- _$ Z. m& J3 {- Q- [
public void setContent(String content) {1 _8 f% r Q0 u' J2 q
this.content = content;
& f! X& H$ r8 _7 V9 y4 ~ }
N+ R5 C* z# `; ? public String getPubtime() {
9 Q0 A% h) k4 D+ K7 p8 L9 w return pubtime;
) n6 ?; \$ y# }4 J2 m+ w }
& l4 T5 E& k9 q( G0 l public void setPubtime(String pubtime) {9 C7 i& C0 r& q# Z9 b1 B
this.pubtime = pubtime;4 J7 Q8 A, z# d" o" Z7 r
}
; \# _9 p7 H! q! [( D" E public String getAuthor() {
# c# }! y9 t. [ return author;
. u, o$ O% Z4 D4 D- c" j L; B }
5 a$ P4 v8 r7 }+ n( } public void setAuthor(String author) {' `2 A! f8 Q* |* L. D% O
this.author = author;$ F7 {/ S6 ~& v
}
+ M0 R& j. f0 w+ X$ ~" S 0 {1 ^2 o& y8 ~" T
}
# | R* q3 f* S: ~0 P6 x4 c 复制代码 package s2.jsp.zhangxiao.entity;& I, L! r6 `* q4 H9 q3 O
import java .sql.PreparedStatement;6 p1 |. r, ?& Z3 }7 H
import java.sql.DriverManager;: W' A. J: C( f X7 X
import java.sql.Connection;' }6 H& X) K- q4 E9 j
import java.sql.ResultSet;4 o0 M. G( M, x {2 z$ |+ |
import java.sql.SQLException;& ?, s" X: @' X1 y: d4 z
g5 N" i/ z" [' y+ o- E8 a u- V- P
import com.microsoft.sqlserver.jdbc.SQLServerDriver;# @4 |0 {( X! l4 X) t9 k
" m2 v, B) q) f2 q( j public class NewsBase {
e! s) f y6 _( U% p& }# U
2 m* s/ R! B; I1 Q I9 | private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
6 f( c4 O9 G8 u) I5 } private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";
7 N4 A0 k' p7 @5 F# @) T1 o private static final String DBNAME="sa";
" s2 d" O; H- a2 P8 p1 \ private static final String DBPASS="1234";
. u7 z; m7 ]7 N7 a
! K8 B2 A9 _7 ?0 c% [! E: Y, i public Connection getConnection(){0 H6 b! X# y3 t
Connection con=null;
* t( n$ y% D0 a" B* i* r try {+ \0 ^" ]) M+ |* k6 e7 t
Class.forName(DRIVER);6 \1 N5 ~2 u& ~- ~$ K
con=DriverManager.getConnection(URL, DBNAME, DBPASS);
- G0 i. m) o) O6 w6 a0 ` } catch (ClassNotFoundException e) {
; }0 H2 c7 f& u: m8 z5 e# z0 M: g System.out.println("加载驱动异常");# v0 A, F, M" f2 g
e.printStackTrace();8 n, h1 U! Y ]" d: O
} catch (SQLException e) {+ G( @5 P; E5 \
System.out.println("数据库连接异常");4 e% a1 b Z( P3 U! x5 a
e.printStackTrace();
8 ?$ q. n$ K* s2 a }
. `! \0 B" x# i: L2 l' r return con;
. r4 x2 `% v" E( |9 O: x0 W4 X2 y5 k }& r! ~0 b1 j5 a5 \* b" G, |
1 Z# o! Q% ~0 _ public void colseAll(Connection con,PreparedStatement past,ResultSet rs){, A/ _" e2 ]4 t
try {
! S; h1 X! J M- w$ s: I( y rs.close();, |0 x" \/ J7 F5 ^. H! q1 Y
} catch (SQLException e) {
' s& N; ]! c) q% V" r0 J System.out.println("");' b H$ L. f, U I" V+ l+ j) |
e.printStackTrace();
8 C0 q% [8 y% I& r+ D6 D }
" [7 T* _" b# b* b try {3 \, L& {$ D$ f; B$ w: E, V
past.close();9 @/ h+ J6 t9 s1 n$ |
} catch (SQLException e) {$ s2 N2 v9 k# D
System.out.println("");
" ^! {, ~+ F2 L6 P# N# \ T1 K, j, F e.printStackTrace();
- `2 _4 ~0 k2 y5 Y; `+ F }+ ?) o% J; P4 _0 y( E' F- s1 j
try {* q' C3 W5 j0 n5 i3 Y" F W
con.close();: n8 c4 f$ `& S+ ~
} catch (SQLException e) {
' T8 |- g3 m5 y4 F) X // TODO Auto-generated catch block) L' ~/ g! ~5 e F- w& R' X
e.printStackTrace();
$ W1 r, o" K- E) G$ \ }
+ O8 T3 I: \% O2 i- {* P }( b3 P4 R" ^1 i) W9 b) X4 [
! e2 n n5 E7 O: e. Q; y! p l public int update(String sql,String[] getValues){
+ x" Z. j, E3 S4 | ?* h int i=0;
3 |! Q4 p9 h* T: n9 i6 O$ I2 Y Connection con=null;
5 c' R2 {: m/ j5 e: e: Y PreparedStatement past=null;
5 S3 |/ U( W8 }' p4 D con=getConnection();
. d; f/ m) O) J- j try {
% @, `+ A$ b6 f past=con.prepareStatement(sql);
) i L4 p; y% l& V `6 L if(getValues!=null){' r5 P2 ^9 i$ q
for (int j = 0; j < getValues.length; j++) {
3 b E% k+ v. w6 o @" B$ E past.setString(j+1, getValues[j]);; W# k; P. v* @0 K: A u/ A6 w& c
}
9 Y- d& ]/ t- g2 _( ~+ n' N$ t/ |4 j }
( T2 B3 r0 l; B* ~; \ i=past.executeUpdate();" {( [+ k. b& b, O
} catch (SQLException e) {! _! n: F I4 M3 @
// TODO Auto-generated catch block0 k' t8 F" v6 ^3 x) E" h
e.printStackTrace();
r/ G4 T- l. q3 J* U }; P' D+ _+ w' h0 ^
return i;" b/ _7 W( D# w5 R5 Z* {: ]
}
$ q# Y3 [1 T9 k0 ]" D: y3 | }' V F# Q3 w: k" E, v9 g2 n
复制代码 package s2.jsp.zhangxiao.entity;# t$ M# e8 F Z# ~' J& R
) {$ g. {7 y( O7 O( t8 h+ F+ m5 v
import java.sql.Connection;
- `- }$ {, I( A' r# O import java.sql.PreparedStatement;
" D8 Y1 ^! p4 O& W7 H$ @/ u' ~ import java.sql.ResultSet;
% U/ o$ t t& p8 N5 r" i$ w import java.sql.SQLException;" X" T+ T# |, u
import java.util.ArrayList;
' T4 s- V' B/ l import java.util.List;
8 y& n7 P9 c' _ / F5 m, Q2 R: L
import s2.jsp.zhangxiao.dao.News;6 k/ u) h" X4 m2 J
7 {3 p! k* z2 Y/ F- n" \, f
public class NewsDao extends NewsBase {& ~$ ]: S7 A- ]6 y
private int count =2;
6 G8 j- p8 V/ F3 f, y0 o 8 L/ X8 t/ K7 R2 i
/**+ ?( j5 d. c) w4 k
* 查询总页数' w% M' r% K- f: q5 |- X
* @return" j; j. T; ?6 r, \
*/% k2 M- \, q! ? k! w
public int countPage(){! i) h7 Q3 s' l$ A: C, i0 G
int rows=0;
, R% Y) X8 h) Q% d# ~) |1 g Connection con=null;
6 J0 J) Z. ?- Y, U PreparedStatement past=null; L- ^/ X" M) {; i& J K2 h
ResultSet rs=null;
( l, O/ U6 {2 h) S2 T$ Z* t' a con=getConnection();
. Y$ i* H6 r$ f //查询所有的的页数 返回受影响的行数
4 w" G% M; [9 h2 c String sql="select count(*)as c from news";. }2 ?- e y1 v! O3 \. i1 A
try {
+ _+ v0 P% x) h$ y: |' n4 J past=con.prepareStatement(sql);
; G8 p3 b6 n6 J; F rs=past.executeQuery();
7 A) j/ n) e5 n! [ if(rs.next()){8 V: [& l1 ^5 O: \3 p
//将所有的行数保存到rows 变量里
4 E- Y$ z* E2 u6 U* H, ?8 v- e rows=rs.getInt("c");( J k* d% p9 D0 x/ {/ \
//如果显示两页 count 代表显示的页数
! ]4 {8 K. P. R+ a4 @: Z/ K* G4 t if(rows%2==0){0 x4 L7 G; R* `' Z+ B
rows=rows/count;* B& R; Y5 ]: c6 z/ K
+ B Z# W2 p" B3 D( |% Y
}else{
6 v4 q! B; I4 V g* x4 _ rows=rows/count+1;8 p; K t/ U7 _$ ?) J2 P
}2 S' R# S1 Y5 w& Q7 [( W
}9 I% B8 Z% o1 I+ f6 z! j
} catch (SQLException e) {# A: n8 H: _7 p7 m
System.out.println("查询总页数方法异常");
" [7 Q1 }7 u) w% l+ N E! N e.printStackTrace();4 i, P$ m* b1 e; p
}6 c" I: z# W$ ?4 s" c7 ?3 v. B
return rows;7 m. h; D1 E9 F. x
}8 l1 z: }4 {% h3 g+ P m0 {
/**
) _! o( E$ {% u) v+ r! F * 根据id删除信息
- h* L K& [% a0 D * @param id$ K& S3 F3 q0 f8 D8 E
* @return
, k3 h. v2 ^9 n$ t+ ]7 I */
9 B6 d/ a" F% |: ~ I public int deleteNewsId(int id){& Q6 }! d7 U3 O. I# {; M% \
String sql="delete news where id="+id;' V* E9 I2 \1 G! Y& x5 y2 u
return update(sql,null);. A1 R9 ?- u3 J
}
9 t; C3 L: A/ ^. W6 D% b+ s /**' S- L% o+ t2 L1 m+ h$ h1 ~
* 根据总页数分页0 l5 `: X1 _6 e: R9 k
* @param page
" G8 L* Q: m5 j8 }( v * @return9 K: ]5 Q+ o- ]" m q
*/
8 b9 m8 r4 \! I- G* S public List getPageList(int page){' t4 B7 t5 X7 l; p/ E) Q4 `
Connection con=null;
9 j" Y5 Q2 O& [ e4 h* R q; K% ` PreparedStatement past=null;
# t# b- C1 ]2 w, D ResultSet rs=null;
8 U1 T0 X4 d! { List list=new ArrayList();
2 Q8 q2 F# d! _6 y. c3 g con=getConnection();
8 Y( B' U- {" p& r //
! [ _! u1 Z9 b" v, P 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";
2 x# L% E) ]+ ?" u try {
4 w) K5 Y# }+ P, F& a; p# X# o past=con.prepareStatement(sql);- G! o; \, e2 s
rs=past.executeQuery();
3 K4 c% J, X3 S while(rs.next()){- a& v7 H" I i- M2 x1 c
) o8 p# P, M6 d6 Q
// id, title, content, pubtime, author$ }! V% S: p8 I
int id=rs.getInt("id");
6 Y; V& A; V+ h; _' {8 |: e+ T String title=rs.getString("title");
7 c! a% W7 E4 b o9 g# g& n String content=rs.getString("content");7 n6 [: H" q/ _/ K' ~/ E
String pubtime=rs.getString("pubtime");4 ^( ^( V, Q1 `0 j# a. k$ n! ^
String author=rs.getString("author");; U, D1 z9 u9 G/ g1 O
News news=new News(id, title,content,pubtime,author); H9 |/ T C" V: c* l$ E" K
list.add(news);
+ F: D) m! O8 y8 ~ }8 q! o* q# Q% P4 I
} catch (SQLException e) {! J& ^2 Y( }. g- ^6 E) B& Z
// TODO Auto-generated catch block" t; ~; j7 N4 d; J3 T7 \; |- `& x
e.printStackTrace();
* e" s8 ?) J2 _7 T1 G }
2 P( _6 s8 L+ ]8 \/ l' g return list;
/ Z+ I, z: R5 C( }/ `
V) f: a* U8 J# R p }1 `' y: d' S+ q) @) S
} Z$ \% j9 P: Z9 Q- T% \& j r: g$ p
复制代码
# b, i5 G) h; ^3 j+ E9 d
, e5 S- ~5 z6 O9 P a2 r, n 源代码以及数据库下载 : + Y3 N, i$ a9 L. B8 ?5 s
% C) s% b6 `6 r4 P
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :