TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;9 U$ a+ P \$ B9 r2 w R' s
/**% ]0 n; G) w1 R2 B4 u& t7 i! v$ t2 o
* 实体类
2 \* P n3 A- B2 ~' a * @author Administrator: @5 |4 j" X( u
*
" Y- q- {% t# S0 k0 ^ */
) a9 e* b; u6 L3 M9 u6 f, M public class News {; m" R+ {. j, p, ~2 `5 m
// id, title, content, pubtime, author
% z2 E" @' Q7 N+ S% ` private int id;2 }) K# ]9 |8 g2 n; ~
private String title;, w1 F8 \* w2 S7 [! O% f
private String content;. ^7 S& a2 v. @7 A+ M- k0 }
private String pubtime;
$ b; o. n9 V, c# Z) n7 }9 y private String author;: c1 @+ y! }+ @. Z) U8 ]
/**
3 A/ J P( v. z/ Z% M6 e * 构造函数% {9 O6 t3 V% o: p/ R2 C2 ?
* @param author 作者0 ]5 r. j _- d. u
* @param content 内容5 M: K; g! \9 K9 c
* @param id 序号
: K9 d5 J% U2 T+ t2 v * @param pubtime 发布时间
4 o* ?" ~" E9 l+ e, i# l * @param title 标题0 E; b& @& Z) _% ]4 N' g% q7 _: A
*/0 `# A2 l+ a, r2 M
public News( int id,String title,String content, String pubtime,String author) {
5 |3 W4 y1 w) \& M( p7 h
6 J$ u( ^1 Q. M4 n this.author = author;
; f. a4 F# R1 b- _2 F5 x this.content = content;6 t: v) m4 ]4 }) u2 ]8 Z4 i
this.id = id;
. E. F! I& B) ]) e1 R this.pubtime = pubtime;
( @8 v+ A1 U- y9 p, Q# g5 J this.title = title;1 s, Q1 i3 o- \% q
}
6 X0 v' ^# X5 E! \ /**6 V o# x! a; j) B$ l# S
* 无参构造函数7 K3 b/ s3 ]- e- R) W/ _7 l8 k c
*/* z ^: c! v/ C" T# t+ g
public News() {' X6 ~% ]7 s7 L) Q3 ~! r
super();
1 F& k) k$ Y8 Y" P- m( I& [% Y }
2 U) N L4 x) F; E, [8 A /**4 B4 F0 u( O3 `6 c0 q& z! _7 G
* get ,set ) a8 f+ W' v5 t- U5 F4 K; U
* @return
* V, _, c4 h' S% P. U; k" @0 g */
& t) X1 r2 ?1 k5 F" j1 e8 Y. t public int getId() {
/ K; O, U9 [# A6 ?# Z return id;" q. N7 W9 W- j$ @! s, T
}
' C" s$ [; S% N6 U. g public void setId(int id) {
. h$ ]& u, V) O% P$ A |$ m3 | this.id = id;4 C( S; `; d. q4 L) p% \! b3 K: A
}
z) j4 y* l6 d7 m+ Y1 I public String getTitle() {3 ^0 B5 u9 G$ ]) x3 A
return title;7 p! E3 {0 J7 f/ z9 x1 j4 B# G
}* d& S7 O b3 d3 G+ c* l% I- }! M
public void setTitle(String title) {3 n6 n2 Q1 c% O' l2 Q0 Z3 Y4 ~2 X7 [
this.title = title;
# y2 |9 w2 r8 A K7 s' d" `8 S }& j2 i/ ]/ v2 `, R8 h
public String getContent() {* I2 X$ p8 v: t: d, _: U/ T# [3 e
return content;: D* T+ I! u9 |, B' [
}
) X2 f1 K w) m* u* W public void setContent(String content) {
) C# l4 g* f# p# Q: U0 l k( }0 }& Y this.content = content;
4 {# D1 J/ P6 m- J } @5 z; N. s& z. p: d
public String getPubtime() {
$ X; P7 S: F) b. P g0 J3 k return pubtime;
+ i( f8 Y" o+ d: _ }. ^6 O$ I7 T! t
public void setPubtime(String pubtime) {% d% c- a' D" G3 t
this.pubtime = pubtime;0 h) Q( A0 K6 }% n5 D
}
6 T' y) e# B4 K0 a# V public String getAuthor() {
) Q6 r, ]/ {$ v, w return author;
8 |8 L `3 d! d$ y6 V }/ E9 i( g; f$ M
public void setAuthor(String author) {
5 H4 O- q! v- @, J/ h6 n4 y4 n" q this.author = author;
. `1 t) p) v0 ^3 j }
5 A% n) a7 k0 A
B- N& T% n9 F% r5 G; e }: o$ n+ [2 d$ X- i F Q! x
复制代码 package s2.jsp.zhangxiao.entity;' M6 e% j( G& C( o2 \+ t8 u! |9 c
import java .sql.PreparedStatement;
$ H/ _% K4 E0 e# w" M% l import java.sql.DriverManager;
: q9 Y& H$ d9 F y5 H) R' g import java.sql.Connection;0 ?4 U( a7 |+ P2 X3 r
import java.sql.ResultSet;( I' [) X2 S" Q( l
import java.sql.SQLException;
! K( [1 G6 \4 Q8 ?! E+ w 2 u6 V9 ^$ B1 R5 n, D* A" k$ l
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
, G: z; W& b& L5 w2 c! s+ x
" x! R0 |- H3 l: i: ~! A! T$ f1 } public class NewsBase {) m1 q2 R' T2 u1 a6 \
2 D/ O2 Y8 n6 v6 X private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
8 y* E) M* L7 |; a9 a private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";' Z: t5 s* P# ] q' l- _
private static final String DBNAME="sa"; k& k- h9 n7 E/ W. C
private static final String DBPASS="1234";8 w& s+ v ~9 }+ d: y! s( w
! e9 Y! ] G) I2 \& u5 h8 T# q- ~ public Connection getConnection(){
1 B/ H7 o6 u+ M Connection con=null;1 n5 E% [7 |& ?+ t
try {
9 e) Y0 b: X' Q: P1 {/ |* N Class.forName(DRIVER);
1 x8 L7 H! q* r+ ]! [4 w8 P' B con=DriverManager.getConnection(URL, DBNAME, DBPASS);
+ {" G3 ]. r0 Q1 q } catch (ClassNotFoundException e) {2 z w7 z, ?/ w* u( H
System.out.println("加载驱动异常");
) F. V* [# ^% y- f7 ` e.printStackTrace();6 q0 r& Q- m. ~3 d
} catch (SQLException e) {
0 B6 _4 s. j* B7 W! \ System.out.println("数据库连接异常");( c$ b G9 d4 ?: p
e.printStackTrace();
6 P% _1 E! A* N, o( K }& F; W1 f$ f' ^, g0 {% g
return con;, d; V, \4 b& n% S/ t5 }
}
; d/ V0 L1 M, M% ?5 b
6 @, H4 p, |: I, a: K public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
7 M: H! l. U( J: Y/ z$ k8 { try {
) L% w% P+ U" y rs.close();5 r5 h! \0 V" x0 [: E( L! Y
} catch (SQLException e) {
; ~& d( Y) x# @6 w0 _$ C7 M System.out.println("");& e, Y; Y! z/ ^" P/ b) j
e.printStackTrace();0 H, }1 a5 P; \' h' j5 \3 H( l$ O! k
}( [% y4 R2 W+ c4 d1 d# H& b: ^6 r9 v
try {
: `3 I4 _, Q- `% ]4 ?* m( ] past.close();9 h: z2 }& Y+ g6 K% B
} catch (SQLException e) {
5 N" f, k3 d" ]3 I6 t9 {5 S System.out.println("");# B) { Q. D+ a' S! n3 c
e.printStackTrace();
: h" o. q+ R# V' B) r: ~* } }1 P7 E0 {, O9 y/ n
try {
6 B4 o2 l( _% x) [6 {3 i con.close();, P, y6 }9 O) d
} catch (SQLException e) { w4 p# T3 B! e0 E& h; H B
// TODO Auto-generated catch block* a8 c% A- h7 y/ L4 d- M
e.printStackTrace();; P8 o) Q. G$ R+ h& ]
}! d6 J2 E* e8 A& H: }$ M
}
( |3 k5 [; u$ p# m7 r
& K3 q# Y" I( T4 C0 ]* A public int update(String sql,String[] getValues){4 y1 ]# U+ K% M$ E4 Z
int i=0;
( {6 x) m/ ~, b& x9 [ Connection con=null;7 c1 u$ @5 m! w6 w. T+ X& H& S! \
PreparedStatement past=null;
$ P) x' P9 K& M" o! u- V con=getConnection();
A3 b( Q% D& C; V5 G: ?* ` try {
5 h9 f( s; D+ R. ? past=con.prepareStatement(sql);; S9 v8 _) d m+ @* x# B0 y
if(getValues!=null){
$ {) h' U$ S4 I& v for (int j = 0; j < getValues.length; j++) {/ [2 f* K) v* b% s D7 a/ D- V
past.setString(j+1, getValues[j]); B0 ~! f3 E: z4 Z, o) F; m
}- Q$ [2 V5 G; @' Z9 g3 x9 T
} s, Y$ P6 E+ l) x
i=past.executeUpdate();
2 R f" n S: }4 ] } catch (SQLException e) { I( l" m# S! Q0 A
// TODO Auto-generated catch block+ }# o/ B$ n8 |9 b! H
e.printStackTrace();+ `2 N7 N- D- W' Y4 r( `
}
7 Q) D7 |- e8 P4 ?. A( ^ return i;3 A* K& K0 m5 }! O5 W
}0 i5 M) I% E% u/ p
}7 s; L6 P: }- t' _
复制代码 package s2.jsp.zhangxiao.entity;8 l' H: O# v" v3 e/ x2 d4 V5 y
3 B6 h; k7 k$ t, d, s
import java.sql.Connection;
& ?# D# E& Q# ]- E, T import java.sql.PreparedStatement;
. w& G% ^6 v; o5 K* z ~( l import java.sql.ResultSet;/ Y4 O3 f8 e; `. t8 X2 E- R) F
import java.sql.SQLException;
% o/ V, v( o( g import java.util.ArrayList;
0 r: v( w% b8 u2 F import java.util.List;
& t y! H4 Y3 N& h) Z3 J a3 X" G7 v * [) m9 ~9 V* A7 E y2 j% I( |
import s2.jsp.zhangxiao.dao.News;0 i1 {' t7 d% _* ?
@7 }: e9 j3 C1 N+ W
public class NewsDao extends NewsBase {
3 f; c+ H& a$ G7 ^5 e1 p# x private int count =2;
$ P' ^9 I d: d% D & p) Z {* z# v* \
/**9 W4 Y+ E6 s: V; G+ [2 J( @4 [
* 查询总页数: o+ O5 K/ g/ @, w# |0 e/ Q
* @return
2 h( k3 y( D) t" ] */6 M# @+ B4 v/ x2 a/ Z
public int countPage(){
3 n t S" U3 x6 J int rows=0;& x3 N/ z# N0 ^: X# v6 i
Connection con=null;
$ Q6 m& D _7 D+ |! Z8 r PreparedStatement past=null;4 C$ U" s S" X" p8 h, y5 T
ResultSet rs=null;
! ]; S- H4 E" Y& Z/ A con=getConnection();
" Y7 ^$ I' h# A/ R$ w- V //查询所有的的页数 返回受影响的行数+ P/ m2 q# ]% W; o
String sql="select count(*)as c from news";7 @9 o: \0 ]6 }0 f S( |1 R" [5 p! h' b
try {6 L1 \ z7 U, v6 j' t B' e
past=con.prepareStatement(sql);
# ^+ }- o% m W rs=past.executeQuery();
9 s. N/ I1 L1 P* j6 w if(rs.next()){+ L% w4 q: a- h( W2 ]" [
//将所有的行数保存到rows 变量里) C7 e" J( d ~6 O1 S Q
rows=rs.getInt("c");$ p% e! A1 c: T5 ` I) Q9 H7 @
//如果显示两页 count 代表显示的页数
; C/ F6 Z( X$ y* J0 E J: ^ if(rows%2==0){+ y( c7 p2 e8 }' j
rows=rows/count;! H K: g/ d/ J- i
0 i: Q% O- c$ \( }0 @
}else{
" o: r' M+ D& Z! z rows=rows/count+1;% J7 s9 R: _8 G6 B) n) ?
}
8 P9 D6 x" J% Z2 N }4 m' \* ?6 y' |7 L# k
} catch (SQLException e) {
0 s1 ^% U6 T5 a2 p. P6 \ J System.out.println("查询总页数方法异常");# V( ~8 D$ _4 o! P2 u
e.printStackTrace();
( Q4 b9 v4 {1 R }; M% M& P% i) p8 S
return rows;
r6 M, q `( U, c+ U1 F8 p }
5 o4 s' w( i ~6 h /**) H! G: h2 p- e# F1 B: M: a& j: E8 h
* 根据id删除信息
$ V7 ~* ]/ N: }- a * @param id
, h' r+ n% X k$ ] * @return
~! I/ D( {. w) f1 f/ @ */. j W3 ^/ a0 y3 F7 w) w
public int deleteNewsId(int id){
6 F1 S, w& p! F' `. U% \' w8 T String sql="delete news where id="+id;
* U: O. c4 X0 k5 ^7 x6 X1 c7 [ return update(sql,null);
' x& d, ^1 N1 J& ?6 Y } `/ G+ T6 _2 t2 y
/**9 b K; ]2 W: X; e. m$ V! s
* 根据总页数分页3 T& O% H( [( s7 A- `$ o
* @param page& i X! H# X# w. F
* @return) }1 F0 l" _- z
*/# q2 e+ G3 H; d0 j
public List getPageList(int page){) m$ {* b5 x6 @& p" I: u( M
Connection con=null;
8 s, Z/ T0 H1 R+ e1 z PreparedStatement past=null;' D- E w# D/ Y0 d% M4 o2 A7 I
ResultSet rs=null;
/ w" j8 f, j7 j; c5 c& X3 ?( n. T List list=new ArrayList();7 g# L U1 O" y, Z6 Z0 V9 H
con=getConnection();
: D. i; `8 b3 t. @- R //+ @5 @! J% M0 }# g
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 T& J8 \3 p+ P* F, {0 `7 e try {2 q: U, G. d% Q* {
past=con.prepareStatement(sql);' \5 p& @/ U+ o9 E9 g
rs=past.executeQuery();
1 h3 ?" e5 O7 g' Q. y8 T4 ^, } while(rs.next()){2 V+ ]/ K, L; }. I; H) _& ]
* t7 K+ [2 l$ v2 T" Z+ Q2 o: s
// id, title, content, pubtime, author
4 p6 E8 ~& S2 K$ E- q5 X int id=rs.getInt("id");' k/ _9 I% \) u8 t# [% x
String title=rs.getString("title");9 k9 [% B$ Z* ~+ ?0 X4 J6 |) r, f
String content=rs.getString("content");) _; o/ e1 I2 G# y, F. u
String pubtime=rs.getString("pubtime");, ?: D% p( l( A& L% h! w8 q Q
String author=rs.getString("author");
# i" g4 D6 u3 t News news=new News(id, title,content,pubtime,author);' F& z! q3 |% L1 ^0 ^! Q; t
list.add(news);2 y* I! O, @7 s# K- K
}
( `3 J& n: W8 i+ O) G# C. a } catch (SQLException e) {
8 w. m# w& z0 z) y: @: ~6 e // TODO Auto-generated catch block. M( v4 b5 L; ?2 F) B. d+ g
e.printStackTrace();, f% y4 d- b# @
}
# _ V( V( A% w6 K z return list;* {" ^. j# z2 T# e. U
7 i; g2 F& G& v$ s
}0 G9 G: m4 B5 ^) }
}6 ]1 e' q3 i! g5 P- P y# R6 {
复制代码 V K9 G6 H I {$ Q1 v' v- p% Q
. {. m) }+ Q% a9 }# w8 q( }8 l' |) o
源代码以及数据库下载 :
- x& Q) e G. |9 D% ~5 v 2 G9 r' u2 t& I1 d3 H3 e+ Y
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :