该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
% B# l( m9 ]% V. w7 d( i" `" s - import java.sql.PreparedStatement;- o; L6 t8 T$ T$ h* }7 Y8 S
- import java.sql.ResultSet;3 G9 V" y( n% o0 j
- import java.sql.Connection;
/ v* T- o7 j/ v9 l - import java.sql.DriverManager;( g9 C+ m& {# i- E$ W8 Z
- import java.sql.SQLException;8 T5 G% r+ A9 m; B
- import java.sql.SQLException;/ u0 l; I1 N( D" \; f% ^5 q
- ; k5 o+ y' H* q( G
' e& X, C7 k9 [7 @7 M- 5 [0 q1 @+ F% V, A+ T4 L
- /**
3 t s: Q5 s+ J# h/ r5 Q - * 连接数据库
; i) f$ W4 R5 P' l- i - * @author student16 F" j! p: ~: O, [ F' [0 S5 l. I
- *# g+ T9 r! l) I( J2 M3 s
- */8 e! |5 i! E, [' E/ c, h
- public class BookDao {
3 u) v6 n0 s3 R% q4 I* P; c
, z3 u! d$ M% y. a1 j( c# r7 _- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
! g" @/ b9 K# l5 l$ l( ?: F4 m* @% @ - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";
! U! f9 @4 W" z! L - private static final String DBNAME="sa";! {% l/ p! t$ O$ j, T( t' N
- private static final String DBPASS="1234";
' Q: @. F, \0 ^# k2 K$ S - : H' p! u3 i5 X1 K
-
3 @' @3 n/ A0 P! H - public Connection getConnection(){; u6 U+ f# X: L
- Connection con=null;
8 F8 \- D$ ` Y - try {, n! Y" |6 o4 a% I
- Class.forName(DRIVER);
1 i1 j1 F* T7 q; [5 O - con=DriverManager.getConnection(URL, DBNAME, DBPASS);9 e! q8 K5 R! h
- } catch (ClassNotFoundException e) {, O7 F+ x4 z. H+ ^9 m
- // TODO Auto-generated catch block7 b& t ~ v+ Y4 A3 g7 j7 y% Y4 k) y
- e.printStackTrace();) f, P# g+ Q( U$ \. H5 G( Y
- } catch (SQLException e) {* u9 x6 y* S5 J! q
- // TODO Auto-generated catch block
% \7 e! e l, ?4 @ - e.printStackTrace();
3 c" w" ~2 C( o" u - }$ Y! `4 x2 h/ i, U7 j3 ^1 W
, z- o& s% B4 Z6 x" k2 g" m- return con;" f0 D7 c. h4 Y% @$ o
- . ]6 I0 Y4 S8 l" |- H- g
- }
) k: N3 d) ]' |2 K! U - public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
. O8 V8 S8 Y( s f0 w; v) `& D4 V9 Y - try {7 d( w2 E' f' _: u* J
- rs.close();4 I. O# f* q1 F
- } catch (SQLException e) {. M- S$ y) ?: @9 v
- // TODO Auto-generated catch block" W" v& n7 Z: C7 [1 [8 l% F
- e.printStackTrace();
/ o+ y( z) n% {4 I# n; _ - }
, ^: d/ F9 u. ~6 o+ E) u, O0 P4 X - try {; `5 [8 p* N5 f0 F
- past.cancel();
: d# [& E. q( c, a! M' A0 k - } catch (SQLException e) {2 R o, s0 Z4 r r
- // TODO Auto-generated catch block
# X) O8 I9 f* L - e.printStackTrace();
) N2 N; ?2 J8 b5 V( f: p! J0 ^0 J9 J - }: C9 k* q4 Q# J5 U& N8 T, m
- try {( C- H" y+ z: g8 F
- con.close();7 w! d! C' g) \6 ]* w9 H
- } catch (SQLException e) {( y1 n; ]) W) r# ]* Y
- // TODO Auto-generated catch block& i* A; n$ a3 y% D
- e.printStackTrace();. T/ R1 B0 q( E- U6 L6 d
- }
, E" W$ j0 J& X- V0 V9 H - }2 V4 U2 [0 T5 a- R j
- 1 j* q% `$ c( A2 G. A5 ]4 Q
- public int update(String sql,String[]getValues){
( J. V/ { o9 u: s) Q# ~' | - int i=0;2 R2 p& O: D n! s' |$ E- |
- Connection con=null;, E) G1 B5 A0 ]
- PreparedStatement past =null;
& b# j0 `8 R* p- Z - con=getConnection();' |; o7 E& O& j; {. o9 b
- try {9 ~' E G, ~" Z* D c) [
- past=con.prepareStatement(sql);( b& L5 w0 ?, t- o+ P4 P
- if(getValues!=null){/ |' p) R K) T j M. N, a9 r, n- Z; e
- for (int j = 0; j < getValues.length; j++) {
m' T+ I5 F4 R G. d% Z9 v - past.setString(j+1, getValues[j]);, ]2 O3 L. d$ ~
- }" o! U3 Q+ o, j6 b/ a' }1 h+ Q: D
- }$ v" M9 @6 T0 \1 y* L3 t! }9 z
- i=past.executeUpdate();
\ t( i0 _% n$ ?! g$ ]( R - } catch (SQLException e) {
4 u- s, Q5 ]" u - // TODO Auto-generated catch block
0 U* p% h/ r% B2 U - e.printStackTrace();
; j# y6 ^% Y. g - }( P$ v3 E1 E2 Y% a# R4 p7 Z
- % \" Y0 A- @' n. p( m! X8 }8 z
- return i;, m, s, I+ I$ o
- 4 F8 n4 ^3 }3 d/ {
- }4 y, X2 R) f0 s7 C+ A1 i
- }
3 q2 q& ^( m x8 R
复制代码- package s2.jsp.zhangxiao.dao;4 N2 b8 A, z6 W% k* i4 z6 @" d* a5 J
' E3 a, o$ Z* i8 t& ]4 W- import java.sql.Connection;5 f" |1 u t4 D7 s- \
- import java.util.ArrayList;
4 [1 @2 m+ b- v5 C - import java.util.List;% y1 b& Q- y, `" O8 F! ^1 N
- import java.sql.PreparedStatement;
C9 H6 X6 o2 W0 `1 v0 Y - import java.sql.ResultSet;1 G/ w1 m0 P7 f" M' n" p: n) ~. h
- import java.sql.SQLException;, q6 E0 d( t& C4 Z
5 R Y$ Q& U6 F# q& D- b" a- import s2.jsp.zhangxiao.entity.Book;' G) W- x: R: |
- 0 m( U' a, q/ h" E( I* r
- public class BookImpl extends BookDao {0 Y6 a5 N' Z- R* e2 H* T/ z
: Q" e$ w7 |" s' ~-
; i8 |) S! O1 R0 q9 C - public List bookAll(Book book){6 t2 l2 H3 `; r2 _7 w+ o: P7 J \
- List list=new ArrayList();
5 [3 O0 p, p/ t9 ]6 U - String sql="select * from book where 1=1";1 V5 B( _! C9 E: y1 |" M( K
- Connection con=getConnection();
0 f* H% L" Q/ h% ?: }2 `* R - PreparedStatement past=null;
+ `% q/ R/ E; b0 Z" N, t - ResultSet rs=null;
6 W9 }: G- `( q* o9 y - if(book.getBook_name()!=""){7 ~% h: m2 ]$ p2 W& I8 b; R
- sql=sql+"and book_name='"+book.getBook_name()+"'";, f! O8 q3 r- _2 p/ G. o# @+ Y
- }) Q2 r+ v' {9 ~1 z# K9 A" U* ^
- if(book.getBook_type()!=""){' t/ ]. |8 w( N; s, [
- sql=sql+"and book_type='"+book.getBook_type()+"'";; q7 i0 d7 E) U0 N% w
- } g/ `) e; Q$ u* |
- if(book.getBook_publish()!=""){6 s. p% E+ s7 P0 s0 t& h! P
- sql=sql+"and book_publish='"+book.getBook_publish()+"'";" Z& U! L' t- J8 i' N5 m* A0 ]
- }# H6 v7 ?, M) D, ?$ W% f
-
, I6 Q- U# J' ?: d - try {
8 U3 |* ^% ~" R7 W$ ~ - past=con.prepareStatement(sql);
# ?6 } m, B' J6 I! s& N1 B5 Y - rs=past.executeQuery();& y/ R7 o& B* o$ T- {6 A
- while(rs.next()){! t' L. I7 u e$ {
- Book books =new Book();- v- r0 r$ @: x; C
- books.setBook_name(rs.getString("book_name"));
. R6 e* u7 v0 H) k8 f - books.setBook_type(rs.getString("book_type"));
6 A& R* m6 S: t# z. R - books.setBook_publish(rs.getString("book_publish"));
' O0 U- d: l1 M& d# C9 ^* w- } - books.setBook_date(rs.getString("book_date"));/ t. Y6 E8 h" y5 z; @: m
- list.add(books);
0 h% v1 G+ p. Y# c+ O - }* g! j# q( h4 j. @+ j; y, o! s
- } catch (SQLException e) {
! L, A8 L) I' p$ o - // TODO Auto-generated catch block
% x) H+ \3 Z0 h$ c - e.printStackTrace();0 o8 P. w, }/ f, r( q
- }' O9 m0 h4 ^+ f1 ^- O# P/ b
- return list;& b5 o% B5 t8 N; V
-
0 J- A+ c2 t! r4 k) F - }
) k& \; I& G, D: Q8 F6 y+ H1 Q+ Y8 @ - public int bookInsert(Book book){
+ K( l! d4 A. c( |1 | - String sql="insert into book values(?,?,?,?)";1 g1 e" M$ j6 Z+ h- U
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};# P1 ?3 a) w6 i# G; H9 Q
- return update(sql,getVlaues);
# y5 L/ X# k' a' P# U4 O - }+ B, S- @$ G' q4 S( E1 D
- public List listtAll(){
: ~) N' K6 G! C+ p' |" { - List list=new ArrayList();
, h" w( [# [/ t9 K% u K - String sql="select * from book";
8 ?; x) P0 ]2 h: |9 i( m S - Connection con=getConnection();
' P( a, U" M8 D% O" R2 M6 z - PreparedStatement past=null;
* k1 G Y0 e1 s - ResultSet rs=null;6 P7 f& h7 g* o$ v
- try {1 a8 Q; p$ V8 H0 T. s
- past=con.prepareStatement(sql);
) R3 C2 T) M) t% [ - rs=past.executeQuery();
- S0 I; X/ f( E! ] - while(rs.next()){1 b: w% I+ P/ M1 Y9 W
- Book books =new Book();+ b" f9 t1 y8 t; C; }
- books.setBook_name(rs.getString("book_name"));
- r% [0 |2 d0 }. G7 S. w/ `7 w - books.setBook_type(rs.getString("book_type"));
; I, f2 A7 Z% P- N9 B& } - books.setBook_publish(rs.getString("book_publish"));# H1 Q+ A( U4 V" ~8 q) }
- books.setBook_date(rs.getString("book_date"));
+ d- I9 s( o6 ]# a4 b) O - list.add(books);
. k; I; o: Z. p. A* W - }/ P+ ~4 A: _) P9 f5 Z
- } catch (SQLException e) {
; n+ ~; f; M. H/ Q0 E7 Q - // TODO Auto-generated catch block
: o) I! [* S& { - e.printStackTrace();# B3 h L/ \& ?/ R" v
- }1 b; P+ e0 o8 i
- return list;, l2 T: {4 P& p0 }' Q
- 4 H ^3 \2 h' `7 z! d
- }5 H Q+ g7 O1 g
- }! ?, B K9 k Z, j8 P5 ~
复制代码 源码下载地址:点击下载
3 Y% @1 M7 G S" k
& G F- U; u9 B# P- ^" N4 [5 k6 w0 W9 R7 G& T. H
|
|