该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
6 U# ~4 |) `/ T" P& q. P - import java.sql.PreparedStatement;
! q7 y* b& G- L. G - import java.sql.ResultSet;
- C, u# o7 l3 k- \/ n/ E/ ?( j5 j e - import java.sql.Connection;7 d6 N: t: a6 S* J6 \
- import java.sql.DriverManager;9 o9 e3 N- I7 a4 G+ ?7 W
- import java.sql.SQLException;
& ]) N7 C( X$ D2 P/ w# o - import java.sql.SQLException;$ b/ D# d6 r8 b$ t) \$ v) a
* {- s+ y* h2 n: ~* I; v w: K- " t8 ^. L; k1 q; s7 y; V5 u& C* i
0 f. c* m3 p5 Z" ]- /**5 Y2 B" I U& V8 `% a2 ^3 s2 a
- * 连接数据库7 x$ h% o6 H. z
- * @author student1; D! N, s5 ]0 b8 w
- *
' g6 [$ c3 @+ ]. a$ @ - */6 S. T+ k/ k/ `. Z4 a
- public class BookDao {
# \6 b/ z v# }
4 Q9 X$ M! |, a) m7 r: }) V' F- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
2 E- x3 @5 ~: ?! m+ I X: Y - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";5 b3 p" `7 ?( G; Y
- private static final String DBNAME="sa";
, T) D; @# g8 H. u$ h7 m( g - private static final String DBPASS="1234";
2 m/ \1 n ?( B L5 S% }: f" B - : V* G, ~- W4 w. L" e- O
-
0 G3 Z, j" Q& s+ j - public Connection getConnection(){8 ~" v$ @3 {& L9 c
- Connection con=null;. P6 T& \8 G% W) B
- try {
# r( z7 X# B9 i; U6 ^3 t( z' L - Class.forName(DRIVER);
( F8 ^/ X& d( N2 l1 a2 o- j- ` - con=DriverManager.getConnection(URL, DBNAME, DBPASS);1 W8 R9 W, i2 \6 e
- } catch (ClassNotFoundException e) {+ m0 Y6 I$ w3 d5 }) ^. E
- // TODO Auto-generated catch block0 x2 B1 r E4 I# r2 k1 V7 @
- e.printStackTrace();
2 l: a A. L8 l0 L+ T - } catch (SQLException e) {3 I' x- _, G' n/ X7 t2 q: W3 u
- // TODO Auto-generated catch block9 T5 |' A; C2 z K: v- g6 K
- e.printStackTrace();+ G: s5 W/ V7 I/ a& c* q1 O m) m
- }6 k; ^3 D6 s7 Q; M. g
- ; L5 G3 S: n- q! q' ~
- return con;
& ~* U3 z2 u* q' q -
; e$ s' ^ L W3 j3 I. X - }% n! f* w* A5 Z7 Y' V
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){" D: B3 R! @1 c" e
- try {
" ]# B# f( {2 V/ f1 i- Z - rs.close();
( s1 d( j3 Q3 N S( f& G' F - } catch (SQLException e) {1 U0 O( k8 J' B o' S
- // TODO Auto-generated catch block1 A9 D1 D8 D% ]* e
- e.printStackTrace();
% U8 f0 n% e& m - }
. m! r- i# w; e - try {
0 W5 k5 K2 {* L e/ O- D - past.cancel();0 |& [# Q' @$ ~) e" }
- } catch (SQLException e) {
" M$ l1 I0 c0 R6 A( E' ?$ @ - // TODO Auto-generated catch block$ G. x) I) y3 T2 R$ ]
- e.printStackTrace();* s& w/ Z7 U4 ]& W9 O3 ^6 f m
- } l, H) U" k; ]8 e. E; K
- try {
0 a. w8 ]5 l3 k) y6 i. l - con.close();- X( G: A: J5 T; t9 I
- } catch (SQLException e) {
7 s! n$ K' I5 A9 A) Y7 N& K - // TODO Auto-generated catch block7 n- p4 H9 ]+ {: B9 @- n
- e.printStackTrace();2 u% S# @ k4 e! m& d' y
- }8 G- K( ~1 e1 J' w/ D, Z
- }
( j1 H& v2 n$ O$ U' X0 B8 s - g2 b+ |8 C4 S' U/ H
- public int update(String sql,String[]getValues){% G+ w/ X: p0 z8 a! e# n/ r( N3 }
- int i=0;$ R2 G" X& X0 x
- Connection con=null;3 b/ K; K H3 j0 P( X. I1 W+ a: A
- PreparedStatement past =null;
+ k: f6 M! ]" O' m# l. V - con=getConnection();( P7 N1 U6 E' U6 |* ]+ m4 g
- try {
}3 y, }5 c. x/ k3 ~. o - past=con.prepareStatement(sql); f& T1 { ^" m2 F& z9 A% I6 ]! u4 ?9 A
- if(getValues!=null){& M/ H3 z+ M! S1 {4 ?0 |
- for (int j = 0; j < getValues.length; j++) {
4 l/ C1 E0 p' W - past.setString(j+1, getValues[j]);
6 V8 Q4 F5 I0 e4 Q `# ]9 A - }' r' B( _4 g# m$ G6 x
- }
8 D/ g+ l# ^* L* H - i=past.executeUpdate(); b, f+ T- @: ]/ T
- } catch (SQLException e) {
2 U+ u# F0 s! s, w - // TODO Auto-generated catch block+ x4 [ a+ M/ Y
- e.printStackTrace();
3 r9 ^1 T/ r! M; P1 a$ n6 Y9 h9 I - }
' t! D( ~1 n( P9 P# ^ - 3 t1 y0 b2 V: Y# S& w
- return i;( B& f5 B4 Z1 j' E* F1 p! Y
-
* y* V8 U z! ^6 T - }
. G. l1 j' ~8 r- I) {- y - }$ G7 q; X/ }! r; H, \7 f9 w
复制代码- package s2.jsp.zhangxiao.dao;
& ~; H2 _' Y ~; j
. O$ T" e, V- E0 d7 U- import java.sql.Connection;
7 J$ }7 x' h- l" v! U( I# ^ - import java.util.ArrayList;) [- T5 ^% N/ Z" l7 \+ X
- import java.util.List;
$ X# {2 J: `3 X$ U( ^2 g" `5 g - import java.sql.PreparedStatement;
4 k2 {; b' v& c( V - import java.sql.ResultSet;" Z; ?0 i% v+ h2 I
- import java.sql.SQLException;% J9 h$ h9 Y" \0 k9 m) P+ p! t
) \, w+ I; O6 y/ ^% o+ g0 U- import s2.jsp.zhangxiao.entity.Book;) l$ I9 Q( w7 W
- 1 I/ u4 U1 }6 c* u& N" p8 L2 q: x
- public class BookImpl extends BookDao {
5 Y% H' r$ Q& d/ _$ G/ M( V
9 @4 o. V! t* \: k: D( g- * L7 q2 }/ A! Z5 C7 C9 Q
- public List bookAll(Book book){
, j/ P4 B' w' o- V( H4 o) q! q5 K$ Q - List list=new ArrayList();) j/ Z" q) a5 ^: q
- String sql="select * from book where 1=1";- T4 s! B/ u- h: W# i8 ~ c1 l
- Connection con=getConnection();2 Z o1 o' b, U( R5 V$ n; P# S
- PreparedStatement past=null;
' w. c' _/ N( K9 g - ResultSet rs=null;
/ u* R2 a* T; H& T - if(book.getBook_name()!=""){& ]! x1 b: L0 O/ x: M
- sql=sql+"and book_name='"+book.getBook_name()+"'";" p, W+ z1 k7 v Q& ~% n0 z( _1 e
- }# q# X, T6 l7 Z, j1 V0 W* H! `: j
- if(book.getBook_type()!=""){, D. ~) w' |3 a) l" y
- sql=sql+"and book_type='"+book.getBook_type()+"'";
( f0 D7 W3 e) Z2 Y4 y; } - }
, K9 t1 _+ x5 O# }- v9 @( Y - if(book.getBook_publish()!=""){/ ~. {3 i2 }8 }7 z
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";
" r3 L: |. I4 M- G* E - }6 {; M- ^3 Q$ D
- + ?& Z" ?4 _( d- X4 S
- try {
2 k; L+ D+ f2 z - past=con.prepareStatement(sql);8 T! i& P. B! C0 i b4 I, ^
- rs=past.executeQuery();8 J; Z4 }/ ]5 S0 H; |8 k% H
- while(rs.next()){: D' I( ?8 n' X, D7 a" ~
- Book books =new Book();& ?0 D# d0 B7 R& X; @1 o* Q* B5 ^
- books.setBook_name(rs.getString("book_name"));
j' @) U; f# k6 K% l3 z! r - books.setBook_type(rs.getString("book_type"));' F( C8 ^; ]( _
- books.setBook_publish(rs.getString("book_publish"));2 u" i. Z5 t5 O, A8 _: ^* y
- books.setBook_date(rs.getString("book_date"));- Y* Q: j9 C6 o0 r
- list.add(books);
9 q9 M' b! \+ @5 v& [9 } - }
" h O: k# Z5 G; U/ p - } catch (SQLException e) {
+ b- d( a' z5 D; {( T/ @# d/ v - // TODO Auto-generated catch block
( i" y' A* U2 x! x( O" o) Z3 Q - e.printStackTrace();
$ y) X9 T; n$ G: l5 S9 ~3 S/ ~ - }
1 E0 r' W X' p4 i m - return list;6 P& `, Q! k4 Y& d! l7 Q
- $ p1 W5 M6 m( e/ I5 a
- }1 m8 V& I: E# s# i" M. T& T
- public int bookInsert(Book book){' R3 p% r& s) s& n% A, L3 R
- String sql="insert into book values(?,?,?,?)";
V6 R" `$ \2 ~ - String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
0 e9 q& V& r9 v, O& j - return update(sql,getVlaues); , k1 y) f; L# ?
- }
' v/ u3 G1 M0 [9 l8 Z9 [, Y - public List listtAll(){
$ w J0 J% V6 Q+ P - List list=new ArrayList();
1 c) E, M/ o. v* {! w - String sql="select * from book";' `! r- T# P" P9 W% E, n5 \
- Connection con=getConnection();3 K! Q! m( _) t
- PreparedStatement past=null;
6 `) I+ A' ?* Z' l$ v1 G2 D - ResultSet rs=null;" u) H# D: O$ c: D( p/ f9 f: J" }
- try {
3 c) l- o, I. v& b( K7 D/ w - past=con.prepareStatement(sql);+ n( e* S4 ]1 _# f& H
- rs=past.executeQuery();
! z0 N: X/ E$ i - while(rs.next()){
! h$ `; V4 J& x; E4 \1 ]/ y - Book books =new Book();
8 |' A `4 ?: X! }* T+ E4 T - books.setBook_name(rs.getString("book_name"));
6 `5 ~; |- C+ _9 ]& W) x2 q - books.setBook_type(rs.getString("book_type"));5 A; O; G3 g8 U1 j
- books.setBook_publish(rs.getString("book_publish"));: C3 t. _& U. Q6 d& J2 j
- books.setBook_date(rs.getString("book_date"));. X+ r$ c& x! B. F0 t% I! @' w! X
- list.add(books);+ D" p8 Q# u7 L! e( H: E
- }8 O7 X3 l3 M% h0 d1 Q
- } catch (SQLException e) {
]* X+ ^) k5 _3 Z- D2 v' C - // TODO Auto-generated catch block
0 Y0 r6 E4 B3 B0 e2 m+ _ - e.printStackTrace();! A/ h+ w h5 Z& y
- }
# Z) T& o2 l4 Q1 y) b* L* h - return list;: B8 F7 n' N+ w. x6 V4 y5 Z6 W
- 1 Z2 x( m1 r6 t: L1 ^/ G; y. ~
- }
, m: U* N6 B9 b ]9 x6 { - }% _. ~- K e$ |( H& O; [: Z5 R6 e
复制代码 源码下载地址:点击下载
2 C9 i4 @4 y+ x+ D
4 s: A5 a" L2 z2 H& P% d3 b
! G7 j8 M @; @) D0 Y- m& O% ] |
|