TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
package s2.jsp.zhangxiao.dao;
% `0 _5 U0 e- O0 y /**
Z2 Z" ?' u1 @ A% }1 |) Z5 O+ {% c5 s * 实体类
% Q) z3 V3 M/ I7 Y * @author Administrator- g' G! |5 ~1 l
*7 V3 y& ^4 A' L" [4 i) x% D
*/* P; X; t. R6 o& a! v
public class News {
( X+ z, s0 k! D2 L // id, title, content, pubtime, author+ f7 {+ ]# u5 }3 V- l' H
private int id;
: j% g' `/ p( A r: ]/ H' {- I' O private String title;
" t0 g8 F& ~8 K* S1 P3 L private String content;# n: f% R- |3 G w4 \
private String pubtime; I. c) B7 \( e; v3 n9 P
private String author;/ W- L- ^# d4 Y4 m# d/ v& F
/**
~0 {' k2 y9 E * 构造函数0 v3 Q! `0 m/ X# k, U. \0 E3 j- ~* n- ]
* @param author 作者
9 p* L7 I2 j2 m4 j * @param content 内容& U5 D1 }# b8 c/ `* ^% z
* @param id 序号
( m3 C4 u! d' J$ o5 o) m+ k1 G. Z * @param pubtime 发布时间1 Q. s3 D$ T2 }' W1 a
* @param title 标题1 S, N9 Y2 |7 v; M" t T" C
*/
3 u" ~3 M" ]( u; Y/ f+ m public News( int id,String title,String content, String pubtime,String author) {, t' b' `/ e1 }; `
; |3 ?3 z' j3 S% x this.author = author;
" @& w) q. g2 n5 e this.content = content;* A0 j" Z6 E' m# _
this.id = id;
( X( z o* o: D1 g( w& Z8 E/ J this.pubtime = pubtime;
% H. x5 v1 M/ h7 `. H0 b1 ] this.title = title;2 D0 K. i3 r7 h2 l7 e" A
}
& t2 B3 k* A9 P h0 g" g /**' j# m1 _5 u" g6 a- B# Q
* 无参构造函数7 A$ d; V% s1 t$ `7 @9 }8 V z
*/
0 C- B4 @/ }2 R public News() {4 i+ D, _' N! O! H
super(); H+ m& q0 b3 m: {" Q; Z7 s" n! z
}" F; |2 |9 |. Q% S: A) e$ U
/**) ?% }5 }: | C/ D \+ j
* get ,set & N2 R5 _4 n" c6 |
* @return' Z; L6 F! n8 Y. P
*/* G2 Z% j+ l0 t# _' \+ y. U& y: b
public int getId() { j3 y* B/ y- K2 b; Z- M6 R/ ~
return id;
; \6 \% e* w7 O( c }
+ r1 L8 R P- {" P; N( ?# d" ` public void setId(int id) {
r- A: W- _* w2 r this.id = id;
% v+ D2 k' f L* A2 O0 W3 F }2 @, s2 }, Q' M. p4 M
public String getTitle() {; \5 T. f q% g; D
return title;
3 h0 I5 l! Q1 W, S }
2 t9 s* P, J; i( G# W1 _ public void setTitle(String title) {( c4 c. T- Z2 j! g0 J
this.title = title;' L9 k: B/ H1 N4 P% `& z
}
% @; N# D: U+ v0 v- O# \8 \9 g public String getContent() {* w; E6 {1 ^$ `5 |8 X
return content;/ {/ q( T5 [ |6 ~, ?( B8 A/ a) X
}
5 D& I, Q2 p! H public void setContent(String content) {, t* \" y- T" ]5 m8 Q
this.content = content;
9 K5 y' S% n- E }. O# h9 A) y4 c4 U" w
public String getPubtime() {' Q$ W* S; }+ w1 `2 s' ~, C( j
return pubtime;
: M0 f. j0 o2 C! G& U! H; F }. _$ Y/ i2 z) C
public void setPubtime(String pubtime) {
; E2 B5 \$ J3 \) Z: | this.pubtime = pubtime;: j4 @7 Q" A v; n
}8 d( S; X" i9 Z1 K% z2 k+ K
public String getAuthor() {
5 C1 b. S$ c2 K3 R. ~ return author;: G2 n: ?8 m7 r* I
}$ U) @. J$ y: ?
public void setAuthor(String author) {: x) B# j3 J6 j8 e( B
this.author = author;4 t8 A6 t- \3 r
} V l4 ]9 \- `3 C
/ ^ j3 i2 C5 p8 D3 n }
+ U9 e, h& Q: }* t' p5 u 复制代码 package s2.jsp.zhangxiao.entity;
, N3 }) X9 D: q( b; l import java .sql.PreparedStatement;
% y C+ @% }- I8 j8 D import java.sql.DriverManager;
4 k6 c) L0 D6 e7 h import java.sql.Connection;, \% d' {, h) f2 m3 o
import java.sql.ResultSet;1 w; z9 i4 h: Y2 P7 g, ^
import java.sql.SQLException;
@! n- O* ]1 z2 p4 m$ S. `$ @ 5 p& S; t& S% Z7 t1 P, M
import com.microsoft.sqlserver.jdbc.SQLServerDriver;, F- H7 D! b3 H( E1 k4 f
4 K+ n/ f1 `, p a7 l: d7 ]7 a public class NewsBase {
' P* c6 w2 _" M) q% U3 _# A 0 M5 E: Z0 T9 o. S/ i, E! P
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";% F5 T" E) i' g7 ?' ]
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=news";" J" a" C0 y* o/ e7 J
private static final String DBNAME="sa";0 v& R6 P: a+ {2 ]# u4 S* }0 @
private static final String DBPASS="1234";
8 B/ |2 P3 t- l& s9 Z# |0 F5 I
+ V8 I1 v. ]1 }- y' I public Connection getConnection(){8 {8 y7 D6 m" d2 f
Connection con=null;, ? W1 y" M9 n5 E0 V, U% X% d% Y- n
try {9 Z# y; b% |; c6 s1 r% \
Class.forName(DRIVER);3 u' ~. Z, G$ {* b- W. @+ M6 N" [
con=DriverManager.getConnection(URL, DBNAME, DBPASS);
8 _* L. S# v- p/ m7 H } catch (ClassNotFoundException e) {" y/ a( j# w' b5 M9 B, p
System.out.println("加载驱动异常");0 t! t% s2 I: y: G4 Y1 G V4 B# j
e.printStackTrace();0 S* U) x4 _* i& ~+ r' E
} catch (SQLException e) {
* }) _2 |+ i _( m- h" ` System.out.println("数据库连接异常");; s5 ~) m' W% e8 K
e.printStackTrace();( o C. [9 e1 Q: ?5 v7 S) H* N
}
8 c: |2 |+ L' E( C) Q' N0 W return con;
& d+ Z9 a6 |' y, x; r, r' m }
6 V, I6 c8 C5 H9 W. ?0 h9 B 2 C& ^, j: A& k, n6 |- G V+ E; Q
public void colseAll(Connection con,PreparedStatement past,ResultSet rs){! T1 C, r7 P, F# l1 l" E8 y
try {
5 e; j) M B6 \% {8 G( V6 e1 j rs.close();
0 ^ v2 I# J8 X" {; M. V } catch (SQLException e) {
I, l8 E9 \# ~( P% E System.out.println("");4 a, L' F7 S. l0 E. f* w% g
e.printStackTrace();
( [7 N$ `1 V; W7 A; H! n3 p) I }
: |8 A3 M+ y# h/ ?% H( ]/ M try {
5 G% k4 d: Q: | past.close();
: l) o/ m2 s( t+ Y& p+ {) K } catch (SQLException e) {: {0 C3 O9 q6 K3 G) |! g! u
System.out.println("");
S4 p9 x0 `# R) ?; Q% M) c; q e.printStackTrace();0 D% e. e7 {$ n8 j
}
6 o* n H' Q \; u try {# a& }; V3 A! P5 f; L/ G B
con.close();
# S; m }! u' @; B3 x7 |7 j, C! ?; f } catch (SQLException e) {
5 _/ i+ ]/ F5 W" v // TODO Auto-generated catch block+ N1 ~4 D* R# ^8 n, Y
e.printStackTrace();
% i2 j- T. l; k }+ R" S, I% h# `6 b
}
& p0 Z# `( E% x) d4 u, ?
4 g4 H+ k, T' j( Q2 m5 i0 | public int update(String sql,String[] getValues){, e9 w& I# t8 E5 u6 r$ R
int i=0;! {3 O) R7 G. ^
Connection con=null;7 L8 E( j3 {' R% b) L, m
PreparedStatement past=null;4 c. X5 D" p: Y. W6 p9 F) u( A
con=getConnection();5 \; ^) p* Q6 F- R3 F- `$ z
try {$ |/ S* a x, `8 k6 h/ Z. w
past=con.prepareStatement(sql);
, Q8 T- f1 n2 M+ |* { if(getValues!=null){
1 Z! Z; @8 Y5 B& a7 Q& v7 ]4 Z% m8 b for (int j = 0; j < getValues.length; j++) {; U' l1 t. ]4 a+ i
past.setString(j+1, getValues[j]);7 M1 u( ]$ _8 M0 X0 W5 M
}2 L2 T. h" A0 s* y# R- a! k0 [+ R
}- u @; R3 V! c
i=past.executeUpdate();( q& N u8 ^( |" t0 i$ s# a) L3 ]! l
} catch (SQLException e) {! J) o$ F R A {0 t Y! U0 Z
// TODO Auto-generated catch block
2 Q# B; @, q+ F+ l e.printStackTrace();( u, @& L8 s3 W& i2 L6 s! d8 j
}
: h: a b- w* ?& W return i;
% v! \& e9 y J l$ i- H# ^" h }( ]! c, b/ o7 u1 Q" N
}
' T9 o9 O0 E/ [7 i# [; q 复制代码 package s2.jsp.zhangxiao.entity;
" H8 E1 s2 ]: D. F8 t
6 I U* q4 ^+ x! w2 i import java.sql.Connection;5 ]0 U+ V' O; x. ~2 o6 |, h/ v" v
import java.sql.PreparedStatement;8 I* ]9 S, l2 p1 u: N- f0 [0 y+ t5 ]
import java.sql.ResultSet;
; I( [; s' x5 O z% x import java.sql.SQLException;2 m' L" \# T& ~) d5 V
import java.util.ArrayList;) N. E3 U* ]: r8 J
import java.util.List;
! y. C2 n0 G5 C: E( r: F - L6 b0 {' }7 Z9 D8 D
import s2.jsp.zhangxiao.dao.News;
7 c$ \6 X% O* g 3 p* K/ |* M1 l' N
public class NewsDao extends NewsBase {6 }9 l+ |8 x% S. T
private int count =2;9 |/ y. {8 ?3 V4 a( ~% G
) S5 k4 Q9 b4 g5 p; o
/**$ P/ q" V. J: v
* 查询总页数2 d: `+ F+ p+ p' a0 [' u
* @return
* t' v" N* {* Q& t7 _# S. l" j6 N */! x) [3 @7 m; |) Q/ n9 W9 V
public int countPage(){
: q+ f1 b+ I$ X: N! O; a+ M int rows=0;, J2 V" c7 l( y& J- x+ C
Connection con=null;2 |# Q3 B) V6 p& G. h. D
PreparedStatement past=null;6 H% I$ C1 x! p3 C5 J
ResultSet rs=null;
1 T3 Q1 B: G; f: p0 ^; c con=getConnection();
8 Y5 r d7 }$ q1 K* Z$ |7 Q //查询所有的的页数 返回受影响的行数
( N1 x$ W! t5 v7 a+ Y, x x String sql="select count(*)as c from news";( a. f3 \' ?+ D! J1 q7 A9 E
try {
) D& J2 @$ M V& G3 h past=con.prepareStatement(sql);. n- a/ M. |5 x$ S; w8 T" p% }
rs=past.executeQuery();
4 F9 e) R+ p3 [% C. T- |" r if(rs.next()){
0 e4 ^) U: q& R //将所有的行数保存到rows 变量里
+ n: Z, {- Z2 v9 [ [ rows=rs.getInt("c"); ^/ {: R+ e4 k& b$ U1 e4 ?- c4 W
//如果显示两页 count 代表显示的页数
- c6 C3 E" r4 h/ i6 | if(rows%2==0){
' ]+ ~# C0 ^8 b* F5 t& ^ rows=rows/count;
- n9 ?% u6 l- _ 7 Z) P9 K/ _6 E, V' _" Z: v- n
}else{ q2 d3 q- y' R* Y
rows=rows/count+1;% p4 U1 P3 o* y# \( p
}' d# @1 L: o- N! ?- E
}
6 Z7 \( P0 ?. G } catch (SQLException e) {
$ k& z f1 ?$ S+ p System.out.println("查询总页数方法异常");, f, [# `4 {' B* M
e.printStackTrace();
7 i3 z. E$ t5 ^4 W8 O, e }
& E1 V6 Z8 W5 }) h) _, r$ ~+ i return rows; J' e0 O3 q1 ^( K
}
, t* u% Z5 C5 P: A5 ?7 M /**& n* ]& |+ j) N6 P* {; i! {
* 根据id删除信息3 g2 P3 @& L6 O7 n. e$ r- g
* @param id
* F( \! e3 z( d) r1 t * @return! C( z: L/ F- b" m! v/ j
*/
4 o% j/ M' Z5 c+ u public int deleteNewsId(int id){ V- w4 d! P) {
String sql="delete news where id="+id;; ?2 {6 |# y. M4 Z9 m% z
return update(sql,null);8 i( U+ h- t5 u& l! c* h
}# P7 t+ G0 i, h6 C, u& [ X
/**
, Q ~0 ?0 T6 p! i6 p * 根据总页数分页
! `' t; m* i2 K& X+ u * @param page
" c9 x' c/ ]' N3 | * @return
1 ~1 |& i# U2 o( v/ Q6 O */
; G4 h7 _( m. I. U, n public List getPageList(int page){
( y- X4 M$ f, h( S% T Connection con=null;8 T6 C ^: w$ |! G
PreparedStatement past=null;
$ H9 U2 u& o2 s% Q/ I3 A- s3 ] ResultSet rs=null;
5 Z' [ P% ?# N; e- E List list=new ArrayList();
% L, o8 B2 H( q con=getConnection();( O" k: q9 _' J9 @6 a
//6 |7 Z" O1 M+ 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";- t" T. J R& G1 D2 T1 Z
try {
2 u& r- Q" |; N past=con.prepareStatement(sql);( V8 y9 S" s2 `7 `1 L
rs=past.executeQuery();
0 Y4 @* l( H- O- p( Y: d0 H while(rs.next()){' j; {& d& K3 c, Z& [
, I/ a l+ @' N ?
// id, title, content, pubtime, author
! u W) x; c3 w" H s int id=rs.getInt("id");1 v$ ] d3 @1 A0 Z) F X3 U- I- S1 i
String title=rs.getString("title");1 w: K9 I# X, O: \
String content=rs.getString("content");% @$ s' e* [( e! s% d. h
String pubtime=rs.getString("pubtime");. Q) x; X+ g4 B/ p) ~1 x1 q
String author=rs.getString("author");- v6 i' x9 c' h5 n% ?" t# [' q' s
News news=new News(id, title,content,pubtime,author);1 w r0 e7 ~% x. H
list.add(news);6 ^1 V8 F* O1 o1 Z# @
}
, l! D" E* \+ j' i7 |9 B } catch (SQLException e) {
4 m( n) V7 _) [" j% D! I+ O // TODO Auto-generated catch block
# M2 k4 }/ X7 h. L, c4 Y" m' L" j9 ] e.printStackTrace();3 u; t0 k+ |) M. {( A) C2 P
}
7 y% Z; u' G; N, m( B( j* T return list;
% o3 a8 J3 d3 h8 ?( g8 W" ^ $ G" o- [6 j5 w# d
}
3 T% f7 S0 M6 X' o }/ ^9 Z+ x- d2 X6 F
复制代码
( A7 |) z1 w: b3 | " C$ y8 z$ c2 ]& K
源代码以及数据库下载 : 4 f% X) `6 W1 X- [
4 t0 n1 {: s, V# M9 z, T
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :