TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;
/ f, }0 h, T' u% h- o `* l# L /**" H. _6 \! x2 j5 M) \( R/ p) J
* 实体类8 z; _8 W0 k8 Z) }
* @author Administrator
7 f% j( S# f. @8 l% x *, v7 x" {& C, Q
*/
8 x0 r. A' Y+ ^ public class News {
1 X9 R! H8 @2 k; M. W/ K9 x6 A // id, title, content, pubtime, author
: f+ j; A O$ c- E5 |4 m: g2 t8 u private int id;
* ?0 K5 M; o# m1 T private String title;
& Z+ b# f9 C3 T) j2 j7 Q* \ private String content;% L0 a$ j, W* ~2 t& a: w3 f$ f
private String pubtime;- F+ O: Q6 e1 U, x. {+ x& V- G: L
private String author;7 ^1 _! X3 b; i/ ^4 I! I
/**. f% O8 F7 N/ b& [4 \
* 构造函数
. d. f) n7 B2 e2 d o: ]/ L) y * @param author 作者
4 m i. r# U+ M: e0 ~1 a * @param content 内容$ M+ p8 ]2 z- L/ w: B
* @param id 序号
- ~$ ~9 e) M! M9 @7 a0 F3 S: A * @param pubtime 发布时间- ^" ^+ K7 x2 Q5 l: q% [$ ^4 H+ @
* @param title 标题( H/ b* K& S5 S2 i; d
*/
( r8 o9 V6 E6 Y1 F8 H% e0 K, a" H public News( int id,String title,String content, String pubtime,String author) {5 |/ |$ o' f* T) W
i/ j% ~- w. Y; F
this.author = author;8 s# \, J2 A. G# a( t6 p3 O. _" L$ A8 N
this.content = content;
8 @3 u" K! t3 l3 z9 p1 s this.id = id;
% u' w. p; }6 G( A" @" Y this.pubtime = pubtime;
. O( e) d! Q+ r6 b0 w+ @6 I# o3 j this.title = title;
/ s" p s% S! B) i }# t7 v9 {2 _0 D) X/ U
/**% a$ S$ \. [5 X* b
* 无参构造函数+ Y* O: _: [% J0 d
*/
) g6 ]; ~- Z/ q4 `4 `- F4 [ public News() {
W$ S+ O) {2 ~7 Q+ b super();
8 @$ @ K% i5 `0 o ? }" I8 ^0 ^6 V* X9 u' q* }+ F! q3 W
/**
- o' A& e3 s; J( l) w, t * get ,set 3 e7 K, u# J; I8 [2 ^; G1 @; g! }6 T
* @return
) \" _+ ]; H% M# b8 U# f7 \ */0 Y2 U3 L2 s/ R3 p* B0 h6 E( \
public int getId() {
9 ~9 a: ]/ i( Z e return id;4 M& n( @' d- v! j6 W5 L
}
$ u% d! ^( i7 V: w& M1 m: { public void setId(int id) {9 A% c6 g, G0 o5 S6 l1 G
this.id = id;: K3 X+ c7 l$ v+ f
}
2 v- y) r* Q+ I5 b$ ]6 _- d& S- j public String getTitle() {
- T) ]! s2 T) _; y" G: M8 T+ }# B return title;
?( Q* V" v0 L' E/ j; X }% A: _8 Y* e& {0 J/ d0 j, O+ b
public void setTitle(String title) {3 M/ V7 Z* f* ?9 E! ^( O0 D
this.title = title;6 A& S0 y: n. n6 y* J
}
3 R( {3 h. z9 _( U. J- _; H1 F public String getContent() {
3 i- _: J% A) R* @/ j6 l; A, K% w return content;1 C) U. @+ W3 j8 F1 p; F- R
}) @0 E5 J Q8 Y
public void setContent(String content) {+ h$ ~# |/ M F/ C% q/ t
this.content = content;
, T% q8 T- a; v! h6 t$ D3 e }
& y( k0 v; |- U. X( Q public String getPubtime() {
. d2 m4 T/ q: T, Y' a" Z" _+ ~ return pubtime;
- @- m0 h& O' K9 ^ }, H1 g: w/ |; l, P
public void setPubtime(String pubtime) {
% E0 w, p1 {' y. ?8 | this.pubtime = pubtime;2 w/ w: i2 V" D( o# q
}
" f& ] |4 I% c! d public String getAuthor() {' v5 T7 I6 k4 x- ]
return author;
; c( R# J; A) ^; R% C* e }
8 u7 B: ^' b5 `0 H) O$ l" N2 f public void setAuthor(String author) {
; E+ q4 Z" J3 ?* O0 C4 m2 u this.author = author;
+ ?" E3 |! N2 U }
/ q; @ L4 w: B2 V $ k6 W: x6 J8 Y; X7 c! X
}/ f! q! f5 t4 y/ A4 M; {$ e; S
复制代码 package s2.jsp.zhangxiao.entity;
" N6 |6 ? D0 k3 Q! S import java .sql.PreparedStatement;2 X7 ~9 \5 W% ]3 e! A+ J) [+ ]
import java.sql.DriverManager;7 q7 Z4 g3 d# ~7 J( Y# r. r$ Z; W
import java.sql.Connection;
! I2 a/ L0 m6 T8 ?3 Q import java.sql.ResultSet;
; r4 g, ]& L) R) T" g7 M' j. j! V/ S' s import java.sql.SQLException;) j: W J) o3 S
) e- D# i- k, e' b import com.microsoft.sqlserver.jdbc.SQLServerDriver;
* D. Q- f. ?; {) p1 n* @
/ y6 b" y* ?) v* T1 J' ] public class NewsBase {
) N- h( S. [3 L& r# ?: b9 q - H: S$ G8 r c9 V- d
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";" p" n0 q8 v/ N% n" {; f; a5 ^
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";6 }" y/ C1 ]/ M+ K3 ~4 ]
private static final String DBNAME="sa";* v2 H% u7 z: @ s* r$ g
private static final String DBPASS="1234";, L) n7 Q7 x. C4 ] D6 }9 n
) W" N7 B8 F3 K: K public Connection getConnection(){8 C" @, @2 F1 R! D% J: O5 T
Connection con=null;
/ i) f8 G! f$ T( L: J6 L try {; x3 L5 ]" _. G& T$ t
Class.forName(DRIVER);
, f* ~# o- E j+ |3 p$ ^( J con=DriverManager.getConnection(URL, DBNAME, DBPASS);
6 x; D* z4 w4 q6 c, r- [! |* Y4 b } catch (ClassNotFoundException e) {
8 |' X$ s6 D. E- ?5 H System.out.println("加载驱动异常");) _$ q5 f) p l% V( W) Q0 a
e.printStackTrace();
& M/ _" i5 u& S# l! P N# q8 b } catch (SQLException e) {
) w( v4 Z1 \6 ~# l System.out.println("数据库连接异常");% e: W" L# s& T: H
e.printStackTrace();
, U6 X3 L% A0 r. A }
- B5 z$ F+ J( h/ J return con;
5 ? |% C$ W4 ~& M }% `6 F" ^, ~- j6 a, ], Q
7 C8 i' |! H8 t: v# f# K
public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
( `* C. G( j% ^0 B+ O, t try {2 k3 s9 X0 l4 Q5 m* h
rs.close();+ O8 j3 J8 K% a1 D) ^- o6 [5 f
} catch (SQLException e) {3 n1 x3 p- P! u
System.out.println("");" w* \3 a* O- L" c8 e* O
e.printStackTrace();
7 Y# _; c4 x* Y9 q# m* J% W" s }
4 F8 l( X* F+ Y1 P5 o; d% [ try {$ M# S0 [# O; O- V5 \5 d5 R! ?
past.close();
; J& r6 ], v$ W3 s. k } catch (SQLException e) {
$ k0 ~) U( p: D2 O7 i, F System.out.println("");
/ E; M4 }2 x4 V, V e.printStackTrace();
0 o% b7 }: Q, }+ [7 D: \ }5 q+ s6 c. Z* [- c
try {
6 t B: X+ c) y; h% Z con.close();* L6 _0 z! r( o. T7 P
} catch (SQLException e) {
8 P3 m0 b. C. |! d6 a+ a6 c6 M // TODO Auto-generated catch block
+ X, Z4 e+ M6 |7 O e.printStackTrace();
5 ^% g' I6 ^5 i }
% J7 J* G5 ?' D- j8 ~5 } }
: d' M5 @. _5 V4 m2 M 0 t$ e3 _, v- ^$ J
public int update(String sql,String[] getValues){
9 x/ N! r3 X8 a/ v1 P' B int i=0;
Q) N( U. K. U! V2 x/ f Connection con=null;
6 n3 s6 R2 q$ ]) P0 f$ V PreparedStatement past=null;
" r& F+ K) z$ D5 [2 W! \# Y con=getConnection();
0 \* u; @. t# ]5 l3 I6 Z try {
, z$ m2 ~7 [" e past=con.prepareStatement(sql);0 ?3 k. w1 T4 n% M2 e
if(getValues!=null){+ |7 d ]. L% N: X' f% I
for (int j = 0; j < getValues.length; j++) {& v: k2 d g0 Q" ~8 B4 o& C, v3 I
past.setString(j+1, getValues[j]);& i* b. s3 ?+ F8 G8 p' O
}. Y- g1 [- n: A* b2 i, e" W
}2 k y( d0 p3 n+ r/ d) e
i=past.executeUpdate();, ]* p( }2 b( c% @* V
} catch (SQLException e) {
+ _% C* o( q: U* Y* @ // TODO Auto-generated catch block
+ z4 H( {9 a& X I e.printStackTrace(); Z: Y2 O. }) d( }6 @% q8 R
}
8 t5 F8 j& V4 w6 |3 d' x0 v) F return i;
! d; F9 D/ i8 q3 x7 N$ _& W% c8 M }2 o% K: Y9 i! M" u* |
}
) s* [7 S. D6 l$ x' S 复制代码 package s2.jsp.zhangxiao.entity;
P& A6 e% @' c Y5 K% c6 ~5 I0 I
, n$ l! [& y# Z3 p. Z& @! y import java.sql.Connection;
! c, A8 n0 E/ {9 o import java.sql.PreparedStatement;% O& O) G3 u# O) E8 L4 n
import java.sql.ResultSet;
; b) ^0 E$ K- ?( E6 k" x, T import java.sql.SQLException;
; [$ T Y9 b A0 p" |8 _ import java.util.ArrayList;
$ `$ m9 Y* [) t) i+ E q% g import java.util.List;7 u" h/ ~6 o1 G: p* W2 U
: {; u( f2 I& g9 W& ^ import s2.jsp.zhangxiao.dao.News;
9 l( [' t/ @+ L9 u0 C, G8 K3 G
V1 C. T3 v. h public class NewsDao extends NewsBase {" S$ v8 f+ U3 `1 n; ~+ O# [
private int count =2;
# }8 q( [" k- S8 D4 U ) h! M, q0 V8 C, K- C* e5 Y
/**+ o; T9 W1 h8 y& W ]& X: A
* 查询总页数! ?5 b& I; c5 \. {4 e
* @return4 J' t, D; U/ Z
*/
2 a5 I$ B' C( n9 g public int countPage(){
2 W" X2 m4 }" `! p# G3 }, j. l int rows=0;+ i( e: E" R1 d+ M
Connection con=null;
) a) {# M3 Q2 U PreparedStatement past=null;: ]9 e. i9 `+ X: X# G' P! u" e0 |
ResultSet rs=null;
2 S- Y; V( c9 T J con=getConnection();
3 {" B2 d6 D' J9 \/ J- i4 T //查询所有的的页数 返回受影响的行数 l$ R5 G8 Q( B$ H' u1 G5 S. n
String sql="select count(*)as c from news";( h: Y9 A) X! l6 b% ~
try {
3 ^$ F" P7 s V7 `4 o past=con.prepareStatement(sql);
3 L# Y/ P1 H; E2 P& _- H1 X ^7 p rs=past.executeQuery();: `% Q+ h1 l! x/ N2 f2 {/ ~5 |/ s) _: t
if(rs.next()){
! m0 l; P' m9 k, t0 Y! e% L //将所有的行数保存到rows 变量里
- J, u+ D7 ~3 z) h m' a rows=rs.getInt("c");! E7 [$ q7 C' _- {
//如果显示两页 count 代表显示的页数
) l+ F% a4 ^ u, ]0 K6 ^- P4 } if(rows%2==0){
' D N1 L0 ?. y7 X rows=rows/count;3 _* G& V4 }' K. v
% p1 J* {/ R, I( X& y* R: I }else{8 J) f. d* ?2 _
rows=rows/count+1;
8 z- W# M5 ~. ^! ~" s- Y( f }' `$ L% G& Z1 F2 z3 s* w$ a7 m+ @
} w- X1 z, t. @
} catch (SQLException e) {
( u2 z! H+ Z7 C% s* o5 t System.out.println("查询总页数方法异常");2 {7 k" g) ~- u5 v5 l( i8 \6 k0 k: Y4 N
e.printStackTrace();/ ]" }* t6 e3 U! D6 u
}
3 o z x$ s" M: ^% Q return rows;+ M \# O+ ~$ U% v% t
}
$ H3 T {! H/ S /**2 Q7 V; g$ Q& |& X: ?0 l
* 根据id删除信息
" v/ ], T- N7 n" H9 l: o: Z * @param id! Z F9 ]* j6 a) n% ] B
* @return
/ {! Z, A* @( @$ M */' N. t6 g3 D4 D5 |
public int deleteNewsId(int id){
3 l+ ^/ U# n+ Z String sql="delete news where id="+id;
' }% L% i( h* ^, e) u+ w- N4 E return update(sql,null);
1 |% z/ f" `5 R4 a2 g. q1 v* k; l }' E; I' P f7 i1 n
/*** _( `& H& i/ w( `/ h) c T4 i3 O
* 根据总页数分页! E8 E. b. f, Y8 f
* @param page
0 U/ w. `5 {9 ]. C9 u5 q * @return I! p ~* \* w. I, `: s
*/
Y! M* o( v6 I public List getPageList(int page){5 e7 ~ u( H( c. n4 w
Connection con=null;$ ~: o. U: W0 R a+ T7 @) O
PreparedStatement past=null;7 J0 i, V( P6 H1 {% H
ResultSet rs=null;: _, X2 i" }' n5 X
List list=new ArrayList(); a" W4 l5 O& m$ t# N& a! l/ @
con=getConnection();
# S3 {5 h- y7 g1 P //$ O7 e" d K( f4 B2 T
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 X8 {. d5 n; q& E2 ^4 N' X
try {
* H) C) e0 |5 V7 } past=con.prepareStatement(sql);
. [# I! t2 I% R1 C4 v# ^% w rs=past.executeQuery();
* F; p+ m; i- S3 Q9 \ while(rs.next()){
0 B0 y" N( m# W X/ i0 [ , @1 P' d8 F, G, ]' l
// id, title, content, pubtime, author1 k5 {. C5 |" `, Y
int id=rs.getInt("id");9 o& O4 w3 m- b
String title=rs.getString("title");; K! S" D# ^1 L q9 b: y$ \# r1 D2 O
String content=rs.getString("content");" K0 U% i* H5 I, t# o, x/ v
String pubtime=rs.getString("pubtime");
% g( `' ?5 r6 c String author=rs.getString("author");
v7 R: ?. U" H. ?# P7 ? News news=new News(id, title,content,pubtime,author);5 t# E: ]6 n# a$ w1 }
list.add(news);8 R! x# ]" S7 F! y
}
* r6 J0 T$ B) v9 @! v/ L l9 U } catch (SQLException e) {
d1 V9 c& r: f // TODO Auto-generated catch block- l/ V4 I! @1 L8 v/ h: {. |
e.printStackTrace();) N( J4 F7 @& u: X
}
3 e% R2 V& o R; _ return list;
1 y) _) ?0 R% j1 a
& Q( ?7 c: V. y/ H; w }' F# c3 j) M/ ^) H# O [
}% \" O0 q% m" z% C% d% m
复制代码 * e; m4 ]% g7 S4 Z G8 W
) C/ T' e% w2 E 源代码以及数据库下载 : / W( p" R, q+ P: q f) U3 j& O. K' o
0 V( q. r1 p8 C, x$ w" P
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :