该用户从未签到
|
- package s2.jsp.zhangxiao.dao;/ T1 G$ ]8 }$ m7 L7 v4 z; e6 n9 [
- import java.sql.PreparedStatement;% U% |3 i3 u& [2 J5 Z9 F
- import java.sql.ResultSet;. x& L' k& w4 Z$ }
- import java.sql.Connection;0 i2 ~0 \5 K0 E1 Z& f6 u: ?
- import java.sql.DriverManager;
2 ~" p* f- y- y6 Q( \! Q - import java.sql.SQLException;
* s" o4 \7 ~- b* @: q, D - import java.sql.SQLException;6 b f$ n$ [: b" P( {
H7 @% a+ u7 R
! \& X3 r) `0 ]9 Z7 u
/ [1 _5 H6 Y6 n: s2 M4 U$ \, b& `- /**/ s- E$ u' c& Y( s s3 _
- * 连接数据库3 d% n; b t( K! I
- * @author student1
N% _+ Z0 {( a5 c5 X - *
& E' P+ g8 K T$ k- ?* ` - */
+ e( ]& p6 m! [6 q8 I* i - public class BookDao {
) I, U4 g& z" c - : ?& X$ a# h6 [# J
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";' E$ ~$ N+ q1 x3 X
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";8 V% P% C- E( E5 }4 w$ I6 W
- private static final String DBNAME="sa";, e' f5 j+ }6 H y/ d9 e
- private static final String DBPASS="1234";3 u: W- o, S [/ @% X0 Y: n
- , A' V( X. \% P
- & b) X0 o4 m' ]4 g% d) B- H% Z
- public Connection getConnection(){8 L) b# G4 b& Y: e9 e. q
- Connection con=null;
$ Q% ]5 Y$ l1 i! G) L - try {) X7 c% o5 ^3 _2 ~3 [! z
- Class.forName(DRIVER);
+ k) e2 e+ j5 o2 S4 p* \/ U - con=DriverManager.getConnection(URL, DBNAME, DBPASS);& D7 a, u, M) F6 n
- } catch (ClassNotFoundException e) {
: U6 A* ?- ], L/ X$ h( n - // TODO Auto-generated catch block: v# t' L/ i9 a* v8 ?9 ?
- e.printStackTrace();
! j2 `& b/ T# e - } catch (SQLException e) {; u) L) d2 v5 x; S4 |/ {5 L
- // TODO Auto-generated catch block
( ^' m; H- h# g: y: d - e.printStackTrace();1 ?4 T3 E- i; `! L* V$ X' Q! L- g
- }5 u3 I5 d& |6 G6 ?) W1 f, R' Z+ _0 O2 `
( g9 g; v( b9 Q7 R# F! ]: ~- return con;8 r! F; U. V0 c; F/ D
-
! d# n& O [+ n \9 S - }
5 }, e3 b( S" Z! B' B - public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
7 w+ o2 ]9 d5 R1 H - try {6 M/ }9 P) o% W
- rs.close();2 a6 }2 [3 c2 p: e$ |
- } catch (SQLException e) {
* s' B; H0 u) f$ Y0 w" ]" m - // TODO Auto-generated catch block
1 N0 P) K) z; } - e.printStackTrace();6 f: I0 `' _. Y+ i
- }
! I. p8 z5 x) H% s8 A* B+ r - try {
9 u# L G3 C2 c7 ^( T - past.cancel();
3 X1 l3 a8 T- E/ ]" r9 Y - } catch (SQLException e) {
, w2 ~" |& s7 {% o7 v& \ - // TODO Auto-generated catch block* W* @5 P8 v' r; U& Y
- e.printStackTrace();
) @* ?/ `4 a3 O! D8 d3 b$ W - }
" i, S3 A& f; B+ a0 M( b - try {* p) H& H" t' A
- con.close();
4 \; ~. [! ^+ `/ n1 v$ m K- B - } catch (SQLException e) {
* N; }# F: [2 S: K' p% d7 a2 c - // TODO Auto-generated catch block
0 `; x, _4 `* G! d - e.printStackTrace();
( E1 \5 i+ k* X8 j - }
0 U; P9 i! m, { - }; o$ Y9 ^. a6 c# ]
- + V1 F" u2 q. a1 e
- public int update(String sql,String[]getValues){; G% ~* N" o9 w# P( p
- int i=0;
1 q7 f1 Q% Y( P0 u5 x% q - Connection con=null;9 n4 q8 R: b7 E9 \
- PreparedStatement past =null;
' D2 f t9 D+ U5 G1 C - con=getConnection();2 R2 a+ E4 [. w: Z4 t* M
- try {6 a$ T& l) t/ @! m# o) g
- past=con.prepareStatement(sql);
2 w) n- i$ S0 N - if(getValues!=null){( u: b7 M# S. u1 `) I0 S$ | k6 N
- for (int j = 0; j < getValues.length; j++) {# n3 g/ J) g1 w
- past.setString(j+1, getValues[j]);- e" j/ n% g' _ W1 x6 i: Q3 }3 Z) @
- }* j7 \5 z" B K- k" a
- }. q! G3 N/ W) f, y) I& m5 ]
- i=past.executeUpdate();
% H9 E3 n: _+ b! v* W) @; ]( l - } catch (SQLException e) {
8 `3 J, }$ }: X; P. x2 m0 | - // TODO Auto-generated catch block
: U* C6 x$ x+ u6 t( o/ h* c. Z: z - e.printStackTrace();3 z5 u& d, H% J+ L
- }
& m% ?/ K @) c+ U& T5 C% J - ) r5 o$ w$ a# u* r
- return i;
3 h7 T. g0 r( }- a c -
: k6 U% V! V7 D; @8 g" {# K* P. i" I - }0 f+ C3 t! L, d) k$ ~. t4 G
- }1 K5 \/ @6 p4 f8 W. l5 U
复制代码
7 K. o/ K2 E- i' J
7 A7 N; \* v& n: O" s; Q# f( u- package s2.jsp.zhangxiao.dao;
) _; \& ]4 F/ r& @" |$ s# R$ p
, ~0 S, [. V. r( _6 Z- import java.sql.Connection;
1 j0 k/ p$ E2 u$ t4 v1 \ - import java.util.ArrayList;
$ M5 V9 Y/ }3 j$ Y' V - import java.util.List;2 T q0 D# h) _: C- R
- import java.sql.PreparedStatement;( |( |# B9 P b Y8 p3 ?
- import java.sql.ResultSet;) Q$ b5 T7 U# v( \6 l" Q4 Q& C- |
- import java.sql.SQLException;. j5 j, I3 B( B5 e; f
- % ?) \1 w: i6 A; O/ P# B6 @/ H8 T a
- import s2.jsp.zhangxiao.entity.Book;& h7 h% f; }) R `9 c% Z9 p
% s3 o# _1 }/ ^1 [3 i1 _: Z- public class BookImpl extends BookDao {
9 s; L' ]# V3 z& w/ B. ~ - : b; P$ G4 Q( ?& I7 a
-
, Q7 _. y& v: J: w3 e - public List bookAll(Book book){
8 x! _, U1 c% _) s& O4 `5 v% s - List list=new ArrayList();
3 S8 h2 j9 _; d3 h! o T - String sql="select * from book where 1=1";
. L3 y' T4 C/ G/ \' @) U. f# K( m1 H2 n - Connection con=getConnection();
8 z& m) V% a) O7 H, C' K - PreparedStatement past=null;1 p3 V9 z+ v6 X4 u7 K. K
- ResultSet rs=null;" p. S. V, T) b( u2 H5 h
- if(book.getBook_name()!=""){
' S- |* i) s9 w0 B6 ?" Q - sql=sql+"and book_name='"+book.getBook_name()+"'";
. V+ t4 H' {( h2 d1 w - }
2 v( o2 _1 i* z# V7 L& ^ - if(book.getBook_type()!=""){; O8 r$ |' j9 `9 l8 j
- sql=sql+"and book_type='"+book.getBook_type()+"'";
9 E8 c8 W& y5 ?1 H# V - }8 h5 X6 E( z/ t+ K+ F+ _
- if(book.getBook_publish()!=""){8 M) a3 [& N s; ]4 f; N
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";7 U) {: f/ E+ L
- }
7 P8 F5 K% k( M- V2 A1 k3 X% n - 6 p) q7 ]/ e ?0 b* h- G
- try {
% t. G3 i4 {" |9 a+ U R& y9 \2 g - past=con.prepareStatement(sql);
0 }& ~% k" d& |7 X - rs=past.executeQuery();3 d6 j2 s0 n7 A W' G
- while(rs.next()){! g" J$ R% ]" j" `
- Book books =new Book();
1 Q' ~, R. s9 Q$ I7 _4 |& @6 H - books.setBook_name(rs.getString("book_name"));0 z" i3 @2 Z9 d
- books.setBook_type(rs.getString("book_type"));$ G( V) X8 ]% s; {; b5 \
- books.setBook_publish(rs.getString("book_publish"));
- R" X$ }2 ~+ X) M6 m! T - books.setBook_date(rs.getString("book_date"));6 s4 X7 l+ E% X+ q' f
- list.add(books);, o0 M! H- g+ Q; C# {0 {
- } M! f3 k! e% d3 I
- } catch (SQLException e) {( S" }' O- p; W# O/ S( D
- // TODO Auto-generated catch block/ o! a; q" L/ j7 W6 n
- e.printStackTrace();
0 x) Y: V* J2 n - }
, i. V* x' U* L3 f6 c- \$ h - return list;
* Z, h( _3 X: Y+ S' `4 ~3 z7 ` -
3 h" d% o8 G9 ~2 f. k - }! d( G+ x# z& D& K" ^
- public int bookInsert(Book book){3 g$ s1 C0 n4 v# y) p8 P6 [
- String sql="insert into book values(?,?,?,?)";
3 Q' T" U. m9 u( m$ a5 a - String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};9 [& I O0 m1 Z/ _ O8 f
- return update(sql,getVlaues);
1 A" w5 _& r2 X9 y: N' U - }) n3 O1 y7 Z3 a# F9 G
- public List listtAll(){
) o9 G8 H6 E1 m, M" z+ ` - List list=new ArrayList();
7 X3 A' Z# j2 u, {# a - String sql="select * from book";
" _+ i J0 y/ [; w3 M4 ? - Connection con=getConnection();
$ _ R7 b S* c" A8 R - PreparedStatement past=null;
" h7 _8 ]" {! t* W% R1 w5 ] - ResultSet rs=null;% r$ J% ^" E4 z/ ]6 C' o
- try {
, a: C# e5 K+ J - past=con.prepareStatement(sql);
% S* s2 C0 ~: E [- i7 ~% m - rs=past.executeQuery();
6 _& H6 E( ?1 m' O5 D$ Z: a - while(rs.next()){
h: Z$ T: O* [. V - Book books =new Book();
" ^- o& W' C1 c6 K/ l. g) t - books.setBook_name(rs.getString("book_name"));
) u# R( R3 o, g! { - books.setBook_type(rs.getString("book_type"));
8 o7 s b! d; o3 N0 s1 |% T* R - books.setBook_publish(rs.getString("book_publish"));
7 x" Q/ c5 C: c. P, u: |' V4 ^ - books.setBook_date(rs.getString("book_date"));
$ R. r. I& i; U6 A+ s( t6 r4 x, Y - list.add(books);& U# O- o- j+ o+ n* k) S" i2 U
- }
. @) u% Q- H+ C2 o( v4 }7 T S3 o - } catch (SQLException e) {
$ r. i5 ]) N4 W$ V5 s - // TODO Auto-generated catch block
1 b# `) _( _+ K8 w& L7 S2 Y% ?1 o - e.printStackTrace();0 l% \ Q, b1 l) _* t
- }7 G1 Q, T. h) p
- return list;
1 j3 R( G; Q, k. y% V. U -
Q2 @7 L; o2 v! e - }
" R: _: i$ ~6 E$ C9 B3 l, ]1 } - }0 `1 r+ c. @4 h% b7 Q. l
复制代码 ! H& p7 F5 U" M0 i: S7 F' p
2 \- \/ \% j' k9 x
; ^( E: Q2 O9 O x% R( q- V. A
|
|