该用户从未签到
|
- package s2.jsp.zhangxiao.dao;3 d$ J0 y& Z% N F* B% T$ w
- import java.sql.PreparedStatement;
6 T8 @" u8 U; u/ T9 W2 z% c& } - import java.sql.ResultSet;
, I; k! A( U1 B9 S - import java.sql.Connection;
1 h/ P% x, u% l6 c3 e - import java.sql.DriverManager;
5 K8 @0 r: u( ?" j) X4 d - import java.sql.SQLException;, X9 `+ `! M) s. |6 ] U) X
- import java.sql.SQLException;* C9 R/ ~4 o. h! M# r
- " t$ d/ X6 N) y" A2 H' \8 z* q
- 5 n6 f( |6 z# j; V$ P+ q
& T% y% P8 s6 `8 | n# T) ?8 C- /**1 w: s2 h/ J* ~0 b
- * 连接数据库
7 C! | v* W& `! O! e4 L- q( ~ - * @author student1
5 A* q, Q+ r2 ? - *
" a/ j" L& \/ B0 F F - */" r+ P( S5 q# _5 c O' n0 u8 z
- public class BookDao {
# H9 ?) W( @, {, `3 d1 B! `) ^
' M: W6 ]1 Z: G, P- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";. m0 N) h3 b( l
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";
" u, c! h4 q2 U0 H6 X - private static final String DBNAME="sa";8 w" u$ q5 h8 J% v" x
- private static final String DBPASS="1234";6 z. z* p5 G& t! g: e
- ! ]3 ]$ N8 t4 Z1 s% f
- ) w" M; \7 ?7 C4 V
- public Connection getConnection(){
2 b f/ ]2 l; N! ] - Connection con=null;. n* ^% Q ?% M" ^; W: l \
- try {- V; N! S* V3 y- p* ?
- Class.forName(DRIVER);
( G, \* m* P# E4 N - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
7 X* D4 A% R3 v; l; s" z - } catch (ClassNotFoundException e) {
% r& [" P8 i6 X9 f3 s2 a5 s0 P - // TODO Auto-generated catch block+ g, C& Q" K; A
- e.printStackTrace();, \; R0 d3 z4 ^+ @: \+ I
- } catch (SQLException e) {
9 w }8 n2 q4 K5 v5 | - // TODO Auto-generated catch block
4 H9 B/ [& V9 z6 U4 x - e.printStackTrace();
/ i4 ^6 g! b, ]5 h - }0 y" V) Z/ z3 z2 H* `: [; @
: c' @6 ]' \+ J$ R- return con;
, e8 M2 w8 C5 j% R -
5 g! N( S# x) ~ - }
& P! \ G0 ~ X3 x/ F$ t: h6 P - public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){, c& }8 x e, X5 t6 B6 j. |
- try {4 i, S6 O* s6 Q/ L1 l( U
- rs.close();5 W8 h7 B0 U- U3 \1 N2 m
- } catch (SQLException e) {
2 y$ L% H9 q* p( r8 F' ` - // TODO Auto-generated catch block
9 M) U% W# O. c* S8 r# c: } - e.printStackTrace();
; F. K( d T) j7 C/ l - }
. p3 E) r3 J+ x( m X7 R5 ` @ - try {
$ x2 r0 z0 W4 e/ ]+ e - past.cancel();
" _+ ? v/ b, s8 }' t6 y3 a - } catch (SQLException e) {4 F6 p! `3 @1 d/ v# I6 Q
- // TODO Auto-generated catch block6 ~9 B; Q- s( ?' X" C ^# g
- e.printStackTrace();
! K9 @1 V2 |& q5 q, W - }
- X2 L. B1 p6 d/ Y1 c - try {
% w/ A& [) c" q' ]9 {% F - con.close();
$ H! q) s: k! ` - } catch (SQLException e) { C) l3 H" K2 d* n" f% D
- // TODO Auto-generated catch block
8 h: L1 P% L7 R/ e6 G. U$ H5 J - e.printStackTrace();$ x4 S5 y, b! }2 B2 N* ^- C5 _
- }: N6 O3 \- f( O' k- @* i$ p
- }- X9 m/ E# N, j; V
- ) U3 c0 U3 U9 W- `- d% m* h$ m* ?
- public int update(String sql,String[]getValues){
* l9 S# `; ~' L6 I8 k8 z8 x - int i=0;
3 Y+ Y. p' H3 u6 X3 v - Connection con=null;
" J4 X8 Q' s$ z! a4 R- m - PreparedStatement past =null;
* {" V) j4 m/ f2 B3 Q- G1 P - con=getConnection();
/ s0 e, T- i8 X2 n6 w- b# Q6 t - try {# }- U- q c% s# C( @
- past=con.prepareStatement(sql);$ [; G# s/ W+ K: P$ n: W
- if(getValues!=null){
/ o7 C0 z l3 v5 P" s - for (int j = 0; j < getValues.length; j++) {
) M0 H7 X7 [; r) w! s - past.setString(j+1, getValues[j]);# n* S Q& I3 F) Q, k0 U! n) C1 O8 o
- }$ `6 ~3 q' O! c0 W
- }
8 I1 P6 r3 a/ f! m' j- i - i=past.executeUpdate();; H8 ^! l+ _, Y6 E, n, E6 K
- } catch (SQLException e) {% G6 t( X& n; a# r2 {: k
- // TODO Auto-generated catch block% t& w2 a# o9 V7 ?5 u) m
- e.printStackTrace();7 C4 a' ]1 A, o& r4 x- O/ b
- }
+ V- d/ a R& u - ) @' v! m- W" w
- return i;! ^) V0 M2 P/ O" G
-
# A7 J+ B$ n. {) m7 M4 e* M2 J+ N& X - }
9 R+ F4 K7 W* Y$ l* f1 R - }5 o4 j0 v+ [$ q. I% ]
复制代码- package s2.jsp.zhangxiao.dao;- o+ D4 N" f% C- C* S
- ) @5 U6 [- b- j1 h, P" F# _
- import java.sql.Connection;/ ~; N* L% F! ^& J
- import java.util.ArrayList;$ I' W+ n+ ^8 D. r1 g
- import java.util.List;8 {) i6 K" n2 O5 k+ P, d
- import java.sql.PreparedStatement;
2 ^. H7 C* Y2 f) B; _+ o - import java.sql.ResultSet;) P" y5 g3 ^! M7 d4 \% \
- import java.sql.SQLException;
$ s& F9 ^6 d! x( T& B' \# I
. g. p0 s4 [6 A( _# o1 ^- import s2.jsp.zhangxiao.entity.Book;2 X5 ]4 {( f6 n/ k
/ y5 w$ {; n: |7 C5 F1 }- public class BookImpl extends BookDao {
2 `, E* _0 {2 Y# W. L) H - 6 ~ T) n! X/ r4 f+ w. i. l
- % I* ~, W. A' E% k% U; g( |
- public List bookAll(Book book){( J3 R, s3 d! P* b
- List list=new ArrayList();
: Y' |( l( c Q" D0 p$ m - String sql="select * from book where 1=1"; D. U1 ~ h) p P& I4 p' W1 D/ @
- Connection con=getConnection();
. ~( }) P6 j5 K! g4 [ - PreparedStatement past=null;
! ]! M8 h4 X! ?: @: z! P) \8 T% l& e - ResultSet rs=null;& t* s+ J' _0 S3 Z* }. h8 Z
- if(book.getBook_name()!=""){+ j' i* H, L" L: k% r
- sql=sql+"and book_name='"+book.getBook_name()+"'";
' c0 f" ~" z6 `4 |7 D& M% X - }3 g8 |0 e8 t. ~% i: x
- if(book.getBook_type()!=""){- Y/ c% t" g. t& Z* B( v1 s
- sql=sql+"and book_type='"+book.getBook_type()+"'";+ P3 j* O) T" `' q* B+ g% t
- } c6 A; W3 X! K* `6 T7 d' @9 s
- if(book.getBook_publish()!=""){
) C3 A5 y1 G8 B! _/ p - sql=sql+"and book_publish='"+book.getBook_publish()+"'";" y s* l' M Q0 M9 H" o
- }
# n+ B3 z/ X" F- u7 O -
2 m6 B% {* |; h' v - try {
7 q: T* o* `* z+ q- e - past=con.prepareStatement(sql);
3 d0 C, Y7 [; r: R$ l - rs=past.executeQuery();+ l9 u$ _# [: B# Q9 u/ d
- while(rs.next()){
1 s* G( D J* K8 l' f$ j - Book books =new Book();
( q0 ]9 C/ J, }# q& b5 i - books.setBook_name(rs.getString("book_name"));. [& c5 \8 c6 ]' L' _
- books.setBook_type(rs.getString("book_type"));5 o! j4 v. o6 P/ h8 ?& L
- books.setBook_publish(rs.getString("book_publish"));
) u6 W; M+ m( ^6 N, U - books.setBook_date(rs.getString("book_date"));, T6 S2 G! J- z. N
- list.add(books);
8 P0 K% u, K0 K4 ~6 } - }
4 W) ^( \4 B" x& v# @2 H1 A - } catch (SQLException e) {7 F9 O* |, y/ b% ?5 F# _& V3 T
- // TODO Auto-generated catch block' K& b2 B6 }! O$ L- c
- e.printStackTrace();
8 f* N0 Q8 Y3 ~5 E0 C - }( G) K' R9 d% i1 C5 `/ L! p. t6 Y
- return list;
/ `: Z) k/ T! U& n1 S - 1 j2 k6 v/ M/ W4 v5 |
- }+ D5 Y& l: Z2 y: V/ B7 j# c
- public int bookInsert(Book book){5 }1 S6 x5 S/ P, ]6 Y
- String sql="insert into book values(?,?,?,?)";
* }3 p' ^) ~) t% u' N z# D - String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
& F' [! @4 [5 F" b9 z$ i6 g2 L( F - return update(sql,getVlaues); + r4 H' A) X/ z' w4 K4 z( I
- }# s4 W+ Z# a# H7 w& L
- public List listtAll(){* B& V5 L6 [8 G: y8 C& F( I
- List list=new ArrayList();
{5 \& ^3 w* L3 I W - String sql="select * from book";- A8 R5 Z. {: |7 j9 j: U: i
- Connection con=getConnection();
- p! ^0 i6 M9 d4 {0 m% O - PreparedStatement past=null;
" l ~$ \; a5 T9 b9 v" e - ResultSet rs=null;; s ?8 \" I/ V' h% J: B2 Q* P' C
- try {/ E/ f/ Z' C/ |: Q* H
- past=con.prepareStatement(sql);
6 g% I b# B" m) c - rs=past.executeQuery();- v7 J, e( `' |' w
- while(rs.next()){
, W& r& w* m& q S- q8 |8 S# B - Book books =new Book();! h6 c8 t" c1 i+ {" b8 K# M. y4 z
- books.setBook_name(rs.getString("book_name"));
. M4 Y: c6 @1 j D/ ~2 Z5 z3 w - books.setBook_type(rs.getString("book_type"));
. a$ t2 \' \" Q* ]+ ]6 F, P0 F9 m. w - books.setBook_publish(rs.getString("book_publish"));2 O2 O( Z, z; d# T" z K
- books.setBook_date(rs.getString("book_date"));
8 n, f" S. d$ X0 C - list.add(books);
/ q1 t( Q) ~* s M9 e - }/ c5 S# d4 n; G, A
- } catch (SQLException e) {& n0 m: \ m& P: I5 P
- // TODO Auto-generated catch block, H& Y% D" K9 c
- e.printStackTrace();
, l, J( k) j9 V9 B; r/ \ - }
" o6 }, Q9 J% X0 [+ @% j - return list;
( C: Z: A2 r1 L0 }, G -
& \2 g2 g/ B$ N+ f* \ - }
# ^- A0 g3 h+ ^( e A - }6 O2 b# `2 T: c+ H3 u, A
复制代码 源码下载地址:点击下载" w, i$ F) y2 g6 X
( P4 C2 ~' d. j# [
, J; P+ k3 J! M2 Q! L; z5 K |
|