该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
& N4 f" J! S& B; ^# C5 M - import java.sql.PreparedStatement;
3 {" |( N/ Q' i1 a% J% N6 h - import java.sql.ResultSet;' J/ t% x3 ~& k+ j. ]
- import java.sql.Connection;3 K. G7 M: W6 H% X0 `: X2 [( M
- import java.sql.DriverManager;
9 d7 [2 m# j0 _, D B - import java.sql.SQLException;
5 m. d2 k1 E8 _4 j+ B - import java.sql.SQLException;* `1 Q; @5 a, j+ t9 I$ L
1 r; Y+ \0 t- l) ]: I3 X' W& O- c* X. \) Q* F8 F- M9 c: F$ _
6 G2 f8 ^7 x Z$ A$ o, V" _% r- /** b* M5 ]5 ~" J
- * 连接数据库
r: a% q; X5 |$ I% U0 Q+ }/ v - * @author student1# |( M8 Z' r" a
- *$ i: C7 j1 T9 a; I
- */' n' a0 r4 i. d$ w8 r, ^$ e9 V6 C
- public class BookDao {
5 ^& m' F. F- t% S# V) ]' I3 K
1 H2 E: T/ J0 R4 |# w- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";6 |$ P% s9 R7 r% \" }
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";
1 M+ z T9 m6 o- N0 C3 z- T: z. k - private static final String DBNAME="sa";, l- d; a6 G" W; v, _' S
- private static final String DBPASS="1234";
* S) \' t3 j. ]+ t% s' E! S -
. a6 H& L7 a) ~. p! h* m$ S -
1 [ d) g5 W7 g' ?3 l- M' o( B - public Connection getConnection(){; U& _0 M% d7 E. Y# [( i0 C, l
- Connection con=null;' x3 E, s/ ^6 q5 p2 h+ I
- try {. R8 n! I+ L' W# F# X
- Class.forName(DRIVER);1 g* c: n! N9 x0 _
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);
9 X' b, H3 s5 I% ~2 h - } catch (ClassNotFoundException e) {
0 \. ^: ~" v; e% [6 H( `* w+ g k - // TODO Auto-generated catch block
$ ?6 O) E% A, S" P. `) I/ g9 \2 Q - e.printStackTrace();5 o4 ?+ r; u7 Z+ \0 J! M1 H8 }3 o
- } catch (SQLException e) {* Z4 O$ _3 F2 M( ?
- // TODO Auto-generated catch block
. V& k& U% x$ q' U/ p- e - e.printStackTrace();
0 ?, R$ d" r, e5 N0 y - }
% v8 X5 N% U0 D* l" P/ w* Z) e - ( h! h# B K: G# ]7 S; F
- return con;
$ ^& S4 Z' }# G0 ^6 @; m - , d) t# c% P* ]7 b. {
- }/ B) K1 \" p( } ?
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){ Z4 C; D2 y% Z3 v' i& }
- try {
) z1 H9 Z8 |7 Q+ ? - rs.close();. R, P4 s/ x2 ^" v Y5 q9 F
- } catch (SQLException e) {
! T% t2 r, d: X! \" p( p - // TODO Auto-generated catch block
2 ?) S- p- \! c) z) c; Z! l - e.printStackTrace();8 w% j! d3 \7 ~( ~7 m
- }
+ C& J: f5 X- o0 w v - try {
, y, Q( I1 K( V) f$ f H - past.cancel();
, D/ C! ~, Y* P/ D3 \- K' h5 V - } catch (SQLException e) {
4 r4 f. h" a1 K7 J1 f! v9 t - // TODO Auto-generated catch block
2 m O* Y2 c8 m( A$ J - e.printStackTrace();
2 ~" [" b" G7 h4 i. X - }" }$ ]6 w4 F) H1 x" U
- try {
/ b+ W: f. T' B: w# t' Z - con.close();5 i3 ?& c( Y Z( ]9 J1 k/ Y
- } catch (SQLException e) {
8 Q/ [5 e3 Y3 q5 b0 x7 N+ E - // TODO Auto-generated catch block
. t& r, ` Q* L. A' ?9 \. R - e.printStackTrace();
1 ?: Q; P0 O$ F; ` - }; b- h' e2 J# L: U
- }
8 z# ^ j! W) s8 T -
6 i( g5 _; n# C9 E2 { - public int update(String sql,String[]getValues){
* [2 |- l7 [9 H2 r3 I - int i=0;$ w! ]3 [8 c# \( C
- Connection con=null;4 ^9 J# K+ V7 b3 ^4 \
- PreparedStatement past =null;5 v( z5 }. |5 e2 n0 F2 G! r1 M4 ^
- con=getConnection();
: `7 D1 c$ U5 H' z/ u$ L- I7 A' i - try {5 I0 J2 R- Z' r1 D! X' h
- past=con.prepareStatement(sql);
! D! w3 e. s7 {3 b7 Z$ A - if(getValues!=null){; Q: E8 j1 G: o$ G) r
- for (int j = 0; j < getValues.length; j++) {
6 c# v5 j' ]$ D. B - past.setString(j+1, getValues[j]);4 Z0 H) t( e& `. U; w& d/ Y/ k
- }$ r, S. _ O& \8 ~/ z7 u8 E R
- }, i, m' j2 n6 _6 J
- i=past.executeUpdate();
/ e% N+ t# q. H8 K3 V - } catch (SQLException e) {9 Y1 ]" {1 F1 e A$ ~
- // TODO Auto-generated catch block# y% U' I" W( U6 ]& I3 C
- e.printStackTrace();" }" `+ N3 [9 a& q2 h; D
- }
. R: _1 T; L# g# J. F; Z* i8 j -
: g; D# H5 t- B: J( [ - return i;
: V1 `, I3 F! D( s% g% U - ' n& v$ i' i+ s# k
- }
' a+ j3 p* R9 l - }
- H9 T" l1 N; s: c
复制代码
7 m* N, H( G* u6 z. m7 @" ]0 g9 z1 a" M( F% ]
- package s2.jsp.zhangxiao.dao;
6 p9 R# j6 M* a
9 b3 }- S7 b2 \, K& M( o$ j# l: b4 b+ w- import java.sql.Connection;" l: V. Z' I3 _4 R6 G
- import java.util.ArrayList;' u' h3 ~/ F8 \9 U6 \/ Q. M
- import java.util.List;
C5 H" r. _: E& Z3 |' X/ o - import java.sql.PreparedStatement;+ n. Z" Y3 W3 P8 H. v4 a/ I, m
- import java.sql.ResultSet;6 O0 o0 z! w2 K, E1 V
- import java.sql.SQLException;
f* \# a! k9 _0 {2 [+ | - & x" E: H b1 K
- import s2.jsp.zhangxiao.entity.Book;
: U9 i: Q, D% L9 D' `, b
5 f7 D) c5 H) R5 }- public class BookImpl extends BookDao {, N( S2 }+ k; G [& ?& ~: X1 L5 x- ]
2 L: z" Z- P# \' L+ B- B p- 8 X9 v4 U" Q0 e2 d( R' |! R1 l
- public List bookAll(Book book){, ~1 F& S3 W J- B
- List list=new ArrayList();
5 y/ } F$ A1 X - String sql="select * from book where 1=1";. i% h1 _. a4 y$ S& `
- Connection con=getConnection();5 v/ n: u! \, v2 i
- PreparedStatement past=null;
4 }) X4 L l- z# Z7 E% V7 a$ g - ResultSet rs=null;
' u5 w/ }' ?, W1 ]2 o - if(book.getBook_name()!=""){+ x* D: a( y$ ?6 O; x
- sql=sql+"and book_name='"+book.getBook_name()+"'";
- G$ x+ Y1 h7 i - }& c5 f: Z5 o9 f, G1 k
- if(book.getBook_type()!=""){( h- c6 g- g T1 A
- sql=sql+"and book_type='"+book.getBook_type()+"'";2 } p' f8 L7 S/ W1 L: g
- }
4 \! T' e' e; A. o' j7 x - if(book.getBook_publish()!=""){
( p& K1 R+ ]5 Q5 R5 U* H- E9 C - sql=sql+"and book_publish='"+book.getBook_publish()+"'";( i3 a* ?* C Y
- }
/ y& m1 W1 y2 B' C v -
+ q. m/ ]! \8 E+ T$ ~' F. M, ? - try {1 u3 u. e9 `/ S. i2 ~ x0 i
- past=con.prepareStatement(sql);
, X5 u( q1 O( S/ g - rs=past.executeQuery();# \4 F* T( ]3 G8 u5 n! L
- while(rs.next()){
: n, Z. \2 p! y E& E - Book books =new Book();
( E3 _! d' v) ~4 N3 `4 J5 E - books.setBook_name(rs.getString("book_name"));$ c1 R, I) r" R+ D* l! v: \
- books.setBook_type(rs.getString("book_type"));
/ w% \& f3 U" O/ J/ h; T; \ - books.setBook_publish(rs.getString("book_publish"));
; c6 e6 V% l9 G0 A2 L4 x - books.setBook_date(rs.getString("book_date"));6 v& |9 e \' u; ]
- list.add(books);/ d! A' I' L/ Y L! W
- }1 q8 ?- D$ W8 T& c0 }
- } catch (SQLException e) {
$ v; i W* Z0 e - // TODO Auto-generated catch block
, b) I" B. J1 s+ v" `6 Y - e.printStackTrace();8 N# L9 e# Z* ]5 A) K0 Q. n: ]' f
- }
# q5 x( f( w- e* {; R( Z" r8 ]7 X# D - return list;- d) n! q2 H/ G/ d$ E
- ; G+ r, C7 N z0 n, c+ N- y5 V
- }5 w# B/ N- y/ E. J: {
- public int bookInsert(Book book){% G; K* Q; v! [
- String sql="insert into book values(?,?,?,?)";: i. x( a, s0 }1 u
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};, U' M* u3 S( O% R! J
- return update(sql,getVlaues);
: H8 c% o E2 [: I: z - }+ T1 Z& [7 Z5 X( Z3 u' r$ y( T1 j% t
- public List listtAll(){
8 l. k# [ r) J% K - List list=new ArrayList();
/ ~( u M9 I1 ~: q) D- L - String sql="select * from book";
, O" `7 t5 o3 A - Connection con=getConnection();
( X# u7 p8 R5 O; V! K( h% Q - PreparedStatement past=null;$ d1 L6 I) w9 U1 Z1 l2 \
- ResultSet rs=null;# R* x3 O* s- o
- try {5 V/ V/ e: R2 ?' M- i3 H" E8 [
- past=con.prepareStatement(sql);% d! v& A7 h3 }! ], @
- rs=past.executeQuery();' n: H Z, e5 [8 ~2 t
- while(rs.next()){) U2 b1 A- {: E7 ~, ~8 f8 |
- Book books =new Book();1 q/ t I+ r- ~0 p
- books.setBook_name(rs.getString("book_name"));. m+ S& ]. @% t- X; L i4 y7 ]7 J
- books.setBook_type(rs.getString("book_type"));3 |* ?' p# E! G" d* m
- books.setBook_publish(rs.getString("book_publish"));5 Y, H8 B9 O4 g5 L/ Q8 _5 S# G
- books.setBook_date(rs.getString("book_date"));
! h- m- C- A# x: X0 m - list.add(books);
) U, P$ _3 G/ L, y; ?' X5 v - }: Q1 W" v, g, m W) Z X7 G0 S
- } catch (SQLException e) {
S4 A8 r, J9 g6 C0 i - // TODO Auto-generated catch block: x( f& `: U1 N+ N6 g% b
- e.printStackTrace();+ Q0 C% Z. S* S
- }
! k, }" i, H- _- s0 y9 V2 y1 U# i - return list;* x- x& P$ [# o$ O7 m
- ; ^4 A3 a5 @2 G3 y3 ?
- }) V* l# |' V/ @% `- e( G% B, ]
- }
' I5 N0 k$ s# H2 @
复制代码
& L$ i6 f5 o8 K, S2 N0 z) W6 `6 `! N5 y5 q3 Z
* W9 V# v8 _ G. q7 D |
|