该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
/ g) }' h7 |' ] - import java.sql.PreparedStatement;7 p- l0 o5 g3 d x
- import java.sql.ResultSet;4 Y% W2 c# w1 n6 Y5 u. l' E4 a
- import java.sql.Connection;
) H/ E, N* ~5 \& y0 _8 }5 g* @ - import java.sql.DriverManager;
: S9 Q; X1 Q% ]" p; {1 E9 x - import java.sql.SQLException;1 e _: b, R3 {
- import java.sql.SQLException;; {5 Y( p% @- w5 M% a" e( K: m/ P
- ; G, S" t! s' i) ?$ i
- ' r3 S6 v! z% z+ B
- $ o( S$ l2 f3 r* _7 U
- /**$ V1 Q- v# f7 P4 {( I
- * 连接数据库" A2 v+ i0 s9 ?
- * @author student1& B" Q" Z X4 W2 b9 n) l& [
- *
5 a/ d/ o; R: W( S% [. D - */9 a8 e: f K5 u" A; V8 ~
- public class BookDao {
0 Y( h0 x& ]8 N/ X1 D1 U3 h
0 N/ Z' }$ {, s- X( s+ \9 q- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";7 l9 F( Q4 u- \. `/ P' |
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";8 q/ A) _1 Q; ^- t! i$ F
- private static final String DBNAME="sa";
+ q. L$ Z% H$ F/ Y* O - private static final String DBPASS="1234"; g4 U, B; T% R: L1 v: r
-
& U$ O! E2 p u0 ?2 }- j. ]# z2 ` -
! i4 X g2 F8 F" y% I - public Connection getConnection(){
: d$ W6 ^$ y" J& Y8 K' M1 O - Connection con=null;1 t! W# D O* a! T1 J
- try {0 v$ o( Q, e) W. H# T) N' F
- Class.forName(DRIVER);/ i' V7 I% n- W. n% U# E
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);4 Y( F# k* v2 _. a9 c2 L5 u. b
- } catch (ClassNotFoundException e) {7 K/ p/ |- ?! ]* g- V
- // TODO Auto-generated catch block
2 E* {/ L0 E5 V, _( P( h - e.printStackTrace();9 T: X; T w. t8 g7 ^; D5 C
- } catch (SQLException e) {8 U) p3 G# V) k6 ?* s
- // TODO Auto-generated catch block
; b" `# `5 t8 T3 C: B5 v - e.printStackTrace();( R( W. u4 F: c( K
- }
: z0 \; L: s2 c: g - & u* e* z' n8 I2 i
- return con;
1 i" [% `8 D& p% C' }: E1 e -
( P9 V5 x' f% a7 q - }6 q& z- Y8 }9 s" G) C. S
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
% X7 Z6 L: s: H# U9 z4 n - try {; I& N5 s/ }# r5 a3 _/ h. N$ _6 ]
- rs.close();
# }/ _$ I1 ^5 J' G4 S6 q; ?' @/ I - } catch (SQLException e) {
& n' t3 Y1 E: S. F+ [ T - // TODO Auto-generated catch block1 v) A8 f! ~9 _
- e.printStackTrace();/ V0 h0 S K8 z* d$ d! g& h# r, L \
- }
0 P, k. b7 F" {* n5 q% m - try {
- A+ S9 w, V$ P/ T/ V0 N/ f1 D - past.cancel(); Y8 K& U5 m+ t+ X8 d" ?6 M
- } catch (SQLException e) {1 f* C5 r+ m3 v: Y9 A
- // TODO Auto-generated catch block
; w& E4 Q0 \4 [( S7 b! I6 @. f - e.printStackTrace();
' z2 ]5 D9 d* I' ~2 H - }
5 Y% n: f% c6 V/ Q5 K! i# [6 { - try {
8 o X7 m- [ x - con.close();3 {9 e0 n2 ~% ~& N
- } catch (SQLException e) {+ Q9 m" I2 ?! e2 l0 U- w0 Y
- // TODO Auto-generated catch block
+ A2 @. j2 Q+ r% n7 i2 C# N - e.printStackTrace();
" s+ y$ c6 }) l6 ]0 Q$ `6 i; D' Q - }
; V6 V) |, N. V) m4 R4 I! o - }& s/ Y# h' a/ ~; F! b& P/ ~
-
3 o4 F; _, V# Q3 m8 @) q# \ - public int update(String sql,String[]getValues){& j; O+ s* Q; F: w; W) H# S
- int i=0;
- i* y4 a& e) I0 @ - Connection con=null;$ \. m' q' ~/ h/ N9 R4 l% h
- PreparedStatement past =null;
0 P5 K9 l+ S( X) t+ Q1 e1 x$ Z1 B" y - con=getConnection();
3 P& |: ?0 k6 k. [5 r - try {8 [! L# i; w3 L/ L* f9 }, B0 `. f5 u% v
- past=con.prepareStatement(sql);0 K% ?& g2 W( S# o( i% [- a
- if(getValues!=null){
, R3 q# Y8 f: i0 G" H - for (int j = 0; j < getValues.length; j++) {
3 p. H& z) H. _4 E( a - past.setString(j+1, getValues[j]);
! N0 T% C! V' f0 Y p3 H& N0 O2 a+ e/ U - }
; _) m ?, [6 s K' `. b9 r - }, E' @; S0 S$ w0 P
- i=past.executeUpdate();) f. `5 @$ }% ?, D
- } catch (SQLException e) {
?) V8 t& ^3 N, W/ }! a8 i - // TODO Auto-generated catch block6 d+ `* |+ A2 l7 {! y: e( T; p
- e.printStackTrace();. b6 O7 i# I& _3 Z: I
- }
# P0 J3 R. z! L/ L8 X - 0 k7 O- W8 S, P2 V
- return i;
' V# I2 ?) |& U+ n& l -
3 S, X% D H: I - }
; s8 I5 Z0 j: }+ ?- [% W: W - }) w$ N* c: s) L" [4 o" y& z; U
复制代码- package s2.jsp.zhangxiao.dao;
& ?. m% M5 M1 h/ H% R) D - & \ {$ K: z! H, {
- import java.sql.Connection;( j. V% Q: T1 w; ^0 z, Q
- import java.util.ArrayList;
* N% |- G4 k! n Y' k" b - import java.util.List;+ k3 j. I3 Z/ R+ U* ?2 h z% S
- import java.sql.PreparedStatement;; i4 M+ N! a2 _8 Q7 S
- import java.sql.ResultSet;) c7 ?8 K) }* _9 G* w6 {. ?/ n
- import java.sql.SQLException;
: Z1 R1 b6 ]- e4 _
' v; t8 q& N0 m% i5 t9 F5 e% c4 O; f- import s2.jsp.zhangxiao.entity.Book;
; J W1 w& o" |0 Z7 @' \7 q7 ~7 \ - 9 O% H+ S& x; `8 n% G( Y/ [! H" r
- public class BookImpl extends BookDao {6 T9 U, z3 n! {7 |5 J
- ! M. U% M4 R& l& L8 i. k
- 7 q$ s" n& T: F3 V2 z4 J
- public List bookAll(Book book){
! Y' x; X- y ?4 c - List list=new ArrayList();
. ] A+ z& ?" ]! s" f - String sql="select * from book where 1=1";) `! [$ \$ `$ E0 a- X
- Connection con=getConnection();
$ z+ `3 h, C5 j - PreparedStatement past=null;
7 K; L/ a# [0 { - ResultSet rs=null;' c- j6 m6 t- i/ M( I
- if(book.getBook_name()!=""){+ t O0 E) Q3 n; o' A5 |
- sql=sql+"and book_name='"+book.getBook_name()+"'";
6 b4 d2 S |8 Y; d9 p& u - }
; ^$ R$ x% _% ` - if(book.getBook_type()!=""){7 _; E0 Y$ i" e& x; I
- sql=sql+"and book_type='"+book.getBook_type()+"'";
' m- I5 t7 G4 w - }/ c- ?/ @4 S. w7 c2 e5 v4 M
- if(book.getBook_publish()!=""){
* T) J" Z* H5 W i; i. g* F - sql=sql+"and book_publish='"+book.getBook_publish()+"'";
* a. P1 X. T9 a! F! G/ Q - }
K* U+ q5 @: d+ l$ B) z1 P -
; B( s" A: ^6 Q7 \) c' F - try {+ h* N) M* c: r( i
- past=con.prepareStatement(sql);# [0 X5 q! @: A4 v7 o
- rs=past.executeQuery();. |( w* T# [) N
- while(rs.next()){6 J( i' d0 C9 ?. u# I( c1 P& c
- Book books =new Book();
1 a: A4 F+ Q4 B: v+ _+ R, Z* n - books.setBook_name(rs.getString("book_name"));8 I5 [( H& a' s8 h: u0 \
- books.setBook_type(rs.getString("book_type"));/ _$ C9 ~" _) D9 l \6 E; U
- books.setBook_publish(rs.getString("book_publish"));
5 k5 ?% ]5 h3 f) @, u - books.setBook_date(rs.getString("book_date"));
7 L4 U6 s' ~3 P) z - list.add(books);* s( Y& ^1 k b
- }
2 E5 [4 R; N b9 r8 z6 Z2 k - } catch (SQLException e) {
$ e6 ]- Z/ i- D, N6 `9 j4 }/ R - // TODO Auto-generated catch block: q0 L/ Y% Q3 v& P- }
- e.printStackTrace();
; @* C t E8 D2 t+ ^' H - }0 n5 L( e3 A: V6 h- m
- return list;
X( S* X3 m8 h6 `1 g, z -
# S+ b9 b: w) O) K0 V - }
/ z) e) `+ T0 j) e: y+ s9 A" O - public int bookInsert(Book book){
4 ]% y# ~7 s& m+ Q- {3 X# B - String sql="insert into book values(?,?,?,?)";: h' I* H2 ?; \1 u6 } }
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
4 d! S0 x4 o' O0 s- D - return update(sql,getVlaues); 2 i' u; \5 {( J7 K
- }; m6 Q% {* W8 w! `
- public List listtAll(){5 E {! v9 I6 d9 O1 P& z5 W2 l+ J. w
- List list=new ArrayList();( S6 r1 D$ j. B3 ~
- String sql="select * from book";
9 R" N3 S7 k, q: ?' T - Connection con=getConnection();
' G$ J" i# x! s8 ~9 x" v! | - PreparedStatement past=null;
& N0 K3 w. u6 O) b1 \ - ResultSet rs=null;
' y1 d3 b6 W7 }8 c) D - try {) J# m& D7 S- ]
- past=con.prepareStatement(sql);' k6 s: K+ n( M) q# Y, ~0 |# E. n
- rs=past.executeQuery();
+ _* p# A' G8 l3 A* w3 n - while(rs.next()){
6 I+ q, B k9 A) U - Book books =new Book();
6 }4 _5 c; j2 l% P/ J - books.setBook_name(rs.getString("book_name"));
6 j1 X9 T, O( j# h+ ~1 s3 j - books.setBook_type(rs.getString("book_type"));% U$ n/ E$ v. a& e: i0 }2 u
- books.setBook_publish(rs.getString("book_publish"));7 d5 s8 R( V* S) u7 Z1 R* \& ?2 |
- books.setBook_date(rs.getString("book_date"));
! Z# C4 }) q$ C& \3 u6 o - list.add(books);
) C* B) `4 s* k' ^: L; \2 x" a - }( Q8 z5 e! i# g% Z- p
- } catch (SQLException e) {
% ^9 A( J3 v# s - // TODO Auto-generated catch block
# v- ~2 y5 g( y# w. r5 }* d - e.printStackTrace();$ f9 U$ a. f# ]& y* v9 {% ^
- }
- e5 h, o' m5 w( }* V w, a - return list;
: z! _) P) Q4 X4 z" Y- j - " z- n# L2 a: M, v8 H+ E. J) d
- }! d& T- B2 n: }- j- Q# \( I
- }% \/ c8 e2 O5 ~$ Z$ B H5 p7 K
复制代码 源码下载地址:点击下载
6 s$ ?4 b" D0 r# I1 r, W" r8 `% U9 b, k p
. q% A% O1 n$ b1 W @4 z |
|