TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;% \, W4 M/ e/ J0 u0 O: i. {
/**
8 F# s* {3 m% Q j- r7 I * 实体类
/ a/ B* R4 z2 O8 }1 z& i * @author Administrator
/ Q( Q0 s% v- b *% K9 P7 \& p7 w6 |( S* M; r
*/
f/ b7 v5 B6 P1 ^9 F public class News {
+ y# u8 j% g: R( I, F // id, title, content, pubtime, author
+ U7 X( _/ E3 K private int id;* x* T: B2 ~* h* b, G+ {0 ?
private String title;
. n( e% y( S5 _ b4 n/ P, U `+ a+ b private String content;
+ a6 ?" g \( S' F, f. V* P private String pubtime;
) Z) v5 {6 b- n, ]% } private String author;& L. Q6 }% M5 T0 |- a7 `% q* p
/**7 N5 i& S: l2 l8 q
* 构造函数# F- O- \" H3 k4 ?1 j. R1 C p
* @param author 作者6 T' }! l# q% A5 b5 n
* @param content 内容
* j ~4 ~- n$ g F+ a& A7 r2 S6 G * @param id 序号5 [% W# b% f8 U1 L& E9 G" T( M
* @param pubtime 发布时间
- I) ~% l8 q1 F! q/ J * @param title 标题/ X0 k( o, d# x, y+ e; N q+ ~) k
*/
6 j! A I6 O- c _! I2 i9 `) k public News( int id,String title,String content, String pubtime,String author) {* Q1 ]6 K% y! z7 r& v2 T7 J2 D6 W
9 c# H+ J/ }7 \" w5 Z
this.author = author;$ P. v3 H/ Z% [% ~# Z
this.content = content;
# Y( b, H7 r8 { {, Q/ X, X* U this.id = id;7 z6 \# Y) y& x% ?
this.pubtime = pubtime;
* ?/ D; M1 h8 R this.title = title;
& P: a* s, o- B+ t# b$ ` }2 s, }. t0 O" H ?$ z" Q0 z
/**
+ U% s. ^) Q. p6 w5 I * 无参构造函数
' f Z6 z. ^8 G* m1 Q */+ j1 O9 b$ O" n
public News() {
$ K3 i7 j0 m+ |! ?5 N( p super();
0 l" f; x. l/ K. I }1 m3 Q, j, N2 r+ a
/**' }' e1 V% I+ C: i* U" H
* get ,set 5 @0 c5 m" G8 A/ G
* @return6 P! m4 C- j4 ~0 i- M2 D, d+ i
*/
! E6 ^" j- L4 N6 {0 L; K public int getId() {! z; z( y. n& P9 v7 U
return id;1 t' \7 @# V2 m% z% v' r0 n) g: k3 d# A
}
# I) I! A, s$ g$ u& l K4 o public void setId(int id) {
7 Q: [: L1 _+ T0 Y this.id = id;7 w& B9 k" `( ~5 o
}. {) }( B; i A0 p; U/ h+ ^
public String getTitle() {# m8 G1 f% }4 I8 `# H
return title;
# f% ^5 X' J5 N }
Z& W/ A4 D+ j4 l/ W public void setTitle(String title) {
$ b" A( I6 g8 U8 \ this.title = title;
, Y+ v3 F$ _6 n0 o$ x }
0 I: W9 s5 q" e- J% W public String getContent() {
# o/ k, ?% V6 ^0 I# i* C3 n* M: ?5 M% _% v return content;
. i; P n( i$ Q9 z# j, z }% g( W; X1 p# p- L% {) k$ }. ^
public void setContent(String content) {( h% s6 ^* a9 A" C, J3 l& T! C
this.content = content;
. O1 m, p, O+ o, p) f8 s2 Y% b4 I( \ }/ s# _! C6 V3 q; z
public String getPubtime() {
2 y! c3 z5 P9 ^# X return pubtime;# ~6 f) a. k( i9 d
}4 `! ?+ e1 [3 P9 |. ]' R
public void setPubtime(String pubtime) {9 Z. \) p0 C" Y Y5 b. z& r8 `
this.pubtime = pubtime;
' B- I) Q w5 @ ~( d }
8 Z7 Y* I* B; j) w8 n; ?/ q) N public String getAuthor() {: j+ p9 s$ Y8 q7 p7 F
return author;
|- c2 g q" `8 i9 y0 r }
' ]# f4 Q% _5 H0 K public void setAuthor(String author) {+ \2 X2 ^2 g0 D3 o) W% E
this.author = author;# x9 V* C$ |$ U/ C
}
' c- d. p+ ^/ C( B6 ^% E
/ e0 ^9 I0 @& H9 j3 y2 J }
2 C8 ]: i) c% U; d/ m. T" j/ C 复制代码 package s2.jsp.zhangxiao.entity;* D# u" U/ x! ^
import java .sql.PreparedStatement;! \# d! D9 M& u: [# O! t) @3 B
import java.sql.DriverManager;6 q4 P2 z9 I( y q0 [+ s. h g' s+ x
import java.sql.Connection;$ N4 q+ ?# ?3 [( ], P% Q# n' Y7 D
import java.sql.ResultSet;* ^% m! [7 S; i; b* k
import java.sql.SQLException;8 v4 ^/ l9 H% Y# }9 t* L1 r& X+ `
% w- N D+ `. |' J8 i3 H
import com.microsoft.sqlserver.jdbc.SQLServerDriver;) }; y9 x& W$ I0 w5 K# K
4 ?/ _' i+ K6 u- }, N. k public class NewsBase {) F J% j. [0 G3 L C
; M1 D! E8 h% E9 B: K private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";# l4 Y3 w) |% W# D9 m, X e1 r
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";
; x* A+ X7 H/ J R! H private static final String DBNAME="sa";
' H4 C# a6 ]6 b/ R" V$ U+ |7 i7 b private static final String DBPASS="1234";; n# t( j& ^5 b3 ~
$ ?! Z( |7 |3 Q public Connection getConnection(){
1 `& m7 t9 X$ N$ i- J Connection con=null;) M- g; v* m" n% N) w' W' s0 M
try {
# h% J) l3 m r) @ Class.forName(DRIVER);
& x& ?$ D8 x3 A1 y* { con=DriverManager.getConnection(URL, DBNAME, DBPASS); V/ g/ D/ O/ o/ K. e0 K
} catch (ClassNotFoundException e) {
# s! M* Q$ w; @9 f1 u System.out.println("加载驱动异常");; }/ ~0 x2 K! J" e+ W: C9 S5 }
e.printStackTrace();
2 F+ _: W" t9 b, Y K: Y/ Z3 M } catch (SQLException e) {3 o2 H) j$ R5 |. y ~5 n( U
System.out.println("数据库连接异常");
: g9 t+ o$ P9 K8 D e.printStackTrace();! a/ O \1 |0 d( l/ K9 o7 A
}9 S$ p; ?* t& y% I% E5 _0 c
return con;
; |$ n0 o4 ~4 f$ N* @- Z$ s& j }
h0 W: f9 I1 W6 ?: {
6 S @" c9 M& z public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
0 L, C( @' @7 _0 F& y try {( ?4 F; u4 p1 {; e
rs.close();
0 d. [2 A9 ~3 d6 l E2 j- }6 X } catch (SQLException e) {) H; {9 |& V% e/ F4 s
System.out.println("");
4 L& A/ s" C8 f: Q6 ~ e.printStackTrace();( L& ~9 d# c# K( O/ i# p
}" S: r" Y( Q- r& B) }
try {
4 c& w3 a: C4 y- x9 T" L" a, F past.close();
0 E. s! K% H: h# w$ S( }: P- D } catch (SQLException e) {
# z9 |. g- M# n$ H! ` System.out.println("");4 N* Q V l. V" s4 h6 c6 }
e.printStackTrace();
l) W, w& b5 G( N- t }# E! x+ Z0 f. C* Q+ J, T# _( V
try {, V- w0 ^ p3 ~* e* {3 r o
con.close();
. t) T- k& l; a$ P/ r4 k } catch (SQLException e) {
& j" d5 L% B' X m* \" T // TODO Auto-generated catch block% ^9 }7 e' H1 R2 \5 {
e.printStackTrace();9 ?4 R* L2 k4 H. p
}
, d; N1 z7 d( z. p- b }& c2 c( v+ }6 H+ U, m
. v5 W. A" S$ Z7 t) {
public int update(String sql,String[] getValues){
- T- A6 H9 @4 a2 u- ?* o int i=0;
0 |5 V: |2 X8 K/ c7 b5 N Connection con=null;; h6 C, i2 ~' ~7 _, S1 t) I: {
PreparedStatement past=null;
. d; n# ] q: t" ~0 e9 C7 F con=getConnection();
/ ^- U ]" D8 h0 d' M( b1 ? try {9 ~" l, \7 u% J
past=con.prepareStatement(sql);) c/ Z4 y# E+ ~
if(getValues!=null){2 d$ P6 W* P& O4 [
for (int j = 0; j < getValues.length; j++) {
2 i& U; n/ x a$ k4 b/ i past.setString(j+1, getValues[j]);. I& R7 O( `9 D8 B7 i
}* F3 v9 c6 U0 [5 s6 E
}
. j4 W9 ~2 B' } i=past.executeUpdate();1 I1 h7 J! k& M( t: I
} catch (SQLException e) {
. |+ h# [/ m& b5 F% c // TODO Auto-generated catch block
% k" C. h) A4 Q5 F1 x7 l8 d e.printStackTrace();
" r( R O c& d }4 \! t. L/ v; W9 }- R) @
return i;# E$ E' G& N {: p7 q$ z2 s. @
}8 j& t. z3 X9 z2 d) W# S
}
1 V; `) U/ e* s: _' E/ ^9 Y 复制代码 package s2.jsp.zhangxiao.entity;
2 h. Z) p& e- Y, W* W 8 u3 D) l1 x0 R# B" j( U) i
import java.sql.Connection;. B) H* `: w5 f" _+ _+ S0 {+ C
import java.sql.PreparedStatement;
6 C* z* `3 ^# @; C/ x# H1 S6 m( O import java.sql.ResultSet;5 _% `, R% p1 ]( N, q
import java.sql.SQLException;, B( N; ~$ _8 M
import java.util.ArrayList;
! t+ ]2 E5 s9 [% \ import java.util.List;
& h+ c# g- V( K- d. P. p) K . i" V8 R6 d4 r# T6 e7 {( i
import s2.jsp.zhangxiao.dao.News;$ h3 C3 a z: Z
9 Z$ p( Z* D5 P- M, e) t" n
public class NewsDao extends NewsBase {
" y5 S0 s' d5 x7 K1 i: N private int count =2;
. r0 r: `2 n# O2 h$ U
) g: R/ X @3 x8 _ /**
- O3 C w/ c' a! Q+ p+ P * 查询总页数
J; S0 t+ w/ V6 B* o * @return1 t& T+ c7 F( i$ _" R
*/
% E/ n$ X, D# [6 q& g3 ~ ] public int countPage(){# B/ B( ]5 C( [. h7 D; u3 O5 U
int rows=0;
9 \. D6 j& p* y3 j Connection con=null;
* d; k9 \( o- E# b F/ S0 ? PreparedStatement past=null;. o2 U5 |+ _2 Z
ResultSet rs=null;$ P, e' f7 i$ n! `: r" Y7 i" k
con=getConnection();# o9 t6 \/ l; Q$ _
//查询所有的的页数 返回受影响的行数
+ Q1 n* l; s+ U% B* ^$ }* r String sql="select count(*)as c from news";- i8 J" U& }) [9 \" s& b. P
try {; E' I& K9 ?3 }6 s f
past=con.prepareStatement(sql);2 R+ M" e' |& D! y/ ~0 B; z
rs=past.executeQuery();
7 c, c, f7 `* t+ x% s. j if(rs.next()){
6 w! @4 w7 ]+ r9 F //将所有的行数保存到rows 变量里
6 V h" j! s5 y- E( z3 I0 n rows=rs.getInt("c");# A5 y# r k& d d T
//如果显示两页 count 代表显示的页数
3 N3 M) o3 H4 H if(rows%2==0){
7 m1 u6 B% r! X) `3 {( }' ? rows=rows/count;! T! A+ n& W! B5 C0 ^
) A J8 P) m' ?- a }else{+ I+ ?" L2 P! n- z9 f
rows=rows/count+1;
6 N+ x6 w9 }/ W6 G$ l2 m# g' H" t }: k7 z4 F1 E9 f. n c
}
6 {5 g. e7 t0 _/ v+ a } catch (SQLException e) {
+ {; k& ^' g# G3 B' k3 R4 f6 h System.out.println("查询总页数方法异常");
" Z6 z& |, W- J- h& \) {! g& I e.printStackTrace();
7 \! R& i- _; I7 [. ^! g9 y* x7 W }
; _, G, l2 r" u, U0 N: C" F/ D2 F return rows;/ N- g1 c u. o: L
}+ m1 i! Y: p! y9 ^. k6 j3 Y& e
/**
/ i! d& Q, E9 z W * 根据id删除信息
+ p3 L. B( C( ^9 H * @param id- n/ i& j- s% y$ y; L _
* @return5 p$ g3 Z/ W- j( m x
*/ ~/ W% K4 \3 V' t- W8 O0 [
public int deleteNewsId(int id){7 F+ p% e( u: _( t! \/ Z& R3 R
String sql="delete news where id="+id;' o% v0 M9 b2 x, z$ ^% T
return update(sql,null);
! v- C8 J0 ^7 d( a" j U) q8 R% D% x }! ~7 H% N6 t5 n
/**
; X6 C# W1 n( {' Z6 z * 根据总页数分页/ Y* e* D# ]/ h2 P; \4 k0 ?
* @param page
z. }* k+ N" {! |* u. i3 a * @return
U* |4 z9 E( g0 ~8 Y0 q */
- d% m4 R# a/ B public List getPageList(int page){
2 |# m) r! k) [3 w) V2 \ Connection con=null;
4 K1 `- J4 R' T- M/ g" \ PreparedStatement past=null;
4 N& x; l7 Z: a r ResultSet rs=null;
5 l' y+ ^! P' |3 R, s/ t+ ^) j1 W List list=new ArrayList();
, j6 e! w! @3 Y( S" U con=getConnection();. R5 M9 w- h' c5 _9 Z, U& m' }
/// v" S2 r0 d& N, }" ?; B# t9 `0 c
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";
# e6 o1 x- B1 _ try {8 C! n! O; e3 W; C
past=con.prepareStatement(sql);. S5 g- U" n# g H
rs=past.executeQuery();9 m& N+ V* d. z' U2 |) b7 v! P4 ^
while(rs.next()){4 I! q& E8 G8 y2 T/ q4 v2 n
$ a- V. W9 k" j; Y7 m
// id, title, content, pubtime, author
; l" V4 O m; u/ z int id=rs.getInt("id");
2 P" b7 z, D2 \& i* k3 K String title=rs.getString("title");
" _4 v& G* u& i1 R+ N( Y7 {6 J& t String content=rs.getString("content");
4 R+ J7 V1 x; J String pubtime=rs.getString("pubtime");6 k+ S* l$ A) J0 b, z
String author=rs.getString("author");+ e; p& F3 y! X2 a* ?
News news=new News(id, title,content,pubtime,author);# x0 C+ D+ Q0 L1 ]0 A5 k
list.add(news);: ~6 E1 u s9 w/ M. S# D: ~8 c
}
: [0 Q4 u% J; ?; ]& K7 W" ] } catch (SQLException e) {
! C, c% }3 O* n // TODO Auto-generated catch block
) |. H3 ]/ T8 E5 @" Q e.printStackTrace();
" ?! h. ^. R! J* t- w; E, C }5 v* h7 q2 Y# B; }! U" B
return list;
1 p0 t, d( i Y( W
; Y# y& Z7 D) _9 A2 _ }* B! ^9 `) A/ j# A4 F( X
}
. y% P) l: R6 @$ m 复制代码
( E' H5 b8 j9 e. ?! e$ C+ ^* a" A
5 A/ L6 x3 g. o$ F) M2 ?, x 源代码以及数据库下载 :
) N' x: r5 X* O5 F/ r9 y8 G/ J
0 I( H0 k5 `# \2 j# G- d! Q
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :