该用户从未签到
|
- package s2.jsp.zhangxiao.dao;; o% X7 }! Y6 T6 w3 z2 P9 `% o+ z
- import java.sql.PreparedStatement;
& z. ~6 Y) m" H7 i/ @( p# r - import java.sql.ResultSet;8 e5 k& ]3 V: t- m
- import java.sql.Connection;$ @, `# O1 ]; T. e; P( [8 [
- import java.sql.DriverManager;' q8 _, w1 I: |# e6 j& \7 V5 ?
- import java.sql.SQLException;/ D J& I7 k+ \4 x9 z3 d* \
- import java.sql.SQLException;
2 h; w7 U: P. L7 Y: \
! w6 k$ E$ l H7 o# P
+ k9 |) S* ]; s1 J2 } c+ Z/ `" D
4 R! ]/ a9 C1 C6 f, M2 w/ K0 g' U& Y- /**4 A4 A! [, z& S. \/ ?2 Q
- * 连接数据库% ]* B. y( r$ d7 X
- * @author student1
* e1 V! ?6 q- b' ^! ~ - *
0 u# C# V. ~. C+ B# G$ t: p - */
# r9 N* P' a6 _6 P& j! V - public class BookDao {* U. q3 `8 S e! O
8 l. I& `# ?! v: {- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
, S8 s% M6 {# z - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";1 h$ b, q! {3 h Y/ \- Z1 b5 {
- private static final String DBNAME="sa";, x, A- q5 N. j; m( A: l: T$ Q
- private static final String DBPASS="1234";
* H8 S) I. O, d - 7 _1 ?: Q& ~) e V
- 1 E1 Y5 S5 Y; a5 C- X9 c
- public Connection getConnection(){
4 P m6 w* R) `; e$ f - Connection con=null;+ ^/ X* }3 j; ]1 @; m
- try {0 b6 Y- u9 y0 ~" z
- Class.forName(DRIVER);, [, B& D/ a: I. Q9 `& y) l
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);/ h* \- D' U n0 H, r
- } catch (ClassNotFoundException e) {
- r6 w7 t0 [* I5 H6 I( a { - // TODO Auto-generated catch block
2 j5 e. a& Q) r; t; w& \ - e.printStackTrace();
/ _1 h5 C) U; E* E - } catch (SQLException e) {
/ P0 h* T) m6 Q' w: w$ H/ r - // TODO Auto-generated catch block
; f8 [. I" R( X - e.printStackTrace();
+ F! W0 Y: J" x7 ]+ i3 l - }* R6 l ?7 \" H
- , y+ n, @/ [# q7 \. B3 f2 f3 S( I) Z
- return con;
4 P1 x S6 T1 Q( l* Q. d - 3 {9 q9 p/ g+ z& S' @9 M
- }4 \- }& q6 q, D% C! o
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
6 ^2 I( Z$ \9 N( e - try {4 @* _0 K5 t! Z; v1 l4 Z
- rs.close();
8 x9 S: d0 v2 f/ E6 Z - } catch (SQLException e) {5 P) v4 y( L% ]+ X. v" T4 E7 j+ s
- // TODO Auto-generated catch block
% R7 G: M/ V6 o4 q7 N8 M$ [+ q - e.printStackTrace();
% b0 M" f$ G; m4 d$ U4 u% Q! P - }( r7 A: ]8 p# T
- try {* b& \0 {; T* w
- past.cancel();
; P0 P) \7 F% E) _ - } catch (SQLException e) {) s7 M/ E, |+ @
- // TODO Auto-generated catch block7 j* n0 V, ], h; m+ }$ b
- e.printStackTrace();% L: b* E: i! m* s+ C
- }
& i# Q: T5 l2 @, e/ \4 @7 e - try {: e3 d1 z' h1 {0 _; ^* U
- con.close();
" y; l m2 L# \$ E. G0 S6 ~ - } catch (SQLException e) {* G8 y( V! |7 j% m8 F! A
- // TODO Auto-generated catch block9 t& Y+ |- R" f4 j {% X( {
- e.printStackTrace();" ^* `2 N$ }, V2 Z
- }
6 ~" Q) ?4 M2 d - }
$ m% w+ P+ K7 U8 N- w - 1 e" Z9 j0 V R q' U' z+ F2 O
- public int update(String sql,String[]getValues){
- W7 P y2 D: \5 p- u* E# P - int i=0;
& D5 e% W' R0 C( k( f1 e( k - Connection con=null;# l( n6 T, H+ E* k1 @; {3 M" Z" c
- PreparedStatement past =null;
! _' z& |) Q( S# Z6 J - con=getConnection();
2 Y- i, a, h- U* C6 _ - try {
7 @# q. ]7 j$ H: U2 T - past=con.prepareStatement(sql);. U j6 k( U$ v
- if(getValues!=null){
" y& `+ H* g: ^' }0 m5 G - for (int j = 0; j < getValues.length; j++) {
3 C1 C- v1 v1 l4 X, S+ k. `' Y - past.setString(j+1, getValues[j]);
; @9 D2 R- u, | - }
% U+ W8 i5 ^0 ?, S+ r - }
$ F, Y4 q% d9 T9 p0 e( S7 A G - i=past.executeUpdate();
4 t) J) Z$ f" ]' t& Z4 c - } catch (SQLException e) {- C% s& I2 L$ b8 u' U4 x1 f/ Z
- // TODO Auto-generated catch block+ J& C- c y2 S
- e.printStackTrace();( O5 ~9 N5 R/ k( E8 J
- }
* J U2 K6 {: {$ x" N& p" m. U -
, i% d8 B* ~! c) r2 j0 l |: D1 D& c - return i;
1 H7 [4 _9 A* i u. ^7 a: f -
5 a: G ]; {" A7 D7 p9 \% d* `0 K - }8 \6 w. m8 h0 A, W& d0 N
- }
% o# `. O6 M2 O1 L# U. {9 k
复制代码
1 l- C' J1 `) ?% U: P9 T6 f. m& Z7 g6 |1 A( m" b( B4 m
- package s2.jsp.zhangxiao.dao;. w$ d( f2 ~! z- \+ n5 c {% w
% T+ m8 n |. U- import java.sql.Connection;/ f4 u4 i; [& V% }; _0 h
- import java.util.ArrayList;/ p% y+ M; t0 u! q7 o
- import java.util.List;( i3 V7 |( u. s2 I( H
- import java.sql.PreparedStatement;# |- o( ]4 [4 R* ~' P) \
- import java.sql.ResultSet;
% p2 E4 r, P( `- W - import java.sql.SQLException;
8 l& k# d) ?, k# c( s h X - * l, c8 `4 w4 u8 ~/ y' ?. E
- import s2.jsp.zhangxiao.entity.Book;
! \- l( M i* f% Q- [, p - " w+ ?) e" ]* Q$ s' D: t1 ~, }
- public class BookImpl extends BookDao {& U9 G H4 \( H5 X6 I. U$ z$ [, p
" C [4 H, H, m& G. J/ f-
6 s) w! F! ?6 e - public List bookAll(Book book){
- K) t5 a0 c- C, m0 n, `0 p. p3 G - List list=new ArrayList();
8 e. l' a( d3 n7 y6 G7 O9 x1 m6 k - String sql="select * from book where 1=1";0 N& ^9 K- }+ `) O; k5 V) H- |
- Connection con=getConnection();' C" _, L% S) y+ H
- PreparedStatement past=null;
: Q2 d( q( F" b, i9 {- r - ResultSet rs=null;$ \4 d9 \$ x: B
- if(book.getBook_name()!=""){
# P0 z3 Y2 |5 E: }, N. F# l - sql=sql+"and book_name='"+book.getBook_name()+"'";& K! ]0 U# n& I2 K
- } _9 ?* G( w; |3 H8 M7 R3 h6 X
- if(book.getBook_type()!=""){
- d6 n( ?8 s2 e0 U! j - sql=sql+"and book_type='"+book.getBook_type()+"'";
7 @7 a6 b3 u+ Q. \! V+ O+ o) A - }
P6 a. A, r8 W. y - if(book.getBook_publish()!=""){, y( ~: \4 e6 i# q- K1 p- P$ v' S
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";2 Y2 L3 a9 f# r& K% ~8 i. [0 Z& D
- }0 D' G! }4 E4 _1 \' p7 u) _8 y6 S
-
+ N L. [' ^ ^% @# N - try {: M& D8 N( t% E
- past=con.prepareStatement(sql);4 T3 y! b* h% K# d/ u& x
- rs=past.executeQuery();
! r6 H M3 N; I' T3 Y9 l - while(rs.next()){
- t0 j* S! i' c+ B! H) P9 {' Z( p - Book books =new Book();% Q. ^0 a% e! I4 m
- books.setBook_name(rs.getString("book_name"));
' E; f8 R( l9 F5 o& ~/ m( R - books.setBook_type(rs.getString("book_type"));
2 h+ n# v$ V6 o* P/ x% h$ [ - books.setBook_publish(rs.getString("book_publish"));
' b: k+ q* V4 A/ x6 t: p - books.setBook_date(rs.getString("book_date"));
8 C! M) k: A1 z0 T9 ] - list.add(books);
" \4 q: T. M$ ^- I8 b K5 `3 ? - }0 C0 c5 Z8 N" _! m
- } catch (SQLException e) {' r9 U' S2 ?; [) Q" j
- // TODO Auto-generated catch block/ f7 r! o7 Z; t2 F6 ]
- e.printStackTrace();
5 r; u# H) u2 {# w; k& E9 R - }
5 X# g7 Y7 e; d: v& e - return list;" [1 Z* L5 X4 S/ J
-
' m! l; ?; e# S8 z - }
$ ^4 M5 U2 B) }# ]' D' e4 \3 ^ - public int bookInsert(Book book){) O& @8 N2 r' j8 P" ]
- String sql="insert into book values(?,?,?,?)";' j, N4 Z; ]# ?5 |) M T( L! {4 Q
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
# u; Y0 B' r; [9 \6 g# M - return update(sql,getVlaues);
# |& ?- v' r0 D. J) v - }. _) Q* E- B* W9 r1 ~! W" y
- public List listtAll(){
& ?5 x8 m: a+ f - List list=new ArrayList();/ U. h* ^, ~) z/ R
- String sql="select * from book";/ d$ s8 F6 U9 K8 K
- Connection con=getConnection();
, U* k7 M% M _& Y2 }* F - PreparedStatement past=null;& a" \+ \$ k( P2 G
- ResultSet rs=null;
6 B. B& U' j. e: ` - try {# r' ~4 C* f6 t2 z3 A8 i
- past=con.prepareStatement(sql);& l- Y5 X' T. b5 | U
- rs=past.executeQuery();; u9 P7 o* E/ M. Y+ f
- while(rs.next()){* \& Q4 I0 Q+ w& S
- Book books =new Book();- {9 H( V; J6 e9 f8 S' P* D
- books.setBook_name(rs.getString("book_name"));- B. w$ F2 N5 d
- books.setBook_type(rs.getString("book_type"));( F4 b& l3 L$ b0 }7 a M! ^; T
- books.setBook_publish(rs.getString("book_publish"));
2 L& }. h- s& E! Q+ { - books.setBook_date(rs.getString("book_date"));% J% ?$ h" H2 c8 d4 t
- list.add(books);
/ X Q, \, k( y7 d9 t$ K2 W! R - }
2 r* U9 m- L( u4 k: K; L, b+ T. r' g - } catch (SQLException e) {
7 V! ^7 E7 v# ]5 R - // TODO Auto-generated catch block+ X/ V5 N; f. p
- e.printStackTrace();8 a: O! }9 b7 w5 C4 f
- }
" o( y4 o+ K# F) ?. b - return list;
8 K: d/ M0 V# W: e1 o4 A! N0 x5 J& S -
, Q+ L/ k" c+ h1 c) F: Y5 s- B - }* h/ m* f. i! ?5 k9 M
- }
! P" l: O6 [: E
复制代码 % p# [; B7 d4 u# R; y2 i5 `" d" @1 X I) H
7 k- p% }4 o! G8 ?- P, ]6 F
! o( K6 u& O ?5 z3 q' s) y# J |
|