TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;7 R+ p* m& E0 u, @- O
/**
! P* Q; {6 t* J5 R * 实体类6 Z( E! _. @2 H b+ g; P# b i
* @author Administrator3 I% a0 C4 p5 S- r4 q8 ^/ [
*3 E; G8 G; }, a8 E
*/
3 d1 A) v W4 @1 A6 E0 V: W) \1 K! L public class News {( h* D1 O! L! T
// id, title, content, pubtime, author; {1 y, ?6 }+ I! Q( b( B7 x+ \, g: \
private int id;
& J! e. s' x/ \( E) m3 C private String title;7 u) e" |* C% K; E
private String content;- x5 G5 {% |$ B& N
private String pubtime;- L( {& d7 F# F# r m
private String author;
) h0 }( u( e2 s& K I$ |, W5 m /** ]5 @2 f) P+ z8 n/ c4 s
* 构造函数
2 `: _7 b0 y: M0 w$ ^8 ^ * @param author 作者
% Z$ i: {' Q& B4 ~( E3 {3 H * @param content 内容0 B) }2 ]7 {( t& p0 C$ D+ m- I6 p2 ?
* @param id 序号, x0 T4 q Y6 G9 F5 I+ j0 {
* @param pubtime 发布时间
2 f7 t2 A9 T4 i" P * @param title 标题) }" f+ I3 x! r0 L
*/$ }4 v4 X& Y9 Y* E% s
public News( int id,String title,String content, String pubtime,String author) {
3 F. K& W9 y( j. w( c
4 `' l! ]* t' v; R! T. M this.author = author;* M& |6 ~/ Y2 k' b( r/ U+ Z
this.content = content;
* m( h& [6 R' j# n( _& ~ this.id = id;
$ ^0 S; C4 L4 M( u8 b this.pubtime = pubtime;# Y5 G3 v* T( z$ ]$ \
this.title = title; l% m" t; h7 a
}6 q0 ]2 m7 h, t4 A8 |
/**
( h) t( J1 r, n- q8 j; r * 无参构造函数0 A8 ^: ~; @5 V" G* K
*/
. U; y0 ^9 O- F8 A0 u2 m* l public News() {& H: n( _- m- {, O! z7 s
super();
6 k3 R/ |- C7 \) Y }7 P3 M, \/ N l
/**& U5 D; b$ P4 e; a" h ~" p! ^
* get ,set & q# B7 W) {6 C/ n1 _7 O. P
* @return
2 E, G) q% Z& X: F: Q2 R */- Z E" [5 l( S. _3 F6 Z3 z
public int getId() {& `3 A, \: T# S: { V' |
return id;
) G! k- U3 Z2 z) m& G }. n8 H4 O. n, o
public void setId(int id) {
M: Q, U# u' _: @/ K this.id = id;
/ E' `( J0 |+ s1 Y- I/ t }0 X3 D3 |/ F2 p
public String getTitle() {
5 k# r; e' E7 ]( ^: N* U8 y( {/ e return title;. H, b4 ]+ K1 I7 ~1 k
}$ E0 [; r9 x! m
public void setTitle(String title) {
: O4 [3 j0 l' H- t" J; M this.title = title;) x: q1 E0 f7 }/ Q% _' o
}! c5 o: U S3 c! `0 v- H3 w F; W9 u
public String getContent() {2 G" d, {9 Y) ^: M! r2 H5 |2 z
return content;
7 H, i5 i, I& j% c2 }: q }% Q, U1 q3 j0 I9 w! G! e
public void setContent(String content) {. x: }! e5 P4 T
this.content = content;
. W0 J g/ g T+ \$ b& J }3 w& z- a# @, c# t% @1 m3 e3 Y4 T
public String getPubtime() {! A1 O' w$ @. L0 L$ s7 h
return pubtime;' f4 d+ ^ U7 N6 I+ A$ r
}
$ s+ u% U4 A; | public void setPubtime(String pubtime) {
. t8 Y S x+ Y, { this.pubtime = pubtime;
/ l/ e* O# N! R- i- N+ t( ~4 v }
K; m1 A( }8 g! }7 d. x public String getAuthor() {- V9 U% S$ ?4 I$ y" j' L6 m& A: ~
return author;( Z7 O1 T- v0 }9 f+ s& z' p7 o
}
+ |4 X, M9 q6 y1 l- k; Q public void setAuthor(String author) {# q, F' h% F! | ^
this.author = author;* I E* z0 u% k% b- y: m9 t7 @8 i
}7 g0 U" [+ C g/ N
: S q) Y- O$ E) T) V( |
}
" a8 r5 Z9 q: ?7 I2 N/ T 复制代码 package s2.jsp.zhangxiao.entity;
% |" O$ e+ y1 X! N& i" a1 r import java .sql.PreparedStatement;2 S1 \* w5 }, ]4 x5 V
import java.sql.DriverManager;
! L% Z3 R" U$ O; @4 p/ m7 k1 i$ g import java.sql.Connection;
9 m @+ P% }7 w2 z! I4 T import java.sql.ResultSet;& ^ o0 r4 L0 ]2 m: d: R V- R
import java.sql.SQLException;/ F) g4 I/ E6 `% p( f& r$ n1 j
! l8 W" ~, K# m @% o* S import com.microsoft.sqlserver.jdbc.SQLServerDriver;1 X, h& }) x+ M7 {7 `- U S
/ J x: @2 ^. W public class NewsBase {; S# l: K" W4 U# ^ s3 Q( B( O, _
8 s, g5 t9 ?& G+ x% t) U private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
5 e7 ?5 z/ w4 B: S; g; d7 d# ]* W- \ private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";
+ a( J" j) w9 z* H$ u private static final String DBNAME="sa";
0 i( w' J/ }- \ private static final String DBPASS="1234";8 y( u0 l+ I+ K) A. l4 S3 v# G& T
* f0 m& L0 `) W! ~ |! Q
public Connection getConnection(){! C8 B7 \+ |: e) C: D. ?7 _
Connection con=null;
* c! i0 F/ i( w! L$ k, c& q try {+ I% A- |7 V& d$ A
Class.forName(DRIVER);% ~" [& z' Q* W+ m
con=DriverManager.getConnection(URL, DBNAME, DBPASS);" `5 D" S. z, c& L
} catch (ClassNotFoundException e) {' g& |% ^6 ~# J5 F
System.out.println("加载驱动异常");
[. y1 r/ q6 c4 L% v e.printStackTrace();" f2 ]6 K8 x R; r
} catch (SQLException e) {0 _; F2 P* P* Q: l. |
System.out.println("数据库连接异常");
- w# |. A9 N" n e.printStackTrace();+ `# b! o( O& r
}" z8 @* R1 S* Y
return con;2 x( O0 u( I" I: u
}
4 [+ W( ^ q* u$ k$ h: Y2 r5 W* T8 A" a
4 |, p/ |8 s" G4 Q public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
4 a) F; a: a4 k {- c! I' r try {1 U# Z1 n/ D# \: H( V# k6 B. \$ h
rs.close();
5 y4 @) Q" P. R! A } catch (SQLException e) {2 Q$ ]. M: t m- q9 N
System.out.println("");
?" X) r$ h- U4 z: u0 G" E) f$ o e.printStackTrace();4 s" s. k7 N) B% z9 V- c+ [8 i
}
" f+ f9 F5 X0 r try {
3 g' v4 o% i- S: }, x: r0 p; p; ? past.close();
: x s5 O0 h: r. f+ _ } catch (SQLException e) {+ m- Z# T: n; w9 ^! o" Z; M
System.out.println("");
- ?- t5 ?( _, b7 j2 o0 q e.printStackTrace();1 C7 N* x4 e& g1 m. a
}$ @, N. l% G- f# L4 ~7 T
try {
" o+ D/ z% S1 b3 e# L con.close();
; N8 H! [# Y" | r/ G6 R2 M1 f } catch (SQLException e) {1 r" r3 p$ n! `# E
// TODO Auto-generated catch block
5 Q! l W w6 ]7 ^! N* o# Z e.printStackTrace();, g5 \( L: p( S4 b
}( J( ^ [7 E* X. e
}, d# ~- y/ D, G% O0 z8 H
# _6 c ^8 T4 C* L; e% q. D' u, b public int update(String sql,String[] getValues){
* b; `: b- @" u' Z6 X6 ?4 z. r2 n int i=0;
5 T& A9 ?, X* l2 m* N/ |$ j8 B' G Connection con=null;
0 u" \. l+ ^" P( o PreparedStatement past=null;
+ ?6 I7 ]6 X1 Z con=getConnection();
( R0 W' p' d, o! Q try {0 L3 u8 N; v" m$ |4 B+ U+ v
past=con.prepareStatement(sql);
3 ^: u! W$ k6 h# R z if(getValues!=null){/ U" W6 N) w ^# ~# d4 T
for (int j = 0; j < getValues.length; j++) {8 L' Z" M2 O2 j, d8 l
past.setString(j+1, getValues[j]);& f3 J4 ?1 Q3 h( \, ~ f
}/ f" f6 n1 y# W0 A8 Z4 k0 |
}
( Y) I1 I/ @3 J i=past.executeUpdate();, t5 Z ~ T% P4 P* f" k
} catch (SQLException e) {4 b6 T) o6 @) f% t; c
// TODO Auto-generated catch block, t C: D! K( e
e.printStackTrace();7 j% T$ a4 a( K3 \/ e. W# `
}
/ X$ D, n2 _2 }; B' B7 h# F return i;
" C# t# q8 `/ v* g$ i }: W6 K7 L* H/ B9 J
}4 s* K" [" Z6 C3 O3 V/ S& i
复制代码 package s2.jsp.zhangxiao.entity;
' ~# s5 W9 z$ G7 z' Y
2 \/ m) B8 m6 N4 r5 j6 L3 P import java.sql.Connection;
, o) z6 X/ R" Z! O2 l) _8 X import java.sql.PreparedStatement;
3 W w7 `0 F4 o import java.sql.ResultSet;
# Z) O& m$ z- p: f; I import java.sql.SQLException;0 R* u$ o2 @4 \# `. m" t
import java.util.ArrayList;- S9 I" e: D- x- ~! f. R( {5 p
import java.util.List;
; a1 W3 R/ C$ c! S& J _
! W2 \' N* k5 e- f import s2.jsp.zhangxiao.dao.News;
4 ?* g# B' ~" {4 t
8 {( S9 o2 ]) g public class NewsDao extends NewsBase {
/ p! W2 G" \, _, J1 h private int count =2;3 q( c6 R( A, ?- J2 _& [
( Z G9 ~" e6 v& a0 m3 [ d3 j /**$ u& z. r7 z7 ~/ V: n, k
* 查询总页数
9 W5 [0 ^% x( |: A/ R$ ~4 h: x * @return
6 W$ M2 e# i# S6 X; S */# e% }% C) ~! [
public int countPage(){5 A0 ~ ^5 A. d$ N: C* {% q
int rows=0;* M$ O2 e1 y! h( y- x
Connection con=null;
) t; o( i+ _' y0 i$ j. ~( V4 T PreparedStatement past=null;
/ H! |, I' s. x+ f$ k6 O* a5 U ResultSet rs=null;
w( H$ C! b/ `- d4 y# R3 C% t con=getConnection();8 S, L% e% z. R% r# F
//查询所有的的页数 返回受影响的行数
" }; T/ w& |1 O" s) D4 ~; b String sql="select count(*)as c from news";& `' R& \9 R( N
try {% i( Z$ H# d' m$ K# i- H" o- W" q
past=con.prepareStatement(sql);
) q! C+ l0 F2 A( V( y% ^9 d rs=past.executeQuery();
0 A5 k: p3 m/ i: }0 u2 j if(rs.next()){% n: I) q/ b$ ?
//将所有的行数保存到rows 变量里 G0 m3 J( V+ v, d; I* M& Y) ?
rows=rs.getInt("c");
( W1 K8 Y; y% g+ s: V& e //如果显示两页 count 代表显示的页数
& J/ M% }+ [7 r* V" T8 K if(rows%2==0){, U8 X1 g2 Y8 T# p+ o6 Q
rows=rows/count;; R4 X- s$ L7 K" K {4 m
: h2 j! E' E. A, z* _ }else{
) O) { y J) B) P3 E3 n9 x rows=rows/count+1;
1 ^4 F; c* q, T) z& F4 a }
( R d' y- E0 l* _6 |% s }2 G! L' d5 d3 O( Q/ z6 Z1 D& l% d
} catch (SQLException e) {
& I3 e7 b: h; g, B% s System.out.println("查询总页数方法异常");! B, P% b: `0 N4 f3 e. \
e.printStackTrace();) m( e0 e2 g$ p4 i' G1 [. n
}
+ I- N) w [2 f6 R return rows;
M' y' U5 C+ ~6 R }" ]- f( {! V" Q7 O8 \% j q
/*** s7 k9 \9 ^: F0 I
* 根据id删除信息2 }$ I4 X4 I: ^5 O% u( N
* @param id5 G. U3 }2 m7 \. i, j D- b
* @return$ `: l0 w0 r+ \+ L6 P5 p8 j/ ~% O# Q
*/+ Z M$ Y7 f* b; w
public int deleteNewsId(int id){" M! M F) ^- a+ s7 C# m" x N( N5 {
String sql="delete news where id="+id;% U' ]1 l( X' z8 J- t6 c0 P
return update(sql,null);
0 f- m; s, V1 L7 ^ }
/ J& I. O" e6 b' S/ P /**0 r8 t( R' L0 F# J4 @! ]4 k
* 根据总页数分页6 P; h o; \9 t+ L( C2 o1 C. O
* @param page, h5 q# Y( a) Q
* @return+ k N" i) k& x/ R2 j7 d# e) g
*/3 H/ M- i' ^/ R
public List getPageList(int page){
$ n* H$ y; {1 @$ V ]; y# t Connection con=null;
' ~& R' U, {1 ~) A; s4 m PreparedStatement past=null;! B2 S" {+ d0 ]2 K, Y9 t- ^) V9 C1 T
ResultSet rs=null;
- x- d3 e2 a: D8 F/ E: @ List list=new ArrayList();
) @: ` h: ~1 v# b con=getConnection();
" R( E8 p6 P ]) j9 f( Q //& C3 C7 x1 @3 o% l8 H
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";
9 o2 ] l: z0 Q) I0 Y9 N try {+ E0 \ @8 M# E
past=con.prepareStatement(sql);0 E) M$ w. p% v
rs=past.executeQuery();
4 U7 K% L5 h3 c9 L( q while(rs.next()){% [ L3 D, v" T& V$ R) x4 D
; t7 @! Y5 O( b+ b
// id, title, content, pubtime, author
# {* z( D, B4 n% e: e) y9 G int id=rs.getInt("id");
% f, Q, F. }: n0 j- X String title=rs.getString("title");
9 U/ o) c5 d# v1 i7 m String content=rs.getString("content");
: P+ o* r9 }' {, e! p8 ^7 i5 G String pubtime=rs.getString("pubtime");
8 a& q6 [% J+ u5 ?2 [/ g* @& @1 I String author=rs.getString("author");5 ?5 E0 E( z" Y g" v- i
News news=new News(id, title,content,pubtime,author);( ]+ q% J8 G Q9 b4 r! i6 X
list.add(news);
5 x) T( ~- _0 Z. K! P R+ h5 g }
. o( H9 ^" D! A" }& W } catch (SQLException e) {# b( W+ c& [5 d# R }7 ?- J
// TODO Auto-generated catch block* U u t0 `' z6 U+ p
e.printStackTrace();9 O3 T/ Y$ H0 q' k1 ~7 A2 t
}
( F6 B/ B# Y' k% c. Q return list;
& I$ F! u. D6 F) n' f7 y( ?1 Y; K
O3 _: a' B% K% M% L }1 P( X; t' i- Q" c0 J* i
}
" w2 a7 ]. U2 K 复制代码 9 f. x, \4 a( Q! p, u' f5 W* q
: J" s1 R9 _2 m) g; \ 源代码以及数据库下载 :
/ K$ G- G9 i0 x( c, F6 S
4 O) ~- `) I2 v/ X' K1 r' Q$ E
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :