TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;0 w0 c) A2 g* f- a0 `
/**
( _& l% Y, A. w% A * 实体类
0 |1 m( S" U: P: } J * @author Administrator3 C3 U& T- J( M7 l7 o$ A
*
) o' @# a. l: ] */# k- b& r! u7 I: L' O
public class News {" P' ~- G5 K- X' Q
// id, title, content, pubtime, author5 D' A; g7 V' e [6 ?
private int id;
% s: z+ K! f$ f/ D0 s private String title;
' w# ~3 |( b6 p private String content;7 z1 I8 i8 M8 l. W; y& X) h
private String pubtime;+ k. U; r. z, N5 l/ P4 j
private String author;
; |9 b/ R- Y, ^9 q, U, ~( h+ S. x1 b /**) T% ^, u% F2 x% J1 v: u7 @! E* v3 [6 W
* 构造函数, X6 p9 x7 \, B0 o' h0 C3 U9 C4 L
* @param author 作者- k9 o7 ^% |% F3 O% I. j& v
* @param content 内容$ y6 R- z$ v! Y! W( Y
* @param id 序号
2 v' P P, y1 _ J; v * @param pubtime 发布时间
" y! N! `' o, o1 L4 \7 X7 r4 p4 i * @param title 标题
/ u* n8 L( a. b! v9 I */7 W" |: d+ f" J% P2 U
public News( int id,String title,String content, String pubtime,String author) {" K _9 \+ a, K5 U$ ]! L9 j7 ~( p
3 q% V" E' ^: u( V) X7 E7 p4 o9 _
this.author = author;
, P7 W+ R3 r' K1 C this.content = content;( ^" x3 h a' \1 \( q
this.id = id;. O2 p( G' H. V, s" K5 E
this.pubtime = pubtime;' L* Y- b R8 p0 B, f E6 H
this.title = title;( o% ^) @# T4 J( q6 Z
}
% E* n6 A) e% U' L /**0 O8 s" a8 u, Y
* 无参构造函数
/ T e' t a1 S, U8 s1 V */
8 n5 c; Y) Z( Y public News() {( y' D! ?3 B9 C
super();
- N4 V2 \, t3 u0 [% v }8 q2 F+ C _5 l2 C! Y' h) _3 \
/**
$ Z( t3 m* Q& P4 V& S * get ,set
! m" c9 y( w) r; e * @return
( _1 Z7 b8 i/ {6 U2 V */$ ^0 H; l' C6 O% t! @+ q
public int getId() {
, H+ j( X8 O0 ^, o return id;
4 h+ V$ O" C7 n3 F( Q" i }
; ^# C; X$ l+ U# P; T+ d public void setId(int id) {
; F! [; h0 g ]6 H, q this.id = id;
& y5 S* T" J1 B h. Y, N }4 w5 _0 ^. V% a. h5 Y. }* ~6 u; B
public String getTitle() {
3 C ^, o: o, z8 ]8 s) z return title;
8 G% s1 F. y; L1 Z) e }
- {* _* k. o6 Y3 n8 [ public void setTitle(String title) {
* z% u( x& ]3 H* {! I this.title = title;
5 ^1 o: e9 r7 l( T) {+ N9 K }4 e7 ` [( m% z5 M& J
public String getContent() {! ^/ B* G" R+ l7 I% O2 e1 f: ^
return content;
9 j t5 ]1 f( ^+ U1 v }4 v8 o' r& l: B
public void setContent(String content) {: u/ R: V7 V" I
this.content = content;
3 d0 r3 `( ]' ?5 D }6 @9 V" e1 D' K" R* b2 h
public String getPubtime() {& i- R, U- y+ `, L9 z+ v# V3 Z6 K
return pubtime;; [( x2 ^2 |6 u
}" S3 @/ {* K( b% Z4 @4 B. H- u
public void setPubtime(String pubtime) {
: W4 s/ F1 N4 t; P0 z( b% m. l this.pubtime = pubtime;7 W% k' t! b( |5 P+ q, D. Y3 ~
}; C1 p+ C8 t4 \/ d- U
public String getAuthor() {. w: V/ T8 F# n3 x6 ], W& b
return author;
5 v6 O9 O5 R" e4 L1 F b+ J8 Z2 d }
/ f' [5 M- P: c- j" k* F public void setAuthor(String author) {8 [" e8 E) E2 J% R: J/ o8 `! ]
this.author = author;
L+ Q7 i! Y1 u* v3 U# P }" T; _ M# r9 [
3 O1 y: K! M$ I( D8 g4 e }
$ T+ E- F; g* V' A7 ] 复制代码 package s2.jsp.zhangxiao.entity;1 M( d- q, ]7 q
import java .sql.PreparedStatement;( S9 J }$ k5 j; C) o
import java.sql.DriverManager;
) Z, I V* y; {( s" h import java.sql.Connection;
" V) z" T! q) e; O import java.sql.ResultSet;
8 q6 o+ I, U0 { _3 [ import java.sql.SQLException;
$ |; k; Y: R4 u. i4 d$ ?) v# `1 R
?' ~" w# m: i2 x; J* F7 h$ h5 N: M+ n1 O import com.microsoft.sqlserver.jdbc.SQLServerDriver;
0 y) c. Q. L4 z, `; e; ]: M) ? % K( M7 x: e! C7 d
public class NewsBase {
0 s: P+ w8 w: d* r& K" S , l- Q/ K' R, Y, G2 n' M" o ]# P
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";! [' [' a7 [. u7 `; s3 D G+ i
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";* U3 V$ S7 J# H6 L, |: d9 [' _# ^
private static final String DBNAME="sa";) L5 D, T: [& s3 h a Y( Z
private static final String DBPASS="1234";
4 x# S9 `& ]" K6 y% W/ L Z/ B v4 y: p: g; I# S
public Connection getConnection(){; z4 I8 }1 A1 P: J; R
Connection con=null;
5 e( {* ^! J0 O, [9 B try {' q$ Z; C$ G; ]3 c
Class.forName(DRIVER);
# |- Y( l9 h( \4 f/ w( Y$ _9 g8 H con=DriverManager.getConnection(URL, DBNAME, DBPASS);$ Q& L9 l, x7 v" l4 T7 h
} catch (ClassNotFoundException e) {. }1 W2 }2 ]3 o* w4 O L3 X( N
System.out.println("加载驱动异常");4 ^" s8 e- V4 f7 b: }: c3 X
e.printStackTrace();
& }- M9 _' c5 ~ } catch (SQLException e) {
* v0 U# n8 z$ @* Z1 v5 l System.out.println("数据库连接异常");& h3 I' s+ w8 `
e.printStackTrace();9 B% x, ~. l$ t4 ^" J( K. X9 ?
}" ` g9 R8 Z. r4 y) v
return con;
( y: `7 c! p! L$ c$ u( x, j, z }0 p& c- c' M2 ~+ z/ J! F
6 P, O5 N* u5 O public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
6 |% H7 v9 |, j1 N7 q/ P try {" u4 z6 V U# s m& U8 {2 ?
rs.close();
, U J+ ]. @( U3 C/ T7 ^ } catch (SQLException e) {; u& r; k2 A+ {
System.out.println("");& R) ~. O; _3 e5 h! g; h
e.printStackTrace();/ g6 C" C8 G& ]& `6 e7 |
} M( m* k, D, R% i
try {- D/ {+ h/ t2 h) u2 E
past.close();
k) b/ P/ ?' h, e! W: q6 Y/ K7 a } catch (SQLException e) {
1 N$ l; a" }! h: H2 B System.out.println("");2 E) H, b7 [4 m: c5 h- R
e.printStackTrace();
% M) H) D8 `* R' R9 R }4 _1 p# J6 }* }* ?( l# e/ W2 N2 M/ Z
try {
+ j: F" {! \ H# }6 t7 `- U con.close();
) F; h/ Q6 E! e) x; Y } catch (SQLException e) {9 g7 N. E8 V- J) d+ p6 r1 X3 _
// TODO Auto-generated catch block4 g9 k; [& N5 M' P X
e.printStackTrace();& O' A7 ~8 R. Z3 i4 B
}8 V$ r: S k) d8 g
}
: X" N4 s7 E |' P( {# H, D& F @8 y
& d. p- j% Z+ U9 _5 B; r/ w public int update(String sql,String[] getValues){. @5 B z" r5 S4 Y) C
int i=0;. ~4 c0 K" w$ Y/ U" G
Connection con=null;- a* t" w* j! l3 w( b& L' V
PreparedStatement past=null;
9 I. u! h) ?6 w' v con=getConnection();
, J- l- `7 [$ u/ I% q" U try {# c# y$ O: C6 M3 }
past=con.prepareStatement(sql);8 ^1 h2 r4 ]/ I7 T8 e
if(getValues!=null){
/ I- Q% L; i) o/ O8 Z p' y for (int j = 0; j < getValues.length; j++) {0 T8 }8 d1 }- x
past.setString(j+1, getValues[j]);6 t2 Z$ U% A0 Z1 d
}
G K3 }, U' \% m }- v, V' C$ {& d0 o6 r* f9 R
i=past.executeUpdate();2 a* G+ f- G* j+ V% A
} catch (SQLException e) {7 \% j$ j# H% b5 T
// TODO Auto-generated catch block
! R, |0 }% [- v9 I e.printStackTrace();
2 j9 ~( q6 s4 z9 T( V2 Z' ~ }
. T' U+ v. b! G return i;1 _/ G6 B: f \( o7 W+ I4 X6 ^
}
0 x0 p8 R9 p1 p }# M: ^2 ^/ G7 w) J8 a. t
复制代码 package s2.jsp.zhangxiao.entity;
5 J1 O7 d U2 `/ N* p5 ~' C
3 t! J9 @' h: s+ t) T, w import java.sql.Connection;
- d! v c/ ]# l$ q2 n& n import java.sql.PreparedStatement;
# w8 v& G5 k5 H/ H+ [, W import java.sql.ResultSet;8 P' O8 J+ O4 P3 p' Y
import java.sql.SQLException;3 Z. M: v2 i) L7 C8 B5 j8 \
import java.util.ArrayList;
, D% x$ \5 g1 |4 H import java.util.List;2 u% z$ A2 d s9 M) U# ~9 a5 R3 n
3 K& S6 J( ~- F6 s import s2.jsp.zhangxiao.dao.News;! I# L" l* e5 O/ W6 |
; n7 t+ d$ v& f2 a
public class NewsDao extends NewsBase {+ H3 q6 L4 |" Z
private int count =2;
0 \. v4 a- j9 K5 y: W; ]9 R/ d
: [ {: ^& A- g9 ^/ R /**
1 X; ~( J- j# _) t! k( s * 查询总页数
7 ]: s1 y+ ]# m" R' U3 v * @return
: F5 [! r% ~9 s: E8 [ */
8 ~4 h5 _( K8 F# L1 j0 { public int countPage(){
8 p# `6 k$ M7 @' r2 g int rows=0;) `- j* q6 H7 l. \/ X" L* u
Connection con=null;$ H# f1 _6 y, R+ K* S
PreparedStatement past=null;
8 m, W& _& }7 \3 ?, x0 \, f0 a3 r ResultSet rs=null;% n. B4 y T4 y* g$ q7 Q5 f
con=getConnection();
, }! b: _! q0 I. l( ~ //查询所有的的页数 返回受影响的行数 y3 L8 g* h# D" A% i# ^7 T1 J
String sql="select count(*)as c from news";! j! _9 M; x: }- G: C d0 S2 ^
try {
$ S. i5 J) B1 I, R. i. A) ^. o past=con.prepareStatement(sql);5 U3 u1 g9 L8 X/ g
rs=past.executeQuery();
1 `, A$ w y: I if(rs.next()){
) @7 q8 d& P& Q0 o v7 e) i0 j2 Z //将所有的行数保存到rows 变量里1 g8 p8 P" J6 O# p
rows=rs.getInt("c");$ E/ E; s2 o2 q. a
//如果显示两页 count 代表显示的页数 2 O& u. P( }# \5 Z
if(rows%2==0){
8 g$ k/ j6 a+ J: T% E rows=rows/count;
. j. _. ~- J; D2 [6 O 4 w, B6 J5 Z+ y) k# i
}else{( A) I) K1 O: n) E+ O# z4 u
rows=rows/count+1;
& c+ `8 @ [! T5 Z }4 _* y; S$ Q- Y* I# Q* q( S
}
) l( v) c, ]1 M; `+ M, P8 X } catch (SQLException e) {
" h9 t3 _2 q2 d$ s System.out.println("查询总页数方法异常");
6 V/ t8 `9 D. o7 P. H' G* P7 v e.printStackTrace();
) E+ R0 Z5 w y7 I( U& L1 | }' C2 N1 S- h$ t5 p: j, \1 U# I
return rows;
. D9 @; Z# T7 y7 m/ ?+ |* ?* K }8 E( G3 T; J8 r, t) ?; m5 B
/**' H& {- s0 z4 d; W& a
* 根据id删除信息$ ]7 Y5 i$ z, u4 ?( V
* @param id
& J% W- j: D% X8 A * @return* p7 i6 {9 }2 G8 N
*/0 d& }& o' }, n! ^ f% P
public int deleteNewsId(int id){
/ |2 a5 @) }. ?- M. _6 C: p) H String sql="delete news where id="+id;
% z! z( w, [( `7 S) W2 {4 a) d return update(sql,null);- |6 t8 t" p' ^* s+ Q6 S+ i8 H
}. N- C# j9 u- F* o F
/**+ c, y. ^/ k; Q
* 根据总页数分页& B# N! R. R/ u* N1 Z; a
* @param page/ o6 Q1 m5 o1 h6 m& Q/ |; H
* @return( S+ Y6 O3 R/ X! w& e, N
*/+ o1 U! n# Y7 m- _
public List getPageList(int page){; @+ A: b+ t# o
Connection con=null;
5 x% I' ?% d5 y6 i# V PreparedStatement past=null;
# z/ g% s# T( ]) Z$ {: r; y- ~. C ResultSet rs=null;: a. Y' l! O# t1 R# D( H4 E7 f/ x
List list=new ArrayList();
6 U6 }- H5 d4 v4 {$ l; o3 a3 V' K con=getConnection();
9 X) @; `! z$ T! J" h( d //" r1 f/ ?$ r# ^! Y/ C( A
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";4 Q! p/ d! K( y1 ~
try {
! f) q% c- i: P# l& ~0 S2 [3 } past=con.prepareStatement(sql);
2 `' l% a$ ?. n/ e: @ rs=past.executeQuery();, Y* t3 x; l. `1 J% i5 t. {+ B
while(rs.next()){- ?: \/ S6 ?4 l$ b: n
& v& B. | @1 ?8 _1 f& L
// id, title, content, pubtime, author
; J% y+ [9 l! b int id=rs.getInt("id");
' |& U6 N# H" v# e, \9 I/ k String title=rs.getString("title");
& O6 s% q& J8 e0 T String content=rs.getString("content");
C& z, Q' w, ~1 d String pubtime=rs.getString("pubtime");
; Q3 c0 S. s* n- o/ O String author=rs.getString("author");6 k( l. a6 X: c% x( b' ~
News news=new News(id, title,content,pubtime,author);" W' z- Y2 r. s+ `6 [- ~& d
list.add(news);
! o% y5 p, g5 r8 o$ }/ J }
6 n; G7 h$ o* V } catch (SQLException e) {
7 _/ m7 A! ?& Z7 X b7 `/ E' ? // TODO Auto-generated catch block
3 {! t' Q5 Y' ~4 n! v+ H e.printStackTrace(); v$ I& {5 f' V( Z
}
/ a* `- T: r3 e return list;: _/ M K# {6 z, z
0 N0 v" T; G$ C, r; h7 y
}
& u6 r' z% b8 W) S% A }5 G( c0 H( [5 x- r7 q# b( I
复制代码 . B$ o; X0 F1 ` E6 N7 c
+ m$ q- h# `: H0 N2 }; D1 Q5 y8 P
源代码以及数据库下载 :
( |% v2 c9 \7 F6 \9 m0 D
8 e0 }! O" W- g2 A- U3 V
科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网-Java论坛、Java社区、JavaWeb毕业设计 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :