TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;! X! H$ y( e7 G
/**& d8 t) {% a' A
* 实体类
2 O) c: U* f" l$ Z1 q * @author Administrator
$ v2 e8 _9 {- t) p *
8 J& Q* D7 n2 _, {2 D1 m */2 ?+ G5 J4 E' ?
public class News {
8 l( g3 a: |# \3 d! V4 f. |' g // id, title, content, pubtime, author0 V2 [' q/ A' a, g- G
private int id;
! e, v8 Q0 k/ E; \. T private String title;
. B8 @6 B5 f/ L. i7 _6 K2 x1 W private String content;
1 ~) Z7 e t$ E- R private String pubtime;1 m; }( E# Q" m; w+ C+ X
private String author;
6 x7 e x+ w" k5 @) r, s /**
3 v- o/ C5 E7 s, t0 v * 构造函数' s$ ]7 l6 r. ^
* @param author 作者4 `- Q0 q7 P3 F
* @param content 内容
8 g$ o# b* }' ~, ~4 X2 R * @param id 序号
7 c& T" N5 l# t s2 t" e * @param pubtime 发布时间) y( b9 Y* G) Y# }3 ]
* @param title 标题
" d" m2 c( `, f* q0 o3 X+ ~ */
; U9 q+ e! @, p2 N* O1 C5 d( \% T public News( int id,String title,String content, String pubtime,String author) {
7 K( o. O; v3 M O6 w/ s ! s' B2 F+ n/ o+ [. Q7 p
this.author = author;1 J' k Y& U" J8 u# A
this.content = content;
) f9 f/ l3 |- F4 f2 f2 c2 P* W this.id = id;
8 i4 K4 Q8 {( q' U* ]: m8 A1 Z) a' } this.pubtime = pubtime;1 _9 s& }! }6 ^4 [0 b
this.title = title;
4 U0 g' q3 n) K }
; g: ]: v* y8 V- b0 B /**
3 F8 i" d* T- ^; |% P v2 U; y/ ] * 无参构造函数
8 @' u; c0 ^! X' J* E2 r */
# @( q" ?; p2 g$ @, e3 X public News() {3 U( e; D$ A& ^) M8 j
super();. ]1 U/ _& S! \# G5 ^ a& p
}
& l& C& a4 O3 Q /**3 Y2 ~! s4 l( J7 f& ?
* get ,set , u5 c* u T p& E4 g2 Y
* @return& j3 v' Y% _" c
*/6 G8 h4 r! y9 [7 b0 F4 W) }
public int getId() {
; M4 \. G( W* w! J, N7 i return id;7 u4 M5 C9 A7 F b8 S. D7 H1 s9 e
}
( F( h9 H& x) V9 f) H; h public void setId(int id) {
, \$ w0 J& C7 B, `+ y( ~ this.id = id;
, b3 r- ~" k: b. o, v) H }
% _6 b5 Q+ b7 f- ?6 H public String getTitle() {; ~1 Y. @" s, }0 j# V" Y7 `$ L
return title;
7 h* y H$ c; P* h( R3 Y" ?1 }+ e }! [' {! R8 x% X
public void setTitle(String title) {# |4 V; |1 P3 W( |
this.title = title;0 `; `0 h U/ |8 ^
}1 j; }/ V$ J- x' e+ ]7 E
public String getContent() {
2 c& n% k4 S N! A# _" k return content;+ L% G# S, D1 X' m8 R1 M
}9 s! k% u O3 z. w/ ?0 ?1 {2 \; V4 Y
public void setContent(String content) {4 N7 r: P0 \+ [2 k% \6 u
this.content = content;( n% M: O' D* P
}: o x) q5 a$ Z h
public String getPubtime() {2 j, }- Q2 C; N$ J1 _2 s( e
return pubtime;8 W5 x, J9 v/ ?% s' @7 B: t
}% i5 T! R# L( f' G! N5 R
public void setPubtime(String pubtime) {
0 f a8 p6 j% g+ P this.pubtime = pubtime;* p. w$ k+ z' j! y
}
1 `9 {, X2 H# M" l5 O public String getAuthor() {" _; V5 {! @( S
return author;! Q+ L2 r( g. x: |" A' p% y3 B) Y
}0 ]5 j! B: v B; c: H" x3 F4 {) }
public void setAuthor(String author) {: q4 I' n/ T! C$ Z- m* s$ @. X0 W
this.author = author;
2 v# T' F9 T4 G, `- ]- p" ` } ?7 v* ]$ H6 d2 O7 D6 X' E
9 l* U4 E" P2 _4 M3 u
}
% ^* G# r8 @ `) q O- U: } 复制代码 package s2.jsp.zhangxiao.entity;9 l6 o5 s; q- w2 {
import java .sql.PreparedStatement;
! J! q, T2 o" `/ W import java.sql.DriverManager;6 L* K# b1 c+ k6 \5 S: |
import java.sql.Connection;
! e1 F& h5 K/ O9 A- ~ import java.sql.ResultSet;) |) D) V+ M0 O0 h
import java.sql.SQLException;. t7 I. k7 R9 n% }
" a# _) a9 I7 p
import com.microsoft.sqlserver.jdbc.SQLServerDriver;( L9 i' {1 @5 G' b0 W+ D7 R. o% m0 C5 O
& Y* }0 u9 f+ S" ?' x
public class NewsBase {4 _, }% x. e# ?0 J
& P5 K1 B* C H$ ?6 I) \# ^
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";4 h- E8 o; q9 j' o" h. d
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";
- M. L% X' e$ h. ~' B# @ private static final String DBNAME="sa";9 O( g8 J7 b) c) g
private static final String DBPASS="1234";
1 p2 N1 _9 B6 f" D+ n0 C1 G
+ z9 |4 @& P9 a+ h& k8 Z2 ` public Connection getConnection(){% z. D5 s, B$ t+ T) G
Connection con=null;6 C; c$ C5 x! F' h7 a7 D& t
try {* |4 @! N! M V! o9 Z$ \6 j
Class.forName(DRIVER);
& R( q0 U' ?, d0 w. s( p0 B con=DriverManager.getConnection(URL, DBNAME, DBPASS);
- |0 x2 D. c5 ^8 a5 U" N- y: y& L } catch (ClassNotFoundException e) {1 ~9 M% _; }! D. ]
System.out.println("加载驱动异常");% u) c% K5 F/ I* j8 f) \6 S
e.printStackTrace();+ o5 n- T( j; l8 j; h: c
} catch (SQLException e) {
/ ?- a3 U! ~6 R) ~( }4 v7 K6 r System.out.println("数据库连接异常");
- L+ T2 L! [% b. {+ V e.printStackTrace();
5 l1 [! o6 Q% I/ ^9 W }: y+ [$ R7 P( {- Q1 u$ ~2 B
return con;' k! a6 s* S2 C, p
}
& w/ I B ^9 z# }" ^2 M# p
, g$ q) l7 S0 M public void colseAll(Connection con,PreparedStatement past,ResultSet rs){
7 ]6 y8 ?& L1 S$ Q7 y7 O try {
' C! p9 ]/ \& ^6 s7 j rs.close();
! r3 ]7 p6 f' U# H6 c } catch (SQLException e) {
5 g+ B( _5 F! h System.out.println("");+ e8 S0 l8 j' b2 Q
e.printStackTrace();
( G; z) A P c& P; Q+ f0 N, T }1 x6 I" r1 [# G, b" T
try {3 q, N: ^ J7 M5 a! z9 P
past.close();
3 y( \7 R4 R" I* f& j } catch (SQLException e) {
, X6 ?& t6 s b& c1 a& ~ System.out.println("");
" s+ e0 d' d+ |+ `* i e.printStackTrace();
0 |, U1 J1 p$ b }
# f) g0 z0 l% S- z6 X5 V try {; r# U# e- t9 ?( X
con.close();
/ j" q" g; \# t( z* F7 I* [$ j } catch (SQLException e) {
8 b! v3 Q" k' ?: x) y // TODO Auto-generated catch block
' V6 y$ q W, O) { e.printStackTrace();. C9 n1 e9 o- U
}
; |) c8 K" I3 y* Z4 _ }7 K6 Z! f6 ?! _ \9 R! ]+ w& F* C
; ^: c: A" Z, w public int update(String sql,String[] getValues){4 j- s: N% y' Q
int i=0;
- e* e. l, k$ o* S: L/ L Connection con=null;- E% d4 K5 g# n' T
PreparedStatement past=null;
7 H/ ~2 u! W: V2 i0 H con=getConnection();3 b' S0 n1 q, n
try {
) {9 b' V: b% \( f( I6 S; ~ past=con.prepareStatement(sql);2 k; W* H- L8 T4 V- N) E
if(getValues!=null){/ a0 l- ~; i. |$ _ \
for (int j = 0; j < getValues.length; j++) {9 f0 u! m* ^# x2 k2 w; T l* O
past.setString(j+1, getValues[j]);2 Y, L/ r/ {* J, x7 N* X
}; \5 [( V4 P& `
}
) ^" [. X5 b8 C. f T i=past.executeUpdate();1 Z5 g- [- h$ a: @8 O9 _9 u
} catch (SQLException e) {6 z$ e: b& k- s) q2 e
// TODO Auto-generated catch block0 }. a6 a8 X6 t) q3 E
e.printStackTrace();
) j8 x' W" v; {. X* R }
3 C. ?3 [9 t5 P) X- J return i;
4 w9 ^ I( W U }! T n* a! f' Z# b
}
4 _7 Z8 F: x! l7 y 复制代码 package s2.jsp.zhangxiao.entity;& u- H. ?/ Q# @) M
/ V; ]/ i a( D1 ? import java.sql.Connection;
4 X( |4 D; i( f& k$ n import java.sql.PreparedStatement;
2 |: X A" i1 \ r import java.sql.ResultSet;
. m ~# l% u1 U! @ import java.sql.SQLException;
" m0 p% H: A0 n- d7 C- ~/ v8 W import java.util.ArrayList;
/ @4 `" F8 U9 ]: H import java.util.List;( Y% _8 G$ @( ^7 ^: l% M$ f
" ]+ k9 s: S; X; S3 o3 D/ J3 T$ `5 C import s2.jsp.zhangxiao.dao.News;
; K9 ^0 k) A/ y& v ( {3 y; O! `0 y4 d4 v. ]7 l
public class NewsDao extends NewsBase {6 t4 E- @% @" A- P* ^9 l% s2 k
private int count =2;
! C* i0 V* [* p; Q 1 B6 i$ G2 b, b& T9 [2 |
/**
9 N) x7 i0 @5 E, G& O * 查询总页数: U! M! o8 s. c( @% I
* @return
6 I S' A! ^4 s */' S$ {) n6 I5 b! V
public int countPage(){
' U9 M9 j. `4 }" `4 f- ] int rows=0;( K0 ~0 D& r$ `* u
Connection con=null;
. J* T* ?: R A' Q" \- x PreparedStatement past=null;, x+ E" o; Q- T7 Z! J: ]0 y5 p
ResultSet rs=null;1 T- W3 O! D# \- G
con=getConnection();! B( I: O$ k* _5 x! e/ }
//查询所有的的页数 返回受影响的行数
$ j5 O' j- G' Y1 L String sql="select count(*)as c from news";( \- P& Q) [3 J0 p4 Z' B
try {, I# ^' T# ]! U! U0 u. _6 l
past=con.prepareStatement(sql);, B, P' E+ J U8 w
rs=past.executeQuery();
% O) ]' W0 y! k- T8 b I if(rs.next()){1 [8 e! r" \8 c2 u" c0 y/ s2 s b1 d
//将所有的行数保存到rows 变量里
4 V5 B' G" C2 p; d3 I rows=rs.getInt("c");
; d% R9 D. @" n& B! K6 I2 R //如果显示两页 count 代表显示的页数 5 [3 l! ~1 O( s2 D
if(rows%2==0){- A) ?4 z$ y3 a" c
rows=rows/count;
?6 s0 j3 h/ c4 F - X4 Z2 @- L! _8 Z
}else{
) V2 y: y( l1 u8 g3 z rows=rows/count+1;6 B) Q; N" o3 O. }) @! S
}' d$ m% j; }' E% A
}( H0 l( ^$ J% j; n% [$ ?" x. z$ \
} catch (SQLException e) {2 n) @( V1 g7 E1 K) {5 f
System.out.println("查询总页数方法异常");
/ M8 w% u1 W- i7 q( n" c e.printStackTrace();8 H' r8 O/ {8 V5 g9 m% l( [! G5 w
}5 W( L5 T G0 Z9 }% S
return rows;
; w7 @* m6 p! E: p) p) | }' }* W/ s7 U' ]! I
/**
, t( T# G. G' e: E * 根据id删除信息* d* B; P: W8 m2 M" o4 x; o, n
* @param id
: D' o$ P, ~$ u; p/ O Y+ ^& G * @return
! @, z; U" ~# u; ^5 V+ I */
7 C5 F) l# U. x/ o9 O R) }% j public int deleteNewsId(int id){
( Y9 Z0 {% Q; V ~& i String sql="delete news where id="+id;
- K. R+ ^5 E9 m* Y( @ return update(sql,null);) r7 g6 I% z3 O
}
, Z" ]+ i m$ _- k% {+ f! c /**
2 e. S' o* k x! r7 e( ]" F! h0 s * 根据总页数分页
9 u6 H+ \ z* e. O2 n; B * @param page
* r9 d4 L6 T1 P! p+ ^ n * @return
L: a7 n( Z3 Z/ n */3 d" `" j1 }# K! O% E1 t: N. X
public List getPageList(int page){
* g( c4 U. T" v2 r5 c Connection con=null;
8 r7 e O" {( L4 T. o PreparedStatement past=null;
9 X0 g/ |! I. U5 l0 d+ k$ a ResultSet rs=null;" B/ t, w( W; F t$ Q# j7 p
List list=new ArrayList();
4 j! j0 A: ^. l' h: Y con=getConnection();& W% D: G& S) q' q+ z. Z
//, V3 |4 E$ |4 X0 q, N& y6 L
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 t% D; i6 ~7 w2 J: J0 [6 F try {
% F: L* M- L' x' @8 D past=con.prepareStatement(sql);+ R3 I. X, h: T7 @1 u0 k
rs=past.executeQuery();
3 V: ~3 M/ I1 L" W while(rs.next()){
& h+ H5 ?5 s% T: Z 7 R+ F4 ?" T& C. h( M
// id, title, content, pubtime, author
5 @4 ^! Y& f1 g! X; A int id=rs.getInt("id");) f3 y& g" @$ q! ~5 m- `2 ^: l
String title=rs.getString("title");2 c. P+ g5 u: \& i' T
String content=rs.getString("content");6 L, m0 U0 @5 V' _, R2 t
String pubtime=rs.getString("pubtime");
1 ~' G7 v+ W" \. j String author=rs.getString("author");/ u+ Z, [5 q& @/ t8 S" w
News news=new News(id, title,content,pubtime,author);
+ d/ j7 Z; T7 o9 L. Z: l1 H list.add(news);
4 S6 z9 H" h+ ^( i% [5 [, m) a2 } }
- P; i) |$ ?! s$ g) E+ E } catch (SQLException e) {% A( y# b- H! A9 a, n" @$ }+ q
// TODO Auto-generated catch block
* d* R' |3 f) C; g# B8 J G; v8 K e.printStackTrace();
$ u1 e3 k9 x O* z0 K0 a }4 W/ s% Z, t/ d, p
return list;3 t8 P, B& k& `( H c4 i3 i( A( M
, `1 H7 n) O; ^/ z0 n* B0 {8 U) \ }, e8 l8 a9 O" Y# w7 ^
}
% l; k+ U& h9 j( ? 复制代码
6 S- l$ c, G9 y/ u3 D% [: ~+ h$ [
: Q1 R. {) N% `7 {2 s9 B/ i8 \ 源代码以及数据库下载 :
! n. u& K3 K8 ]9 e1 @
# i4 X. L6 a, X
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :