TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;) P' z2 G. l( l* B
/**/ [# ~ [/ J+ }+ H! V+ @
* 实体类9 Z# u/ z1 ~( @" Z
* @author Administrator
" y& A1 o, R% i a+ i/ Q5 { *
; \3 S4 S0 P! J; [3 `' B */
' E y+ c# _0 r4 b$ Q6 P4 T# f public class News {9 y8 Q) ?( i1 h! m2 Z
// id, title, content, pubtime, author) c% ~ d: P& H6 f6 x+ U2 C6 [
private int id;
" s" R! [& G/ @0 F private String title;% a7 J. |4 j" S: a0 Y+ T
private String content;( ?3 O2 }4 f( r H% P S1 j
private String pubtime;* x" J% R3 d) S8 M! S4 F6 c
private String author;4 c$ l8 [1 M2 L- @4 o
/**
' n- z0 r/ H ]3 { ?6 w; j * 构造函数
; Y5 F) e, ~3 z * @param author 作者
0 ^1 V9 [5 h# ?, U * @param content 内容
: t9 ?% J2 h/ C * @param id 序号( B9 i8 D* D" s# v1 T7 m
* @param pubtime 发布时间 J; J; f+ ]0 P
* @param title 标题
' k% m2 a _: J* U0 H9 ^7 h6 f( p' a */
* g7 N; j' }' i- n public News( int id,String title,String content, String pubtime,String author) { ~8 R4 j9 g3 l7 a% q. S( ^
" q7 V- s2 p2 F* E# b/ [ this.author = author;
/ B- f+ m- H7 H1 U" C0 B this.content = content;
1 P% {. w5 V( j+ j0 Y( ^ this.id = id;
$ q2 k; Z2 G8 c this.pubtime = pubtime;
. o* \5 v7 k7 }, F5 k+ S }2 F this.title = title;5 m. ^, L& j, G' q6 H% _
}
9 n# s" P; x4 ^9 i7 |+ ~! W /**7 E( }% z# [0 j& k( W, b
* 无参构造函数
4 y0 O) K# p4 N' B$ s. A */7 J, Z- |' z1 m5 i' h' c2 g( q$ A" t
public News() {; K# a3 A+ K2 ^; X' `# I
super();" z! |3 R# d3 V" ?+ v
}9 \4 i$ l/ D# U2 A
/**
$ _* h' h3 @+ _- v. E * get ,set 1 B( |6 T2 ?# _5 a: z
* @return( i+ T% P# F9 b8 t
*/
9 l9 r4 S, Q6 T8 K* o public int getId() {# D3 n2 e" r1 r" m7 c- K
return id; S6 ~* u/ @0 Z7 [' S
}8 `; ^, n M$ v
public void setId(int id) {: H! r; f, _; O- v
this.id = id;- C/ M' \ a' k0 U b4 b. @ V
}
3 l3 M9 o+ V: d% \1 X public String getTitle() {
* ]; V! ~7 k# A; R return title;+ y& y' H; Z" r% N+ V! Q
}' m2 ?! j! A: v: g$ b
public void setTitle(String title) {
, u4 E7 N; C. V0 v7 b0 g: i this.title = title;3 F6 B3 W0 c b% Q; a' P
}
; H+ C& |/ L2 W) F* n public String getContent() {9 h# N& q% K' W$ P- O
return content;
- s0 G" X6 i! a/ e8 m l2 o$ X; s }3 j$ C p4 G# `2 E: O
public void setContent(String content) {; E5 ?8 F0 p' o/ M5 L3 P
this.content = content;! `9 J1 J, C6 ?; ^4 ?6 B2 k
}1 g- @9 g9 U. x% C. b0 Z. c
public String getPubtime() {) w. G1 H8 [. i2 ?7 S
return pubtime;+ O- c7 ^/ e* j7 I/ j
}
( q, R$ G: q0 P9 G; W5 C$ E public void setPubtime(String pubtime) {% j$ ^+ ~# `, h
this.pubtime = pubtime;
6 l4 m8 R, I1 O+ }' Y3 |. d }
+ I- t# E; ^& Q; ?% f public String getAuthor() {
- q9 J9 o6 E/ Z return author;8 o7 O3 x1 Z7 v$ b
}: U* \0 W& b) p+ A* j: y
public void setAuthor(String author) {4 ~& M, A4 c5 ^$ a/ D9 L1 Y
this.author = author;5 }9 d+ ~9 L: e2 H: u& C
}* B$ K9 s; q4 v9 ~2 e6 W& \" v
- V. P# j2 W5 Q6 ] }* H; r* e! U2 L S
复制代码 package s2.jsp.zhangxiao.entity;
7 j' M3 o& U& ]$ P2 f5 r3 j" |2 `. l2 C import java .sql.PreparedStatement;
4 c$ e2 J- }* V+ r3 g+ E import java.sql.DriverManager;
/ J! K. J6 T! R; w# y. ~1 V import java.sql.Connection;
; d& a4 ]1 O; v' R- t& m3 `7 N import java.sql.ResultSet;' d5 G" `' o( l5 v! x- A. c4 @
import java.sql.SQLException;
B4 i7 _+ D0 T
3 W2 A! _, z* ?) @' Q import com.microsoft.sqlserver.jdbc.SQLServerDriver;9 b/ I1 G0 F8 r1 p" C% B
# g$ c$ @* E- W6 q
public class NewsBase {( _& w' j, P e7 J
, G# _) @3 X( b; f2 N, ] private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
- b8 H2 X" Z, D7 x private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";
/ G! F5 c* f7 w7 O private static final String DBNAME="sa";, L3 R0 y: P: X: b: ~/ X4 u
private static final String DBPASS="1234";& t6 t2 ?6 K) f4 Q$ ~. C: ?1 j
$ R6 I# J( q; o9 d3 W4 C public Connection getConnection(){# ~' F) C: C& y
Connection con=null;
4 x& n7 G. g: P2 M" V try {
* j4 P+ X! M5 L7 X# ^ Class.forName(DRIVER);
$ w9 a/ R$ [ I$ I6 L5 `1 E$ Z con=DriverManager.getConnection(URL, DBNAME, DBPASS);5 y9 k! n- N' J" H" m8 b
} catch (ClassNotFoundException e) {
- Z! B; o5 s7 {. S5 G. k3 ^2 k System.out.println("加载驱动异常");
j: N! q! l o9 p e.printStackTrace();+ \0 k% s( A1 l4 f% z
} catch (SQLException e) {
9 L3 [: \6 {! ~: L System.out.println("数据库连接异常");
* X9 _ R5 F8 C- _& A0 ]3 M e.printStackTrace();
5 a3 P- J0 }- Q, [3 u }
- M: H& f; y4 J7 s8 ^* S6 n return con;* x0 Z2 e2 j' z' |. Z) z% T
}) b9 O; t5 G6 S# x+ ?( V; A
. A+ a/ O% c& o2 O6 u) z
public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
" I7 V( e% W/ ^5 [. q6 s) E try {; N- e, q1 I, j$ P% j4 B5 t: J. [+ S
rs.close();7 Q z5 y- d, E3 w' c' h
} catch (SQLException e) {6 g$ c6 w- e) r' c
System.out.println("");, G/ {' n2 D) Y; j K9 x2 l* s5 i
e.printStackTrace();
; {; k5 B2 v! ~0 V- f( E }
6 @; R# J" f5 b3 C6 K* W/ U. ~; p0 j4 X try {8 c5 Y4 F- q- o8 O" h. w
past.close();4 f4 {$ W2 ?) u( ? Q
} catch (SQLException e) {
6 X& A& M4 `2 u5 s3 G+ v System.out.println("");+ |8 c$ {( X2 S
e.printStackTrace();3 V" A A3 d) Q
}0 b# S/ s- q( ?( k2 V& w8 C
try {
" ?4 Y' G& J" N* M2 i8 Y con.close();
' f, F# }% x1 ?3 J& w' l/ L/ {+ D } catch (SQLException e) {
& [$ r l0 h3 p9 n6 F' \ // TODO Auto-generated catch block9 b( f: V# b4 r, P
e.printStackTrace();" P7 f' ?5 o& _# p4 [. L
}4 O. e8 k1 x" |
}
$ B' R+ b/ t6 O; V0 a$ f ) E; }: j' S5 g% }
public int update(String sql,String[] getValues){
1 c: L4 p0 A. i6 i4 q int i=0;
! Q- {+ W. Z# N Connection con=null;" D' B6 h, @, |4 ~/ U, @* n
PreparedStatement past=null;
% U) j$ J% Z/ f4 `! ] con=getConnection();9 \8 m4 s N* \# S7 k
try {9 W. U) E3 j9 ~9 I: ]
past=con.prepareStatement(sql);% [# P7 }( W9 v8 p9 w' v- D* _5 x2 j
if(getValues!=null){
4 B' I; B+ [6 }8 c for (int j = 0; j < getValues.length; j++) {
3 d& J; r; O4 |& x% s$ u8 B past.setString(j+1, getValues[j]);
m0 l8 D& E, k- T2 ?5 ^ }: S9 h# k d5 {# K5 q
}
. g, e; ]; X( @# v i=past.executeUpdate();
9 s5 _3 ?9 w# d$ s+ _" l- z- X3 h } catch (SQLException e) {
' ?5 Z9 ]1 m G% e0 @ // TODO Auto-generated catch block
) _5 P# l% m4 b e.printStackTrace();+ E) W! r6 M! c T
}+ Q- D- T* \# }; P3 l2 k
return i;% w% E3 X" V' E7 e
}
+ y1 d9 J! g* |4 y" E }$ ?$ p# E2 L! H1 C* f/ c
复制代码 package s2.jsp.zhangxiao.entity;
: D9 F* @+ I# V5 z' f$ t
/ S" d9 X2 w8 Z, j$ | import java.sql.Connection;
C) L; B5 M7 O. Z1 l import java.sql.PreparedStatement;
* w" Q. T* s0 C* m. Y8 @. k import java.sql.ResultSet;+ ]' c( }7 k) `. `$ W. @6 z w
import java.sql.SQLException;+ L$ A2 W3 u6 o: W
import java.util.ArrayList;
; x0 f2 y; ^7 B) v/ ~6 J* c, y import java.util.List;
r8 I: y3 s2 j% T5 m3 c5 f9 a / R, P; }: ]* \/ n6 g
import s2.jsp.zhangxiao.dao.News;
9 z: \! G+ o3 T2 a / f# q( f6 h! B3 G! \# |9 @
public class NewsDao extends NewsBase {9 S T: u' e" X6 u. o p
private int count =2;
% y. Z" R$ }/ s* G( Y 7 M% X7 ~' v. e$ I6 R: e
/** S8 y, o4 w% v3 h" Z4 m& _$ g" H
* 查询总页数
- g! x- n. F# T- A * @return2 q3 t" \! S1 f J
*/
0 ]# H5 P8 A1 v' p public int countPage(){; J. s- {: l. q8 M" m
int rows=0;
, d" b7 x* ?8 c& J. T: A: I9 R+ Z Connection con=null;
9 B) b. t0 T7 p5 m PreparedStatement past=null;
+ y: o/ y& j1 ?. F4 D" q) ` ResultSet rs=null;7 ~% f" J0 @* D( ]
con=getConnection();3 U1 G5 `/ h7 `7 J( S v
//查询所有的的页数 返回受影响的行数" n: m X0 O" `! @0 B& L
String sql="select count(*)as c from news"; m/ Y9 |! x" B7 @/ i- i
try {. M- Q9 t$ x& Z9 B2 l1 m: D7 W1 B
past=con.prepareStatement(sql);
9 h: ^1 k5 p1 J" j B rs=past.executeQuery();4 |# c' q! s1 H) N/ @
if(rs.next()){8 z( q/ q% f- u8 U, ]
//将所有的行数保存到rows 变量里
- N% s- I0 y" l. U/ [ M rows=rs.getInt("c");' C$ B1 W* p k2 z7 m% I
//如果显示两页 count 代表显示的页数
% A6 F8 J" y. P( n: B if(rows%2==0){
: _9 u$ D9 J$ E- m% R+ L3 j% D) z rows=rows/count;
9 \' X! N; W- t1 v4 r5 G6 w$ G6 ?9 S
+ A' d+ L, J" l. y7 h2 @ }else{6 u3 ~: A% ~1 b& Y+ |" }1 N( ? z
rows=rows/count+1;' D: L% s4 M( O) b+ X
}3 o' b5 w2 I9 U, s' C6 S
}$ K$ R/ k7 r3 g* e7 S `
} catch (SQLException e) {% J, x. S3 `! B9 C2 M/ _
System.out.println("查询总页数方法异常");+ w# t1 N$ ~: I, [+ ~9 z
e.printStackTrace();
8 y$ P8 ~) o8 z2 [9 F }
d1 p7 r0 F7 R1 x4 N) P" n return rows;
# F; Y1 X) S# f) u5 f8 R/ | }
2 W' h1 M1 l' u5 V& `' I$ ? /**
7 q- i! i9 z8 i/ ^+ j! z * 根据id删除信息
8 [0 v ~3 L' ?5 x8 @ * @param id: ~( U! R6 q2 K# S" X
* @return
m- l* \3 N: H1 ^7 H& y */
9 G* \, \: }& H x public int deleteNewsId(int id){
6 s1 u4 b8 r: w% E/ s/ z b String sql="delete news where id="+id;
# H8 m7 j1 E% y return update(sql,null);
. U) K& \) ?7 U) z7 Q5 q }
: S, s; E. E% n/ a /**
8 _; A3 w5 x" m3 M( e' b$ c; w * 根据总页数分页
8 i: J7 ^4 f$ {) ? * @param page
4 g a- S( |1 K& C# b* G( @ * @return' i, [; N: Z" z: K+ _! x" E1 o
*/
. X, g5 o" [. ^4 q5 J public List getPageList(int page){* a2 z) _. P, t4 B& n; B3 [
Connection con=null;! d/ m- S4 m4 C: Z2 Y( D* z. F1 N
PreparedStatement past=null;. `7 y! e* X9 Z
ResultSet rs=null;- v! P8 u# w% r' d8 K9 l
List list=new ArrayList();
) s5 d( t7 c& W% Q con=getConnection();
; ?* |0 P# m! M //
* M7 s2 K' V& N+ @4 B 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";
8 T2 y# T2 D0 k" Z( j try {0 w! I! i: w% S Z) Y
past=con.prepareStatement(sql);
( t6 ?* A7 f: _' b rs=past.executeQuery();9 r( v, B% v' c3 u1 r( V6 {
while(rs.next()){
3 b v0 }# h7 j5 |6 q; j . b( ?4 X! w+ a
// id, title, content, pubtime, author
8 c7 _- C3 J: L1 ^ U: A6 l, t# W int id=rs.getInt("id");
/ i" s( C, S; k0 i8 O1 k String title=rs.getString("title");
+ V) j) S8 Q4 p; z1 Q$ D0 Q String content=rs.getString("content");
1 m1 w8 X& N- ]; @1 k6 w String pubtime=rs.getString("pubtime");
2 q0 C# h# h/ b+ e+ K2 H' Q String author=rs.getString("author");1 [$ L, D/ x& v* @5 I) V, U
News news=new News(id, title,content,pubtime,author);9 e M6 ]& W, v8 v3 `
list.add(news);
4 a y. Y1 d1 l8 a0 ? }; d7 s& p# Y n* N' n- p# j% x
} catch (SQLException e) {3 X& T6 U+ [2 O9 U1 a2 R
// TODO Auto-generated catch block* |! P) D9 t. m4 }% z4 ]& I' O
e.printStackTrace();- ^6 D- r( B; c
}
: \! j, e* _% b: [, B return list;# N% {, o# T6 x5 c
) C/ Z, ~6 F# p b }
+ T9 t) s6 e# a% s, d! Z }
& a ?3 j# y9 Q2 V, n9 T 复制代码
* c- S" J; I$ n6 {8 d% K B
$ j; h: l$ ~0 O. `" F 源代码以及数据库下载 :
$ ^$ y% {& k3 Q2 j5 h" \ 7 Q+ D7 O& H! B2 q/ l' Y
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :