该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
0 v5 f& N" q' I5 V# ~5 q - import java.sql.PreparedStatement;, E$ Y P2 x/ r
- import java.sql.ResultSet;5 V' R( s' E, E& @- ]. S s7 J: |
- import java.sql.Connection;
/ k+ f6 m- Y0 W1 ~+ h! e - import java.sql.DriverManager;) E |, ~" A0 T; a! N& j
- import java.sql.SQLException;: K9 I S% L* e/ M7 T: d
- import java.sql.SQLException;
7 x9 H5 \5 I+ M/ \) i - ( j y8 F2 n" u% F
6 H* R7 ]' G6 u2 s+ V( P& ]
( [6 F: u9 J" V' _, o5 G; b- /**
* h- A5 B% J* v7 q, a1 @ - * 连接数据库& k) a$ S1 i6 a# E
- * @author student1. A" Y! | o+ E
- ** d) `9 w7 \% ?; b% |
- */ M# \5 M3 }* c: s- ]: A
- public class BookDao {6 s$ Q; Z9 t9 x
- 0 y/ S2 J! `" }. w. e
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";% g$ Z9 \1 ~9 Z) H
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book"; o0 W% X9 X4 z2 g
- private static final String DBNAME="sa";) [, q1 B S( p* t' R4 T7 z
- private static final String DBPASS="1234";
; L' y3 f. z0 S% e0 M( T - ! W2 U8 ` k# ? X$ Q
-
% m% M) \+ E- W+ q3 S2 T- t - public Connection getConnection(){; V7 S( K' g' ^/ X: t
- Connection con=null;
% L1 e5 X( M6 \: @0 q - try {
r2 J) j6 `' Y3 c% ` - Class.forName(DRIVER);
: o1 H }* O7 L: l. N* y - con=DriverManager.getConnection(URL, DBNAME, DBPASS);* w$ M2 v+ j$ k# z' |) e
- } catch (ClassNotFoundException e) {. F$ B1 N* |7 A3 o( Z
- // TODO Auto-generated catch block7 o$ L- ` q5 D5 @9 H6 R
- e.printStackTrace();$ U _4 X9 `5 [0 `( O
- } catch (SQLException e) {
! H/ a+ B, ^. t - // TODO Auto-generated catch block
3 ~9 D a+ } B1 x: c - e.printStackTrace();0 Z( `0 k0 x7 j$ _, i5 L
- }
& c( s) A( K6 i9 O
2 K& i/ p8 j8 `2 `6 B6 J2 k! q- return con;
2 ]2 u ^; [$ |7 v* Q - " f, A6 v+ y/ ]2 v8 `
- }/ T8 }* o( v! v7 ]; b1 r* d
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
" k$ e$ U: c- k+ H - try {! ~( Z, \# N! m1 D
- rs.close();& b# ~1 b; ~/ P6 G7 C7 L, n
- } catch (SQLException e) {
) _& l7 P5 y* l, n! H" } - // TODO Auto-generated catch block" x/ y% Q( ?0 _. |
- e.printStackTrace();8 [( D. p& y$ ~/ d1 @9 V( V y
- }
# n/ v6 O5 j& \4 e: t& R) W$ v - try {
) }+ q' w" M! Z8 ~, ?3 \ - past.cancel();
L1 W! x J% b - } catch (SQLException e) {
2 D! E7 p( a, b - // TODO Auto-generated catch block# t+ x4 f# h0 s3 D0 Y, C' B1 u4 V
- e.printStackTrace();
* F" ^) _/ {7 |" A5 i - }! r+ T9 p4 W7 G# R5 T- h# H
- try { [+ J2 ? j& `$ B) ]+ }% ^
- con.close();
5 i. L" d4 b$ z+ L* j8 Z - } catch (SQLException e) {% q! p' C$ z& Y1 f% W& v5 _
- // TODO Auto-generated catch block
9 Y4 z5 {6 _& i9 t# C2 L - e.printStackTrace();. U. P9 K, Z! O. m
- }
( n; N+ s1 h8 U& Q, Y - }, t3 K: s6 s, a1 \! m
-
( R$ B; ^/ K# [ - public int update(String sql,String[]getValues){
: H* C6 z0 c5 f* m$ s5 Y7 K - int i=0; g% a. Z d! v8 R, C4 [7 N5 g6 g1 d
- Connection con=null;. f! O- M. P! w# _0 X
- PreparedStatement past =null;
. _1 d0 G @+ }% D# i% ` - con=getConnection();- q8 B3 x3 f8 r0 \- q8 n) G
- try {
* O( P( q5 E" A - past=con.prepareStatement(sql);3 y/ C& a; Y8 a: F
- if(getValues!=null){! {0 E3 S! _% G$ F6 F! l( ?
- for (int j = 0; j < getValues.length; j++) {, p8 ~0 b+ F& a* D
- past.setString(j+1, getValues[j]);
/ X& f! ?5 l4 V - }
: j6 z r1 x S3 ?4 `7 J0 ^ - }1 y- z; Y; g* ]+ t4 S
- i=past.executeUpdate();
) E3 z) B( o- M - } catch (SQLException e) {
; k7 n3 c W+ s; }$ J+ o* \" { - // TODO Auto-generated catch block+ y0 A/ o. Z4 @8 `5 o2 l
- e.printStackTrace();
1 @+ S( w5 x9 I - }( p8 c8 b, i. Q+ F) ]' x
- . S" s c0 m1 }7 B! x7 j
- return i;
5 n% Z3 U _; y/ y2 d - ; d$ J( X% _5 N+ b! `3 H0 b3 m6 G, ]( O
- }
- x; n# ~( F# f2 L - }7 Q8 v3 Z- G' Z! _: e3 Y
复制代码- package s2.jsp.zhangxiao.dao;
' B7 z" Z; N# x& d - , {1 w8 C- |* R8 k1 R! N
- import java.sql.Connection;1 s3 ^: v) r5 N* Z. |! j+ c% x2 K
- import java.util.ArrayList;( o# s m3 F4 P4 H3 Y6 w) A
- import java.util.List;0 Q f0 d' ]6 }. V) X& l0 r
- import java.sql.PreparedStatement;$ c+ p0 Q+ u2 l6 I
- import java.sql.ResultSet;+ U6 |' B+ r" x1 j' _( Q- {% k
- import java.sql.SQLException;
3 }4 R2 }' G" s+ F
4 [" O3 T0 \$ X. H/ {) }- import s2.jsp.zhangxiao.entity.Book;
6 e! U2 A* [9 j) e, w' A- Z
) M/ k& ]# E4 ^2 h- public class BookImpl extends BookDao {6 d+ }5 e1 Z) N/ q8 l# m ~' N3 r
- 3 A+ q+ l2 M$ ^' g
- + W. c" E& M2 w; L; K; u, O) f
- public List bookAll(Book book){6 r A' I! H: W
- List list=new ArrayList();1 e( f8 h# s6 h7 E5 p8 C
- String sql="select * from book where 1=1";: O* Z* o! Y6 A( R2 a u
- Connection con=getConnection();
% z3 c, ]( O/ Y) e& g% {2 W - PreparedStatement past=null;) Q! F0 w! @2 ]3 J$ @
- ResultSet rs=null;
1 ~2 C8 P2 L( D* K - if(book.getBook_name()!=""){
+ S* @" {. p+ t: N# n$ v - sql=sql+"and book_name='"+book.getBook_name()+"'";
$ W7 D4 [' o2 a - }9 X) Z1 S* {( h0 ^$ m7 _
- if(book.getBook_type()!=""){
& w1 J1 f' b1 H* g7 p - sql=sql+"and book_type='"+book.getBook_type()+"'";6 l5 J) W' v& H/ [1 C- U- i
- }
+ z! v+ w9 p: f0 q - if(book.getBook_publish()!=""){0 V6 r5 W2 y5 Y! y# ~
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";
, k$ i$ v0 M" u3 H# V, v - }
+ \. f, W. s4 O, C - ) h) ^) }: \, P. @- k
- try {4 v% H. a( K8 l
- past=con.prepareStatement(sql);
, n! m; l9 B0 A! M" N u) T& L6 S5 O. V - rs=past.executeQuery();- p8 @- O2 E: A% [9 k8 s/ a
- while(rs.next()){. u2 t, L3 i" g8 |
- Book books =new Book();4 Z& ?) z- ^% v/ G! |) Y' H; E7 p
- books.setBook_name(rs.getString("book_name"));- n' M6 I" D: w" Q
- books.setBook_type(rs.getString("book_type"));
! M, X8 ]6 f+ o- O) M: f* i; c. Q - books.setBook_publish(rs.getString("book_publish"));, D; b5 M6 H, G8 W3 ]2 C& q3 q
- books.setBook_date(rs.getString("book_date"));9 S! s( |1 ~1 s0 c0 ?
- list.add(books);
3 ^" E: U: j0 `# t8 A( ?' m - }1 e" I* _ u" N6 h! a+ |# U
- } catch (SQLException e) {
7 i7 i1 V! i9 k+ y* H1 C: J - // TODO Auto-generated catch block+ q& D8 h$ d [- \- v1 g
- e.printStackTrace();; V& h6 c1 U, o* {1 G- X5 @
- }" e, A& [9 v: Q% |0 H
- return list;
4 N3 Q4 ]2 |9 r' B, |" c1 l/ F* J - + h6 \* X3 V# ?8 t1 Z4 s
- }" r D+ F3 {0 Y' N `" a) ~
- public int bookInsert(Book book){, l/ {5 i7 ]" O: _
- String sql="insert into book values(?,?,?,?)";; m# H! C$ |3 H0 [
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};& P4 B8 s* Z( V- C) F- C
- return update(sql,getVlaues);
0 L+ B# t& B9 t' x) C# X$ ~ - } w0 F8 B! n& \
- public List listtAll(){4 X1 M0 M1 J+ v/ u) H
- List list=new ArrayList();
7 E9 C# p4 b; |# ^, m' I - String sql="select * from book";
4 ^5 Z: J! A0 k- n7 { - Connection con=getConnection();
1 O) m0 I9 X# b2 w - PreparedStatement past=null;8 o( p5 [9 z$ V. _+ s# a' w" a
- ResultSet rs=null;
- n% Y% X! I/ w" _8 Y/ A- { - try {
1 }) R! C. B' p& X& G ~ - past=con.prepareStatement(sql);$ z) u H, k- n/ c; K# r
- rs=past.executeQuery();
+ N9 l. Y% [% \4 I - while(rs.next()){
H! f. f' ]2 x# g - Book books =new Book();# I0 H2 t0 G' e0 `/ o6 r! J
- books.setBook_name(rs.getString("book_name")); d$ h- X5 z- g
- books.setBook_type(rs.getString("book_type")); T/ S, F u: Q
- books.setBook_publish(rs.getString("book_publish"));
* L/ k) ?% t2 L, M% i$ `* H X - books.setBook_date(rs.getString("book_date"));# L, A/ y7 Y* H2 J2 O# Y: A
- list.add(books);4 [. ~& Q% v {% u- t3 y" H7 w
- }
& z6 Z5 k, K2 {* U Q - } catch (SQLException e) {) k( ]7 `7 D" a, D. u; M
- // TODO Auto-generated catch block
* F" C7 W; A8 ^! |6 D* n1 v - e.printStackTrace();
7 O; `/ l: B2 E4 Z/ G1 h - }+ u$ u) k7 n/ T# [+ A
- return list;' u6 ?& k% T# d8 ^2 V4 s
- - e& D; W5 Q# w
- }
0 |; Q. R+ C9 t F - }0 m+ r" k+ Z! h
复制代码 源码下载地址:点击下载
: j% u; Z& P k: @% n4 D+ {+ Z' J! w9 u4 h7 p2 i8 M
7 |2 `5 E- u6 @( n9 O' Y |
|