TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;% A* p5 g. `& i% L, C+ I
/**, z. s$ ]3 G+ Z& h: V$ O5 u" j
* 实体类
' Q; j* a v" ]' X8 ~* C * @author Administrator
) L: I2 {- R* E' _0 A2 b$ h *
* p" T3 }+ _4 s5 f5 ?3 E' A" @ */
/ M1 V' ^+ x _: P public class News {
/ a+ s t- s) B // id, title, content, pubtime, author
* o/ o; z0 K' S; U" b( t! x$ n3 F4 J private int id;
& C, H: G" X0 I2 z9 `0 e private String title;
+ X# r' q; J% I! z4 l+ @; I8 u" H0 v private String content;( h' _+ H+ I# G: v+ [ l0 A
private String pubtime;& Y+ l- t& e- Q" n* V: v6 |2 [
private String author;
) v' p* U T7 } /**! }( M# Q0 T! T; b
* 构造函数
$ g7 c- S. p2 a$ N * @param author 作者* b* |' Q3 F0 Q% S m% P
* @param content 内容
0 s# d, i8 e6 N5 _$ _ * @param id 序号 v: D8 I5 f3 u1 z3 @
* @param pubtime 发布时间
8 ?: A- R/ A( ^ J3 o M y * @param title 标题) h1 I3 c; ]4 }4 ^4 a) Z
*/+ Z- w2 f0 T- C' p
public News( int id,String title,String content, String pubtime,String author) {
0 U6 e7 o% A7 ]0 ]% Q) ]
- V& H+ P% }3 p1 b* }! h this.author = author;/ M( \, ~* N2 x3 f7 [
this.content = content;
- Y, [6 n5 z% _. J* [5 G: `+ }% [ this.id = id;
8 R! q9 k" x: R" q1 q this.pubtime = pubtime;
3 s" D* x+ q1 V* o( B this.title = title;5 J: ]1 R0 }* N) N _" P1 L) ^
}
) @. y) B; k9 ^# R, w; | /**. D- G% V- @ v5 D
* 无参构造函数8 v" }/ p! E2 @' B' P+ ^
*/, L! E; P0 f3 Z+ D$ \4 s: U
public News() {
% `# c# y$ l$ r+ b1 w: t* ~9 v super();& x" O3 k9 D# ?* w) p9 M
}5 O8 O# X( |$ J6 a
/**
! g! B+ F' k+ y$ O! R+ }: m * get ,set 8 I# J: P+ j; g. U6 c
* @return& a9 N5 b$ a2 q- _7 j ]
*/
9 q; M( c6 N$ X0 t public int getId() {
8 W' a3 m& f! J+ R2 O& C2 E return id;$ ]5 q# _" R8 b: n
}4 }- c G% J8 A. d* x; H' ~8 Y& h
public void setId(int id) {
7 E; T& O% ^& ~0 Z' q2 R) A this.id = id;
% f* j2 Z( ^ n1 m5 Z. \ }
! Q) B, C' R; x, @0 H4 e public String getTitle() {2 s3 E5 V* _/ w! n1 N3 v
return title;8 w# @1 L w5 G- h: Z
}
3 M5 j$ w7 Z; w5 ^$ G public void setTitle(String title) {' \6 ^* `) \" ?+ x6 T8 J" f
this.title = title;
& q6 d, W5 p, [3 K }. _6 P% }" b2 M- n1 T: {# q' q7 ]
public String getContent() {1 \/ d* E3 Q8 b# f/ j
return content;( d/ u7 B/ o P H
}9 j8 \, f' W% c$ x- C
public void setContent(String content) {
+ ^2 U2 t- N0 M9 s this.content = content;* n' [8 d% q+ `5 _9 R) T
}' B4 U9 i" V$ ]9 Z6 j
public String getPubtime() { N. J# M" ~9 k" Y
return pubtime;- x$ Y4 s' m: O9 T* b0 w
}+ R2 z6 T) e! ^9 ]
public void setPubtime(String pubtime) {
3 F7 X% P& b: }: b5 ] this.pubtime = pubtime;! ~" O/ S; U7 Q5 M0 C; X; _4 b
}
. M( u% L; E/ z% w5 m, ] public String getAuthor() {' _8 y# V* x- `. P c8 e
return author;
5 k6 g1 M7 f) R, L# j2 { }
; B' o! Z8 O7 H# N" l+ s# K2 }( Y public void setAuthor(String author) {
9 N% m4 o6 i) R) I' B this.author = author;7 {- p! `+ q7 R
}
! y/ e: z8 E ^ N0 D: q8 T+ k; ]# e
}
: H) Z( W/ L5 `. L( m5 H+ [6 y8 t 复制代码 package s2.jsp.zhangxiao.entity;( }0 ^' r6 Y% E1 b0 i- a0 Z
import java .sql.PreparedStatement;6 ?+ C& Z7 O: h, @ o+ W
import java.sql.DriverManager;7 o% j! ^) Z" p
import java.sql.Connection;$ R+ h/ H a. s- D _, D# |8 k
import java.sql.ResultSet;6 G% G, D) S' f2 R
import java.sql.SQLException;: M$ K' m" x& K5 u' Q" o# W
4 G( c! ~0 e/ N/ \
import com.microsoft.sqlserver.jdbc.SQLServerDriver;3 f) f. x4 _' q0 P" }! T
& @2 H5 e4 J" s m% z8 [9 x4 U
public class NewsBase {
9 T1 b. K2 @. c- _) |5 t' i
1 T- X8 x8 C2 Z private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";! b$ Q+ k% l6 I
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";
4 e$ \. y/ L( v U3 G0 D private static final String DBNAME="sa";
7 y6 d* g! G# I- ~3 _* B; V private static final String DBPASS="1234";
% D0 x; ?+ J( E9 C& j6 V5 A : n' O; M' E! Q4 Z. z8 U; l/ |
public Connection getConnection(){
7 O: Z( M) F/ s Connection con=null;2 _! x9 m$ A% z+ e3 W" o
try {
: x- j; \5 p# g+ e5 e- ], S Class.forName(DRIVER);* N; m) N+ f$ J# ]# }7 p
con=DriverManager.getConnection(URL, DBNAME, DBPASS); p* a0 L& P1 ?
} catch (ClassNotFoundException e) {$ }! y& O* H4 {
System.out.println("加载驱动异常");
$ v g) q. R+ R4 U/ t8 C+ J o e.printStackTrace();
5 r1 h* [: A5 @( U4 H" g* t } catch (SQLException e) {
7 J7 L+ }4 k/ k$ n System.out.println("数据库连接异常");
( a* W \/ D' v e.printStackTrace();/ C w* T. l( z9 W( Y
}
6 R! o% ~; ]( Q/ K+ k. {# @ return con;6 ~6 B/ y+ w' F h4 m6 z2 S
}/ B3 v% d6 z, W& P2 h
! o& V6 S/ R' m# ^4 e
public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
) d+ \: m. K$ d" ?! N( i4 d, N& X try {
" R3 k& i- j0 Y# e rs.close();
. g' O0 V! g4 u& x$ U } catch (SQLException e) {
1 H5 @7 l7 ^ `8 \ System.out.println("");' f7 ^4 y0 s2 z% X. s, n
e.printStackTrace();% X* s1 m% ^: y. Y
}6 l8 c) d' S* b2 }" W+ u+ A6 V' s
try {: {+ p# V* F* a9 K4 V' _, k" `
past.close();4 p9 A1 ?6 Y; ^- m! i: \: `
} catch (SQLException e) {( q2 c. V# ~; N9 T( U D) w
System.out.println("");: F9 B1 H; h) E& X. k# c: @
e.printStackTrace();0 m9 H/ n# k9 O( j1 z
}8 Q+ a% H! l1 ?6 Z$ s& m
try {
/ d. h9 R; b( ]1 O6 h* h1 ~ con.close();" W( n! e3 |$ a$ K/ U" h
} catch (SQLException e) {$ e: P# S7 o# Q! r1 K( I! s
// TODO Auto-generated catch block/ `' U$ W3 ?& E% v/ t* W6 J- z6 D. D
e.printStackTrace();3 O" m0 O# c! I- ^9 e5 H
}
3 A! @7 U3 e i4 ]/ ] }9 L7 h q' G: y$ l9 c' c
! r# r5 Q* ~1 t- S3 ]' | public int update(String sql,String[] getValues){
& F) F; Z& G- C; x( Y int i=0;5 ?1 y [( J6 V, n" A# V$ ]: y( n
Connection con=null;0 y6 ]( E3 e$ p! m e8 A% s1 G& }
PreparedStatement past=null;& \, K% O4 A1 c2 h3 e1 x9 {
con=getConnection();
0 g7 G8 G' p/ i9 y- B) E& w try {# S H4 N9 m( n+ _9 I
past=con.prepareStatement(sql);
- y' B0 J5 K1 I. H. @) J% ? if(getValues!=null){; h9 ?& e2 o4 A. t i
for (int j = 0; j < getValues.length; j++) {, H: @; h2 y( Q; \
past.setString(j+1, getValues[j]);
d$ O# W. D* l/ L6 C! h6 H }+ t9 d3 i6 B9 r2 h7 p% S) L$ b
}
3 b" j7 S" O( Y- M! H* N i=past.executeUpdate();0 A9 T0 S: U+ P0 j- S8 G
} catch (SQLException e) {- v( ~7 U2 A9 g* J3 E; T# v7 C
// TODO Auto-generated catch block) p7 u F7 C$ n; V2 y) U7 W
e.printStackTrace();: W% ?9 i( K" N" q2 C
}2 f; S, K" T% l% Y
return i;! r! t! J" }' o2 _- e' |% p1 y g2 h* H
}. T( m1 m, _3 o+ F; _
}
$ ~( A; W; C" s; @ 复制代码 package s2.jsp.zhangxiao.entity;; D/ f. D; ]; k Y1 e5 k
/ H, g5 Q$ W X import java.sql.Connection;- j- N8 R9 X) F
import java.sql.PreparedStatement;- ?8 y0 m* M+ y& Y/ [& O) D. ]8 s* h
import java.sql.ResultSet;- _% f; [# j( o" m
import java.sql.SQLException;; k6 U \; M8 E4 M- U) U+ v
import java.util.ArrayList;
/ q2 x( D8 K8 h; f3 ^8 e# [' c: V import java.util.List;
! n/ L( I2 S! `, }# F7 J & l+ Y3 @( N$ S" W& e7 v
import s2.jsp.zhangxiao.dao.News;
D# g$ E5 k; c 5 V6 Q# ? j# [* @6 {8 O1 Y
public class NewsDao extends NewsBase {' |, r0 r# ?, P% t* I8 {) {8 x
private int count =2;* V9 n+ v9 L3 v9 F. Y' x% N
8 y$ `7 K/ k8 z) N# F
/**1 ~& M1 \+ L2 L) X! `
* 查询总页数
' D6 m: ^2 U& e: h * @return% @ h6 y: D) c
*/) t4 c b n4 s4 c2 j
public int countPage(){
) |( o- ]) @1 H5 B int rows=0;# X$ N( c! x4 U9 L! g/ T/ p
Connection con=null;1 y- O$ W" @5 u5 m( p' e8 ^( L
PreparedStatement past=null;# T. m x& V. p0 o; W" M& @; h
ResultSet rs=null;
8 ] L0 x+ l% L con=getConnection();( q/ |4 [+ x3 X+ e' J5 L# h
//查询所有的的页数 返回受影响的行数
9 s* o F# ]7 o* Y( Z; x" m String sql="select count(*)as c from news";
) Z' {9 C* x1 j9 b0 G5 E try {
* L/ t& E+ _) l; U7 L! f4 k past=con.prepareStatement(sql);
" ~, X- W. D5 o2 k! Z rs=past.executeQuery();
% A; j" ?1 t5 L2 E) L0 I if(rs.next()){6 C+ i5 R4 ?) a% i( o
//将所有的行数保存到rows 变量里& b/ \* V3 }6 L/ y9 o s0 r4 Q
rows=rs.getInt("c");
/ {: a2 R! _9 l3 P1 l- [7 l //如果显示两页 count 代表显示的页数 4 F6 W) ?/ U- X
if(rows%2==0){
4 l7 M/ \+ F Z, a rows=rows/count;9 V7 j" R4 w, o/ `
# |- I* k4 T9 ?9 M5 O2 ? p }else{
% o& P9 {1 f! i: U+ n% O$ S3 r# Z rows=rows/count+1;
, O, t/ {, u6 Z) N6 A }
: w6 A& N+ k+ t" I$ W }- V* ]$ y$ ]' H4 g4 ^
} catch (SQLException e) {
+ u8 |8 H1 B9 z, }( g) m System.out.println("查询总页数方法异常");& d: a) O7 J+ ~/ i& D: ?3 x
e.printStackTrace();
1 p( _3 L+ R$ N7 q }
! }% _" i7 n" C+ U0 M1 o0 { return rows;
. {0 @% o: N$ y8 U }/ j1 Y- M1 Z: Y9 U, O9 \
/**
. f/ Q2 B. T) w- d4 C; K' x * 根据id删除信息/ D0 i2 e( j. T* N
* @param id: Z& L4 P* B/ K1 Y
* @return7 r8 c# J/ C6 P8 t v% A* f8 a7 `
*/' b/ Y& i3 E; g& v7 j) v
public int deleteNewsId(int id){: H' H5 m; |3 P6 z% t! k' G6 L
String sql="delete news where id="+id;
$ w& e. u) I$ ^0 i% J1 Y) W return update(sql,null);0 T ^& f* s$ X( a& o
}' N- M' \$ M; ^! P( [# `# C
/**. i8 L& e; ~$ X! |, L+ i. J
* 根据总页数分页! \2 V" r4 C" o4 A
* @param page
4 j7 ~, |9 X7 `) L * @return1 [8 T4 T5 i# r: m
*/' m' L4 k- Z, }, Y& l( n
public List getPageList(int page){
9 n/ J% A; O. o' A0 T- J* @3 Y% Y Connection con=null;' M7 p) L6 m. j7 R0 S
PreparedStatement past=null;
" v7 \# j+ U+ S+ k9 u4 w ResultSet rs=null;: `$ a1 Z% P: l( _
List list=new ArrayList();
! c+ i; z; J) J: m6 U con=getConnection();
! z8 g6 a* Z) \" B* r& F& w# Y //0 f6 L! v- J# ?
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 b& k; m; z3 u
try {
# ?5 L# F7 N2 s* R past=con.prepareStatement(sql);. ^* [' E& w2 j* F: H
rs=past.executeQuery(); Q7 [2 c( Y! U N, E5 \2 b% \
while(rs.next()){3 J; L2 K5 S! H
" m) k ?& a. d0 Z; r
// id, title, content, pubtime, author; ? X3 m* X* B$ |9 h' s
int id=rs.getInt("id");6 u4 {4 a5 e1 y$ b7 G( @/ ]
String title=rs.getString("title");8 ~- U. @" S0 w1 y
String content=rs.getString("content");
- t) s! U; g3 F1 y. f- R% y; d String pubtime=rs.getString("pubtime");
' [1 k, C: ?% U/ _8 x4 K String author=rs.getString("author");
2 p! s* s2 ^- Y6 u; ]1 \$ w' ~ News news=new News(id, title,content,pubtime,author);
/ {' G$ L, J6 y list.add(news);+ O+ o/ b: y. V$ S# s O( a
}4 ?- }6 H a3 B! b3 f" Q
} catch (SQLException e) {
7 S3 n' S H; E/ X% y3 ` // TODO Auto-generated catch block
& c( K1 ^( J' D0 T1 f; c e.printStackTrace();3 m# i/ T, T+ f% C# k
}
7 R- \% P* P6 }4 E3 j# i return list;
3 U- p$ f; v( x+ l+ _ 2 q' o6 U1 V7 x$ p. R( c0 [
}
1 X0 z3 }* W, g9 U4 _4 m* Z1 D }* z2 s* ^5 M P6 @
复制代码 2 @9 h% v4 I0 t* z1 q) U- y" [% T, O
( u8 K2 E: R6 v 源代码以及数据库下载 : 3 M7 F. W8 v0 O$ b4 t. g! X! o
% [8 e) o; t$ T. ^# R/ J9 h6 `9 y
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :