该用户从未签到
|
- package s2.jsp.zhangxiao.dao;0 q5 L) {# \% V8 v: p( J0 G
- import java.sql.PreparedStatement;) C3 q9 f9 _# g, j
- import java.sql.ResultSet;" ]$ d( {& [# f6 b/ g' d( w* A
- import java.sql.Connection;
+ S, ?2 O2 G) |! S - import java.sql.DriverManager;
8 t( L c' H3 a3 _5 d - import java.sql.SQLException;
$ K; U+ R9 G7 u. r - import java.sql.SQLException;
: Q) M' U. b8 `! G$ V; R* O - 8 Q1 n: o, S8 f3 |' J+ l% p. k
- : N4 o' r' D5 A1 q
- 9 O0 W" k1 i& l+ Z( @" k( S
- /**
4 T; X9 b$ K( A# O! t - * 连接数据库
: `2 ^7 h& B6 V$ m0 k - * @author student1$ V! d4 ]% j7 O
- *
9 J, m/ M j1 x' w/ U; D2 l) `. ~ - */% y0 @" O% _- h& g- o7 K
- public class BookDao {
3 b+ C- j0 [. t+ Z, L
. J+ g+ M+ \. n9 c k% x4 t- J- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
$ \+ H) o1 ~8 D G1 F6 B - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";5 B/ o# i$ j" b7 V" w
- private static final String DBNAME="sa";7 F3 D! G7 B& f# M- p# k' O
- private static final String DBPASS="1234";% x/ I9 {4 ]) ]/ M0 h8 V
-
7 G, d. }4 A. z0 ?: O -
3 I& H# T. Q9 i' D+ y- c - public Connection getConnection(){4 d& _3 S/ X( }: a* c! X3 D. f/ E7 j
- Connection con=null;1 ~6 v+ N) j( [9 F
- try {9 c/ C& \( | h
- Class.forName(DRIVER);. E/ R l+ F4 A) a5 c
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);5 s( ^ i7 {2 u: a' `3 G
- } catch (ClassNotFoundException e) {
7 V G" I% @1 W, m4 t - // TODO Auto-generated catch block6 v2 v5 ~* r- C9 d8 q, i
- e.printStackTrace();1 \* G* a/ J. H% H' `) d
- } catch (SQLException e) {$ T" E3 `$ w. O. m; v+ J5 I) O k
- // TODO Auto-generated catch block1 p, q4 _0 Y- i6 _: t
- e.printStackTrace();
/ e. P ?% U; d - }6 o$ H3 z, t+ X. c# ^+ l
* N- ]4 p' w6 e- return con;% x( @" p6 O$ t" c) L* t; j
-
+ K9 E- }2 H* ?" f0 t - }% H( i3 b$ y, ^8 y$ t
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){$ G' M5 |' K: ~ ^ l2 F- J
- try {) G, L% D% X P2 l) o/ d5 U
- rs.close();% b. x% S5 ?1 t* X X+ M1 Y$ j4 K; R
- } catch (SQLException e) {. [) x# O; w8 c' z. l7 f M; {
- // TODO Auto-generated catch block
2 b9 f, `; u3 G7 O - e.printStackTrace();6 _. s- s, V# W- W$ ^* M
- }# [- A. G, D4 J: w3 l/ C
- try {3 c1 l1 M( ?, _1 e
- past.cancel();/ M' S! o7 G4 k* u: h' k" K
- } catch (SQLException e) {
) y% w1 B H, n! v8 @ - // TODO Auto-generated catch block5 @! o3 y. g$ A# ^2 g
- e.printStackTrace();( i. ^. b4 `! H: X" `- ?+ G* m
- }
: l2 i* V. O, r6 o - try {4 Q0 Q) ]8 i5 @
- con.close();
3 O5 {' P- F. H% g7 q( r# @ - } catch (SQLException e) {
; E X) M2 P& \" b" g2 B - // TODO Auto-generated catch block Q0 B: M5 K$ C
- e.printStackTrace();, O; P, u) \7 n. p# `3 x8 F
- }
1 r' a* R1 |# c! \ - }
4 ] J. }$ `4 P6 c; b! [ -
1 Z' d" ?2 k6 {+ K/ _ - public int update(String sql,String[]getValues){" d( Q0 n6 n; w" c h7 t* u
- int i=0;3 y, G: T/ ^5 j D0 ?
- Connection con=null;
Z, ?0 i: K# A; R) } - PreparedStatement past =null;
# X8 D4 F Q+ j - con=getConnection();
8 G1 o1 X0 B% P n - try {
" l) _/ N$ B( A - past=con.prepareStatement(sql);( o: K; N6 W( E) M5 O
- if(getValues!=null){
+ D7 t/ ` D* q: N# z - for (int j = 0; j < getValues.length; j++) {
9 a6 N0 x H/ E( Z3 L - past.setString(j+1, getValues[j]);) A! ?/ Z$ ]8 o y. A' Z, T
- }
" d+ V* T& E5 q% D - } s4 b3 z4 U3 C* K. l4 S
- i=past.executeUpdate();: l7 \1 _; ?' V* o3 K, J' s
- } catch (SQLException e) {
; I3 o; j0 R' {; a - // TODO Auto-generated catch block5 I& W4 ^/ D, c. a. w& Z
- e.printStackTrace();
' B! p8 S& ^" |% U. l: L - }3 D2 _0 O/ W0 _" S8 m0 u
-
% s' ` z3 e4 a+ r6 D* y/ g - return i;
" @$ n& I5 I+ T -
/ y, z% J& t$ M( X O - }
: A' R) ^* g+ |) ] - }7 b! w& W2 \' X+ J |( @" A: P
复制代码- package s2.jsp.zhangxiao.dao;
9 b: t% g; ]5 K$ i1 X
3 ^6 _3 E7 u# W. X2 J8 P9 p) M- import java.sql.Connection;0 Z9 p2 e2 n( m' M) R( W, D/ `5 P
- import java.util.ArrayList;
. C& b4 P1 W$ T1 I! h/ A* i - import java.util.List;6 g" n) N6 _, }. b; H: W' e
- import java.sql.PreparedStatement;4 @+ x. V4 }/ A/ P
- import java.sql.ResultSet;! @) l2 @9 T! R+ T; K7 T+ j
- import java.sql.SQLException;
2 l: j, l$ L+ T5 f
# A( E) V5 Y9 M; c* @- import s2.jsp.zhangxiao.entity.Book;
& p5 f3 t' B( [2 t* Y" @7 a, k - ( c0 M% d, @5 G
- public class BookImpl extends BookDao {5 \, Y, q. }. ]/ w) [5 ^
- 2 C: I$ R) s9 C+ f0 w& P
-
% L! a. d; Y6 A, W$ \5 r - public List bookAll(Book book){
9 z- t9 i) z9 a: L' q - List list=new ArrayList();' M8 g& P9 R% J; r* z2 ?
- String sql="select * from book where 1=1";
5 z5 }4 V& H% {4 z - Connection con=getConnection();
. n/ P$ N- z B' u6 Y. f - PreparedStatement past=null;; n' ^5 J7 M6 I2 N' a: S
- ResultSet rs=null;
8 y1 K* D! L9 z6 `6 W. I - if(book.getBook_name()!=""){- B& c, y4 G" s
- sql=sql+"and book_name='"+book.getBook_name()+"'";
$ k, g0 L* e+ H5 v" J - }9 G6 v( @: D" e: I
- if(book.getBook_type()!=""){
' D& s& x: ^8 v( P4 { - sql=sql+"and book_type='"+book.getBook_type()+"'";
- {6 T% c' {9 w - }8 A3 b" w+ o! |
- if(book.getBook_publish()!=""){
; M% A4 V9 i8 d; O - sql=sql+"and book_publish='"+book.getBook_publish()+"'";
* _4 Z* A. L4 }: f+ e7 { - }8 d8 o, F) a# H- @
- ( o$ m/ t0 K( u' p0 x& t% V
- try {
2 i. T. ?# P; v - past=con.prepareStatement(sql);
4 w, m8 O' x. \! ^) k+ D - rs=past.executeQuery();
$ U- W4 s; j; ? - while(rs.next()){ y5 T0 w; A, O
- Book books =new Book();
+ [- y1 e2 c8 L0 L - books.setBook_name(rs.getString("book_name"));
% Q2 W, F( t( P6 C - books.setBook_type(rs.getString("book_type"));/ l3 q2 Q5 O1 Q4 b4 V1 ~9 D3 ?
- books.setBook_publish(rs.getString("book_publish"));0 p N7 h; o: _
- books.setBook_date(rs.getString("book_date"));
# `5 l+ R# a- {4 k8 G2 C5 L p - list.add(books);( a0 m8 k$ o) A
- }
4 ^8 p' |8 X) }% I - } catch (SQLException e) {
5 c G* M% |: W8 Z - // TODO Auto-generated catch block5 v' ~3 t0 O1 L2 P& a
- e.printStackTrace();; V9 a& f. e4 X+ r. `" C2 m
- }
3 g5 I6 Z8 V1 }0 m7 C$ d - return list;9 ?$ k6 J$ C+ T1 i2 o2 J
- $ ]( q5 L# w9 L- k* g2 d
- }9 d9 R2 J& x7 K# A
- public int bookInsert(Book book){) Z) T) J. H) m; U; n1 f
- String sql="insert into book values(?,?,?,?)";
3 u2 `' F& @8 l( O' k0 M" F - String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
* r0 C$ I# n C5 W# ~ - return update(sql,getVlaues);
/ i( q M1 o2 I - }, m n- f' J9 w+ X
- public List listtAll(){/ v: J; G8 q; `' o, C, T
- List list=new ArrayList();
6 e/ I% y1 e" z$ B - String sql="select * from book";
/ R5 g4 H3 A b* G# y' ] - Connection con=getConnection();$ |4 ]! p$ n# @) M3 G4 m
- PreparedStatement past=null;
Y8 b/ Y0 E9 m7 p/ L1 x) j - ResultSet rs=null;' W6 c( J7 X4 p o% {1 z5 A
- try {" m$ ?& p8 `0 S. u/ J/ Q# _
- past=con.prepareStatement(sql);6 k5 Y7 I7 Q7 v( [
- rs=past.executeQuery();
6 |( n! q, U- Q; {8 l7 q - while(rs.next()){
2 Z5 I) g- H6 B0 c2 l/ Z - Book books =new Book();, U @8 |( Q6 D- t
- books.setBook_name(rs.getString("book_name"));
' h* a# {$ g# a! ? - books.setBook_type(rs.getString("book_type"));
" g! g. i+ Y3 S4 q- B: x, y - books.setBook_publish(rs.getString("book_publish"));
) \5 d$ G, [0 z% c" B - books.setBook_date(rs.getString("book_date"));
7 r; l1 j& b& @; W - list.add(books);9 K9 o' d1 m' w3 I3 v" _
- }
0 E$ ?$ s. B& r1 n! U. X - } catch (SQLException e) {5 S& ]+ p$ n# D! d; @( ^. [+ B
- // TODO Auto-generated catch block; W h" j. ^2 X$ w
- e.printStackTrace();
4 G! y+ R7 L# y# t) | - }( E- H0 H/ _+ O Y% B
- return list;
+ V" y" W4 b3 x* J6 a' p: H% X -
. h6 S% @# ^$ F9 a! `; |7 L - }1 F, ]% Z! r+ k5 Z- E1 p# w
- }
) |( p2 Z+ u; y g. m5 j1 T3 `4 y+ a
复制代码 源码下载地址:点击下载* ?8 f" N5 |! `) I1 T
$ e. D9 y2 y" Q. i: ?
& N6 d5 Y, m' l3 s
|
|