该用户从未签到
|
- package s2.jsp.zhangxiao.dao;# T+ N# @& n' k" t9 ]& X
- import java.sql.PreparedStatement;
; z. p) F8 G& W/ ~ - import java.sql.ResultSet;5 l c& x( a6 u3 I
- import java.sql.Connection;' B5 j9 f- l# a! U; r$ C0 E
- import java.sql.DriverManager;
) v7 E. Q' ~$ Y& l ? - import java.sql.SQLException;
& U7 D, z4 T0 h1 H1 d4 o4 l' @. X - import java.sql.SQLException;% m1 U# B# u! S% Y% y0 H9 I" E
8 Y* X& w; V( J3 O' P) Q" j
_4 v- i4 L* t
8 p3 H9 r* I# N3 o; v- /**7 f& F! R0 I- N5 [ W8 l$ G p7 K
- * 连接数据库) a" V6 {; X3 ]
- * @author student1
6 M- K5 ~$ _: w h( u - *
( [/ m3 ^3 b* V$ l - */$ [# h) b0 Q/ P2 {3 I
- public class BookDao {* X0 E4 H) Z0 ]
; ^- n8 k2 ]4 H9 v/ h- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";+ n+ S5 g8 v8 I
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";
, x5 Y/ `7 b* B# g1 G - private static final String DBNAME="sa";3 d& b2 a" Q# _) T; d
- private static final String DBPASS="1234";: s# p) D& N7 @$ u) w
-
2 ~: d! K' y2 V/ w" p -
' i4 ^; L3 w" H - public Connection getConnection(){
8 ~9 `3 u+ t4 C z6 v* [* a8 p - Connection con=null;
4 {+ l! L9 i" c5 x1 f - try {; L) F: M6 }/ ]* v# O* y* u
- Class.forName(DRIVER);
, G1 ?. r5 s3 Q3 i - con=DriverManager.getConnection(URL, DBNAME, DBPASS); H7 t; F/ J( g/ v6 D) M- ?
- } catch (ClassNotFoundException e) {
, K+ v7 L( ]; y/ F; w, d - // TODO Auto-generated catch block3 Z9 w% E: q' I3 x& w
- e.printStackTrace();, W. N8 j4 Q v& g
- } catch (SQLException e) {2 V, h A3 _1 M! g- q, Y# l& v! W
- // TODO Auto-generated catch block5 ~, d, y& H' d6 r3 q- R; a% d' {0 V
- e.printStackTrace();
. S) H) h x5 d0 f! | - }, t/ w) H; G! \4 A6 x7 P8 Y
$ d' X9 P, O9 e1 D9 u- return con;
4 i% h1 r1 r+ h2 Y# K* R - & r/ v; Q& j9 w% Q* h$ E
- }* d9 i) c" j$ h# G
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
/ _& E; \1 W4 g8 F+ }# H) E. i" R7 H - try {
+ x9 c; H2 L6 e+ a% X - rs.close();/ F% d( }; }/ a2 E( I, T
- } catch (SQLException e) {
# N7 [3 j+ t) B9 j t+ G9 b9 P - // TODO Auto-generated catch block
5 J; `0 \% t, `# L9 w - e.printStackTrace();9 R$ x+ U1 W# I% i
- }
" I5 R# _$ o. H" {& U. m) k - try {7 ~7 X* ^+ U7 U- O
- past.cancel();
# B7 E# x! e0 F - } catch (SQLException e) {2 n4 L1 F) C6 R0 p% b9 [# B) f
- // TODO Auto-generated catch block
2 e; b/ B- `! ?6 f" s6 w# N$ k - e.printStackTrace();
) g8 a4 X, ?# s6 \$ A - }: ~! s2 l. c, O. {
- try {- j% G0 s+ t( R/ d5 W2 L' |7 D
- con.close();
' P: T' e! e! ?) }6 n - } catch (SQLException e) {
3 g6 G; K" q H - // TODO Auto-generated catch block, z, v% p+ g* e7 P/ _. }2 V$ R5 @
- e.printStackTrace();3 a6 E! k& I) n8 V
- }
. T8 A* q: |9 w% R8 ^* A9 _ - }
8 N1 { ]1 o) p -
- R' w3 \& r4 |# f+ M - public int update(String sql,String[]getValues){+ W- y7 J& ?# X0 l# J
- int i=0;) ^6 B$ o$ T8 d5 n( l g* t
- Connection con=null;
; {% W8 o V/ I R2 u. P" M( g& L - PreparedStatement past =null;
. R; I0 b$ ~+ D# t - con=getConnection();( p% j8 Z k$ f; C9 Q1 E
- try {
0 {9 _) u5 l0 E! M. l w, y - past=con.prepareStatement(sql);4 t2 J+ K4 P% I; n' s3 f# O5 ?
- if(getValues!=null){ F0 K. R( H- K+ k
- for (int j = 0; j < getValues.length; j++) { `* }/ y; x+ t F
- past.setString(j+1, getValues[j]);
3 N6 j7 X" d: |( M+ h( U$ v - }+ i3 [1 V6 [7 V: j/ U2 D2 m
- }
4 E: w8 _$ i- G, r8 U8 k - i=past.executeUpdate();& u- n6 ]' C# D4 C: u/ ?
- } catch (SQLException e) {, v) K; g/ T2 y+ [6 e4 I! c
- // TODO Auto-generated catch block
- A; S) B1 A& a0 m5 w, }5 I' v, w - e.printStackTrace();
8 t# R# m0 K8 D, S - }
7 \9 Y2 B9 v* W1 D8 U - + A& \: H8 p" m0 c, `
- return i;
3 b; A; B" B* H G -
% ~' T, Y& t" U# E! g! U - }
0 Q' k+ N i6 b( g - }
8 X% q* R; {. w4 k+ A! p4 c6 _. K2 f
复制代码
$ U! \0 y5 C3 S, l
( E) j( D0 y& Z5 d- package s2.jsp.zhangxiao.dao;
, }( P7 ~+ @/ M; k7 {" J+ W. m
% V1 r5 N }$ g$ t- import java.sql.Connection;
$ h! g7 l, i+ O. o+ X. m9 ^ - import java.util.ArrayList;1 W" d6 B9 Y8 t+ [0 d
- import java.util.List;$ U* x2 i5 K+ _* |( ?, h
- import java.sql.PreparedStatement;
6 T) E" I: a0 H' S3 d - import java.sql.ResultSet;
* {( q# V1 ^3 s+ G; l Y( l - import java.sql.SQLException;1 N7 F9 x3 \( \' j6 O
: e! `- S9 s; R9 l$ y" S- import s2.jsp.zhangxiao.entity.Book;
2 ^) q5 d7 V2 m* P1 J+ \0 _
" n6 L$ `+ E8 N1 R. I0 @2 K% u- ~- public class BookImpl extends BookDao {: r4 J# c# a; G& M
- ! ?- U3 J6 V' Q$ ]) ~4 ]
-
* x% a. F) u; o0 e) R; A - public List bookAll(Book book){
- ~6 r. d; x" w( ]9 c D' M: _% D - List list=new ArrayList();
' r* u6 G+ r- @ - String sql="select * from book where 1=1";
: N6 \' ^8 e% ?* x6 c: X- ] - Connection con=getConnection();
6 k- j6 | V% a+ k, z$ X - PreparedStatement past=null;; L" L, O2 W# W0 j) _ K
- ResultSet rs=null;% H. D: b3 T8 r5 c! ?5 y
- if(book.getBook_name()!=""){
" t7 s# K: ~. A+ P! U - sql=sql+"and book_name='"+book.getBook_name()+"'";# Y( D% Y/ j3 ~* x7 i, b: P1 x; Q; W
- }
W$ r0 `9 F8 y4 @ - if(book.getBook_type()!=""){6 f0 o4 Z3 k+ C, r/ `' @1 W
- sql=sql+"and book_type='"+book.getBook_type()+"'";2 M) E$ C5 a4 f D9 E6 ]9 A
- }- S; p: u9 F5 `; P& D$ L/ A
- if(book.getBook_publish()!=""){; ?/ S: \# e2 F0 ^/ z- Y* J
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";4 a9 p! s) i3 V4 } j; Y8 u, K
- }# N: D1 p( f0 U$ J5 p1 Q
-
- Z' E% t. Y& N6 U# }" a - try {6 F ^4 w" @* H# L9 A: V; p
- past=con.prepareStatement(sql);
' p5 p+ u( L. v- ~ W - rs=past.executeQuery();4 W; J1 g0 ]2 v4 E7 o
- while(rs.next()){$ x$ Z8 L8 L% [$ o2 V6 F+ D
- Book books =new Book();, C2 n* @! [9 U% N+ a
- books.setBook_name(rs.getString("book_name"));
0 ?" a! M2 F1 t) c# I8 f. @ - books.setBook_type(rs.getString("book_type"));
* g8 z- S; K. x' ^- U+ ], z* Q, P - books.setBook_publish(rs.getString("book_publish"));
! h% E" G6 O" l6 n5 D9 Q - books.setBook_date(rs.getString("book_date"));3 E2 F+ y- E" o( ]6 v( ^. ?) s- J8 P
- list.add(books);
' ~. f6 Y( o, T% e$ L - }3 T8 e# }4 }! R% o3 K# U
- } catch (SQLException e) {- A0 U4 R3 i9 U; [/ u# ^* {7 v/ h
- // TODO Auto-generated catch block
2 L5 m! d- g( F( u; T& w7 G7 } - e.printStackTrace();; `+ a. M% C$ f) M
- }
+ {* J& n# C- r k - return list;
) N/ V6 h; B8 Y) r0 ]' A$ W - " P1 m1 [- q" v0 v, h
- }
5 U3 K) |# P' a) ?! D+ u6 ?7 M - public int bookInsert(Book book){
7 Q. F" {, y* n' i6 Y8 `+ a9 v - String sql="insert into book values(?,?,?,?)";: e1 }+ V' `% K
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};+ T: F1 f0 l# K( k
- return update(sql,getVlaues);
( G5 u% D. c' N- J0 h% U, V1 G9 L - }
! u8 d, W. } E$ r7 F; }' M - public List listtAll(){. V( q( @. d+ z- v7 X
- List list=new ArrayList();
$ S% ^* }5 x1 V' U8 Z+ E - String sql="select * from book";" d" q! ]9 K% d5 ?6 a
- Connection con=getConnection();0 ]; H. p: X+ C
- PreparedStatement past=null;
4 w0 c( N( M+ f- i/ k( Q' Y - ResultSet rs=null;3 q2 R- |: y+ m
- try {2 u( j6 I, F' y. r1 V0 J
- past=con.prepareStatement(sql);
3 z1 {1 K3 H& G* y - rs=past.executeQuery();
* |. I% D: C2 g) A - while(rs.next()){
- }0 {# w( S% Y) p4 f1 b - Book books =new Book();
2 \0 g9 a& }7 u% E - books.setBook_name(rs.getString("book_name"));
4 {( [1 x9 u. B - books.setBook_type(rs.getString("book_type"));! V! o; U( Y9 G3 y! ]- L
- books.setBook_publish(rs.getString("book_publish"));! l) Z6 a/ d' C7 U3 Z6 q7 v
- books.setBook_date(rs.getString("book_date"));
9 t' z. [7 h" M$ ^ - list.add(books);
8 Q9 s3 H, |* R# } - }! c% ?6 X @# G x3 P$ A' F
- } catch (SQLException e) {% V( M) {0 e5 n0 F3 C6 e X
- // TODO Auto-generated catch block
+ x" o. l% [# ~7 V& \9 i& X/ I- { - e.printStackTrace();
$ P& q2 j' z: x( x - }2 J. b( a7 H0 {" T' i
- return list;% P) ?' ]3 F9 V: n ]; m- z7 B
- " X7 l2 n* B6 M) @7 d
- }
, E9 L7 c$ |3 M6 k& ~ - }0 G; p, @/ g" U; @$ N9 X1 n
复制代码
# n7 |- U2 K9 v' [8 y/ |
& P8 C: s3 Q/ U6 C0 ?# A% q* N8 m
|
|