该用户从未签到
|
- package s2.jsp.zhangxiao.dao;6 L5 }: \" O2 B+ d, }3 k
- import java.sql.PreparedStatement;7 k+ X! q. ~. m7 f7 G; x
- import java.sql.ResultSet;' b( Y7 M5 l" S" C' Y3 y6 k, X; d
- import java.sql.Connection;: f+ ^: A# @/ B/ T Q8 d
- import java.sql.DriverManager;, J6 K! _! M: U5 v9 g
- import java.sql.SQLException;
/ R/ L" t) I" z4 _2 A) Q: E5 Y - import java.sql.SQLException;- k3 T* W7 z* f
- % @7 ?4 e4 ?* [$ }9 q( C: U$ \% D
- 0 K, F" b/ V# S" u: r, a
- 9 i2 ~% x. }4 G5 y% B8 V
- /**
+ A1 b4 @: a+ u1 I" g# Q# I+ C - * 连接数据库& I0 l9 ?& e4 F- q2 N
- * @author student1* \9 C/ r8 c) h! \3 L' V
- *
U5 ^% T+ l) Y% l+ I6 ~- i - */* U( o2 p: }; x, z( ?# u
- public class BookDao {! R" q% q! A& k
- 8 E; q+ `: J8 W1 x# u% f4 e3 F
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";( X9 A+ z% S; T p, E
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";( M* Q+ i" |$ S3 u/ o4 E0 v
- private static final String DBNAME="sa";, [6 E4 ]9 U0 ]' H1 ^8 `! l
- private static final String DBPASS="1234";( A! u; ]9 v: E$ U- i5 i- t
- , |& I0 f3 ]9 J5 Q- L2 K) ^( `6 F
-
7 D8 w" T! T& T- l7 A$ x: X1 z - public Connection getConnection(){0 b! _4 U: X- l% K! z
- Connection con=null;6 M* `, n& _) U: q7 r
- try {
) L! \* o1 Q8 u: V: ~" p2 p - Class.forName(DRIVER);: x8 f7 _ B3 b6 y! a' o
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);
5 l x3 |$ Q, O# r+ e' T! z5 `( F - } catch (ClassNotFoundException e) {6 ?0 i. y9 A4 `( U6 P" \. {6 F
- // TODO Auto-generated catch block
5 x8 i+ ~1 f' I! z" B8 q, H9 n - e.printStackTrace();
1 m+ @* l, u# h; t/ H5 e - } catch (SQLException e) {
1 M6 O; i0 @( G! c) P) ?! `( ` - // TODO Auto-generated catch block
j3 w8 p' G+ B; q5 _+ M9 M1 u; g* K - e.printStackTrace();
J7 e( L# G/ ~6 _% I/ _$ I - }
, T# X' _3 @ o - * u, C" E) v6 {! y
- return con;* Y2 P8 F. Q. ]
-
4 \) a% o( @' p. S7 m/ v% q: u - }
' l2 {9 w2 x: x, w4 H R# B* p - public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){2 q# u8 F; R' U: g c9 r
- try {
" I, P- J H0 ]$ z7 @( k. x) t - rs.close(); i3 A) y3 O* a* q& N
- } catch (SQLException e) { R5 r# l" S& g5 ^3 v( ~
- // TODO Auto-generated catch block8 D: b$ ^# ^" U% w! O6 [
- e.printStackTrace();
" C* r% Q- M; y; q4 }* n: E - }- Z3 \5 E/ d4 c7 L8 E( {* P' x
- try {/ b! W( N/ W% M3 ~3 d. t- l
- past.cancel();* k, l% K3 i. M7 ] T2 J2 r6 Y
- } catch (SQLException e) {
0 i5 d8 P& ?' d, ^, ~% D0 E1 ?# ]5 _ Z - // TODO Auto-generated catch block8 N; z1 f3 I( e- |
- e.printStackTrace();+ ] |* f7 n9 J. D2 t% Q
- }! p% ?. R4 t8 J
- try {5 f" Q" {5 s- Q) S7 b* G
- con.close();
1 ]" x3 H; o4 X4 `9 D - } catch (SQLException e) {
" @! _% g2 B' C3 h2 n- U M6 j - // TODO Auto-generated catch block6 } H( n2 S4 N t9 a3 I/ g
- e.printStackTrace();
' l! g p( ?" _ - }) O0 o( H0 i( o3 h& |1 r2 \
- }
% a5 `) y; u |5 z( @% F2 D -
: z& F/ ]4 G6 `6 t* ]/ U' s6 I - public int update(String sql,String[]getValues){
5 V' d+ _% b, g3 L% b0 _ - int i=0;4 h, h+ z: e: D# O: ~
- Connection con=null;) }5 I. N9 M# @+ ^/ S
- PreparedStatement past =null;0 }0 d7 i0 w7 y9 w5 V
- con=getConnection();% ~% D8 r/ M |( u
- try {
( R" z; F+ T& }/ x4 j - past=con.prepareStatement(sql);- o' L; C) l% s& B0 P- B
- if(getValues!=null){7 e' T# I% ^1 R+ p) E7 Z
- for (int j = 0; j < getValues.length; j++) {. h' Z8 e! v3 s* w) B9 d
- past.setString(j+1, getValues[j]);
, ]& _7 j$ X k, B; S - }% X1 t! Z* S$ B9 z% C
- }! ^! f6 A: q. ]% X
- i=past.executeUpdate();
6 Y7 `' z6 O$ ? - } catch (SQLException e) {2 K& v4 n+ }8 Y+ Q1 N7 |
- // TODO Auto-generated catch block
+ G* O0 k6 m% K, M# f, n2 J - e.printStackTrace();2 B3 p9 ~ d/ Q0 N- d
- }
6 R) A/ }, \& B A2 i' h - 3 W. O; w) x( M4 C
- return i;
6 L9 I2 _( b b* y -
# D* F8 r3 Y: K$ t- v& w7 j - }
$ n2 o9 C3 s [7 l; w9 D# v - }+ w( R' j( C9 m
复制代码 2 H/ |- A) j. Q0 w7 a
" m4 b1 r- X5 L/ {$ o( |
- package s2.jsp.zhangxiao.dao;
3 z* P1 l" ?& U) L' t4 |! \2 _
y' X4 S6 P/ ~, P9 y @8 e- import java.sql.Connection;
& @$ t( L0 k/ ?. V1 b' x) c/ \ - import java.util.ArrayList;' U( \5 g1 V8 m* h
- import java.util.List;
& J7 d% {& y% [. l# `* y - import java.sql.PreparedStatement;$ l+ `# m9 A# p" C- U
- import java.sql.ResultSet;
; \& [+ j* L7 N9 ?9 U - import java.sql.SQLException;, u4 r9 \0 i e* h) A
- 5 Q- ]; f- v! z. Z% ]. r% T
- import s2.jsp.zhangxiao.entity.Book;( y# }! y/ i5 _* z. G0 U
- 9 A3 d- U* w& [/ h
- public class BookImpl extends BookDao {* e- C0 C6 N" \( P; C9 C
8 e0 z0 ?" `# E- , v' R2 Q2 g% h7 c6 s' G E
- public List bookAll(Book book){# H! L6 T$ T3 H- ]( Z/ O) x& T
- List list=new ArrayList();- N/ e3 ^4 c! A7 B% \- ]
- String sql="select * from book where 1=1";
( b, A$ a0 b9 V# a5 m6 ^0 j - Connection con=getConnection();
9 A4 [2 m. _; ~: k3 y - PreparedStatement past=null;
0 f) {* {0 m1 Q+ Z7 N - ResultSet rs=null;$ w) c6 K8 H. e' M( v
- if(book.getBook_name()!=""){
2 O, l' k/ P; {6 d% {+ l) h) S - sql=sql+"and book_name='"+book.getBook_name()+"'";* l/ U9 y% E S, q* I
- }
& }6 m( h: S$ h/ W - if(book.getBook_type()!=""){( e5 X/ f' h! v# t3 \
- sql=sql+"and book_type='"+book.getBook_type()+"'";2 F+ F5 x4 Z) V+ B$ b
- }2 R, S' @5 A( P9 ~
- if(book.getBook_publish()!=""){* `4 i# z! @$ q6 f
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";
' p& Q7 r+ ]6 Y4 a& l - }
9 j$ Y/ ?1 T+ W9 ^ -
% Q9 l+ I! T5 K - try {
. n& C; R( d9 x5 R+ Q - past=con.prepareStatement(sql);7 ?- r4 A Z' X; ?6 w W! I
- rs=past.executeQuery();1 q0 H, y6 n% I8 I% c& }5 X0 Z
- while(rs.next()){( F6 ~& q" J+ A
- Book books =new Book();
5 o4 c" Y% J# q+ W2 z' b - books.setBook_name(rs.getString("book_name"));/ ]" s: \) `# @0 [# G! @
- books.setBook_type(rs.getString("book_type"));
( J& T) D+ U: u, d0 Q - books.setBook_publish(rs.getString("book_publish"));% X9 g+ ^% f: h' o& D E
- books.setBook_date(rs.getString("book_date"));
_6 I0 S- }/ D( j - list.add(books);
5 g( N ^" t1 a: [+ ?, V4 g# Z - }
, u8 ^# V. n( W0 s- U3 ] - } catch (SQLException e) {' }) ]. |3 R$ L( U+ a0 [
- // TODO Auto-generated catch block$ L! V" a7 d0 Q4 A7 X
- e.printStackTrace();
6 A+ a: p- Q5 n1 I: ? - }
' T% V& P3 l$ A- l e - return list;
* G) Z2 @* h) O: G -
4 G1 [# Z7 ~" f - }
* v2 F5 P1 g5 K \1 b" \ - public int bookInsert(Book book){
9 w- P% Z1 f" u& B# Y- ~ - String sql="insert into book values(?,?,?,?)";2 r* s0 i0 r( q) W$ l+ Y
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
3 T: _4 K% S* |! X2 `6 ~ - return update(sql,getVlaues); ( l& {+ j1 E2 h! O) y& {7 _1 F
- }
0 `7 ]5 V b) v! `8 W - public List listtAll(){) a% v: S5 r- G# d0 Y
- List list=new ArrayList();
% q# Q4 m) M, k - String sql="select * from book";
- c6 S. o; q4 V/ N - Connection con=getConnection();
0 _! N3 g7 M8 h" Z. b; X - PreparedStatement past=null;3 e* R1 G6 F8 f4 y$ `5 T& m4 [
- ResultSet rs=null;! W- G5 [& ]4 [# l/ d; x
- try {
4 L% {2 [% M3 }9 h! u - past=con.prepareStatement(sql);/ V! b! \+ D7 m5 S. K8 F
- rs=past.executeQuery();
" }! g B# V9 o/ f - while(rs.next()){* N4 J c; ~, \1 z% z* J1 Q6 \0 ~
- Book books =new Book();
1 \8 b$ a9 g( {6 S) l - books.setBook_name(rs.getString("book_name"));' n* \: d% z3 `
- books.setBook_type(rs.getString("book_type"));
( E* I" {8 l8 } i: ~5 T0 M. H - books.setBook_publish(rs.getString("book_publish"));
! y" T0 O/ X" _, H$ z8 I; y - books.setBook_date(rs.getString("book_date"));
% D6 Q) y/ |7 c) S- y6 f - list.add(books);% _: N; U; L% k; ?8 ]$ _7 I" @/ V
- } S) a- a# H( i; b- M
- } catch (SQLException e) {. h' Z6 b9 r3 g/ Q
- // TODO Auto-generated catch block
0 {( {- k; U& Q8 i Q( u+ B - e.printStackTrace();
5 d. P0 }8 v$ l0 x0 t4 k - }) ?, P; U+ }9 ^/ [
- return list;
- P% u; k8 n5 s; P - ! w- Y* q4 W5 `
- }$ l7 D! H! I6 a7 M. [
- }
( Z: W O- l- P8 }) @
复制代码 9 x8 I5 {" ]% i
8 j' \# I! j! P. ^7 L0 x! r5 s1 G* P# G2 o
|
|