该用户从未签到
|
- package s2.jsp.zhangxiao.dao;* {# X( B0 |) ~/ r
- import java.sql.PreparedStatement;
9 [% o* X, d- T6 U - import java.sql.ResultSet;$ ^0 K C2 |8 A# j" o
- import java.sql.Connection;
' X c1 x2 c4 \ - import java.sql.DriverManager; x! N# z+ W- I" ]
- import java.sql.SQLException;: |1 p8 q2 \- v( u# K4 F( @
- import java.sql.SQLException;
8 g& X' F3 u0 y% V - % y6 O8 i( s0 l$ C& Z+ e
- 7 R) n K' Q% @; l3 G
- $ L9 G$ h: G& z
- /**1 b3 {, V5 j8 S2 Y7 z' t4 e3 \, O6 g
- * 连接数据库" N+ ]% s4 Z2 c1 h: L/ Q! w8 D
- * @author student19 R1 j2 _, ]; w. c- v1 ~
- *
) b3 j. M! F. D+ k3 p: v/ V- ? - */1 @2 Z) I- |$ A0 o% ?* i
- public class BookDao {0 K9 p/ H7 i0 I9 v6 ?
- 8 N$ q$ g- a9 ?3 T; k
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
9 q. _4 X. s% J2 H7 P - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";
$ W; i- f5 o$ f8 J - private static final String DBNAME="sa"; q+ o+ G& w2 N+ H3 W' Y
- private static final String DBPASS="1234";
& Z- d$ W2 R, h% s8 j -
, o# B8 Q8 G0 H/ U. X/ s - 7 t7 }3 f% h+ _' i- X! k
- public Connection getConnection(){
; B- D3 K: {8 n' V- c - Connection con=null;: T7 C- U" x! h+ E0 |$ E4 w! z
- try {
: v7 D1 {- x, C7 T6 d% C# S - Class.forName(DRIVER);
5 C' W2 f2 z; |$ ~* C) D& g5 q7 } - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
& i2 I+ I6 F' H! [/ ^4 n - } catch (ClassNotFoundException e) {
5 ]! u6 |% G0 c s8 f3 u3 w - // TODO Auto-generated catch block
+ p; _4 ]) A' @" U) ] - e.printStackTrace();
# R3 a# e% a7 P: S - } catch (SQLException e) {
0 e9 ]* c m3 I# a ^ - // TODO Auto-generated catch block+ r1 S( W5 F0 ]
- e.printStackTrace();- P5 B; h$ R* i
- }
( j! H& `# b% J
2 g9 c6 d: q% i8 f; h; h5 n- return con;' d E p7 @: ?" d, B) E
-
% d7 ~- \, S; G/ H. o - }0 z+ _+ o% X7 V4 G' Y
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
. f4 x+ c+ ~* @$ e& f. y) L% w - try {
( y; N$ {# b. G; B4 r - rs.close();
9 m: K! @8 C9 C7 B8 O% N - } catch (SQLException e) {
1 x0 _- c5 n$ I0 V6 ` - // TODO Auto-generated catch block2 z7 p# i& x9 h5 v
- e.printStackTrace();
! k8 U% h1 Q" K4 N% s( Y8 s - }3 C# h5 Y: h5 F2 t) p5 v3 H
- try {
! t: Q$ K! I$ i9 Z" t2 G - past.cancel();( \' G: `4 z: l
- } catch (SQLException e) {
3 M3 ^& Q$ N3 n% j: I5 o, F - // TODO Auto-generated catch block2 L: C! J/ Y7 m8 _: y+ l! R( s
- e.printStackTrace();7 @! d7 K5 U) e
- }
' {+ ~( T0 l5 i4 u( P - try {
n' U6 @9 E- Z2 K$ T* C - con.close();7 _4 b; T; \& g$ T T
- } catch (SQLException e) {, a; s* r7 {5 B$ x5 l
- // TODO Auto-generated catch block
! M* F! N( Y. u( E" K - e.printStackTrace();
# Y4 W: j# b" @0 l. G - }/ V( s) E: ~: a2 W& L
- }; R# c1 o+ D5 v: z g |. D
-
- o# D! u/ h* y - public int update(String sql,String[]getValues){3 V' W1 F! ]! _9 q+ ~
- int i=0;
" ^7 l. m7 h/ { - Connection con=null;
3 n& m( }9 U2 n3 J2 B6 y! @ - PreparedStatement past =null;
( s7 v+ T& H3 P# y/ V - con=getConnection();2 e& o$ Z i9 b; H& g
- try {
2 |+ v( X4 V7 @9 E - past=con.prepareStatement(sql);+ |* r: J4 B# d6 \
- if(getValues!=null){& B; x: F$ Y3 G! I# N
- for (int j = 0; j < getValues.length; j++) {
c. g! T6 i4 ~" q- q. e - past.setString(j+1, getValues[j]);
5 f/ M5 Y3 Q2 V; l/ V* ?& O - }& v* Z* t$ z/ T
- }0 L/ v0 y; ` a1 Q6 f) }
- i=past.executeUpdate();
3 K2 g/ i# k F6 r+ p- `9 Z - } catch (SQLException e) {
( q: x" U4 u0 I1 u - // TODO Auto-generated catch block
9 S- z d8 |- K. j1 R - e.printStackTrace();
: U/ ^5 F6 m5 t" H6 W6 c - }/ g" B1 a7 N3 I
-
! |7 q6 N; O' m' p+ |8 G0 n - return i;- d% f1 }: [3 c) g" e/ H( h
-
! Y# ?! I, I1 N' C - }
: A M: p$ D/ e: e O - }
8 R6 n7 z. V. n/ Y: s% V; ~
复制代码- package s2.jsp.zhangxiao.dao;
E0 H( u/ f8 ~ a5 w6 Q) `
+ k: C1 Y/ {% I! H7 ~ }* l5 Z4 _ R- import java.sql.Connection;
1 W5 l7 |, g8 e! Q' f7 [ - import java.util.ArrayList;* d! {. h2 s, L L. i6 j
- import java.util.List;
1 [) G" @ Q8 R! P5 w- r! w6 N - import java.sql.PreparedStatement;* Q/ v- K6 u$ b5 v8 B
- import java.sql.ResultSet;- u, E5 e7 O1 N" Q" u7 g2 O
- import java.sql.SQLException;
5 j. _: o* ]% v& ? - / l$ u* R0 Y8 ]9 M5 v% l* V) c3 o
- import s2.jsp.zhangxiao.entity.Book;
: q1 W( J& h5 i4 h0 u/ u; @$ j# s ~ - 4 {- z2 }# [2 c* ~$ ?) d3 M
- public class BookImpl extends BookDao {
$ }: O4 @7 W$ e1 v2 v5 U q+ t - 2 A1 D) t. K2 v$ T/ p. o9 q5 \
-
! w T: q/ K- W8 Q$ _1 c" h - public List bookAll(Book book){
. v! O- o7 Q: d - List list=new ArrayList();* L% M4 y: G3 x/ k$ ^! J
- String sql="select * from book where 1=1";
0 S1 G! I* V. c - Connection con=getConnection();
6 W6 R) F8 K* O) S) W - PreparedStatement past=null;/ }5 p9 \% O* P" H
- ResultSet rs=null;
4 H5 |. n6 ?2 j3 a5 W/ u: A - if(book.getBook_name()!=""){* }2 B# ]' i" X& p: X
- sql=sql+"and book_name='"+book.getBook_name()+"'";' _" D# n+ |% Q
- }
* J0 K- a1 |- T L - if(book.getBook_type()!=""){
1 {9 B8 T- w6 A: U4 [1 c" Y - sql=sql+"and book_type='"+book.getBook_type()+"'"; H9 `* Q. s, k* N. n
- }1 j! y% t T- _* o/ w
- if(book.getBook_publish()!=""){4 C! ~& b5 k$ K2 J2 E/ {
- sql=sql+"and book_publish='"+book.getBook_publish()+"'"; P9 L" z7 J& J3 ? h
- }7 V t; e, J, x. [3 h
-
8 t; d! M6 c- {# g - try {, \! ?. X; M- Y* \; w6 ~$ r
- past=con.prepareStatement(sql);
5 G/ [7 _4 `/ g: z - rs=past.executeQuery();6 P% S" t/ t A* m+ R
- while(rs.next()){
, Z. U$ D5 M4 |. j/ W - Book books =new Book();
1 \- ~& w0 J( Z - books.setBook_name(rs.getString("book_name"));5 J5 d& c/ k" {' G
- books.setBook_type(rs.getString("book_type"));1 h. h! P# ~8 P
- books.setBook_publish(rs.getString("book_publish"));
. \. j1 B1 s' H) n! I. l - books.setBook_date(rs.getString("book_date"));, T5 p$ e4 I1 M* W$ W1 K
- list.add(books);1 M+ J [; K& p- ]/ i- c' [. T% Q
- }
) p4 p4 x+ H, ^: h' F( t - } catch (SQLException e) {
, z3 X$ I, h+ R" o8 y - // TODO Auto-generated catch block
0 d; h" ]& Y! X+ L+ u( i& z+ {- a8 ? - e.printStackTrace();, D O5 |5 t! F5 I
- }- ~- n% a2 A, F5 S
- return list;
: A9 G, E" @/ ]0 F - * O4 A# [4 _& F, t6 C1 g
- }$ f! A K* H' ? _8 b; |! E4 g8 \
- public int bookInsert(Book book){
$ \ w9 @( x3 |* N+ A! o. F - String sql="insert into book values(?,?,?,?)";
, X' v+ `5 S' j) J9 y - String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
4 _! p$ z. K' s' D - return update(sql,getVlaues); + _' u) s: \. @8 _' c$ B
- }
; G5 p8 E: J0 N* H9 \. E( c( c - public List listtAll(){. z R- {5 O) G' h/ p& b# n
- List list=new ArrayList();
- |2 j' u3 O' m) O5 _ - String sql="select * from book";) r- D5 h J" ?3 Z
- Connection con=getConnection();% E8 E; |- _- _ M' X5 i& K
- PreparedStatement past=null;, ^' Q8 w! y x
- ResultSet rs=null;$ d* Y! Z- A) R
- try {
& i: A6 o9 o+ Z3 A; ` - past=con.prepareStatement(sql);
( C: Z' z: \5 P" ^ - rs=past.executeQuery();0 W! h4 u' ?/ T- @- l
- while(rs.next()){
1 z. Z: m- a, d& w - Book books =new Book();
7 s c" m6 K% r: k" D, ?* D - books.setBook_name(rs.getString("book_name"));
& U$ K6 Q/ e4 D. m - books.setBook_type(rs.getString("book_type")); p T" {1 v7 R8 E
- books.setBook_publish(rs.getString("book_publish"));2 x7 e) ?! `" `; E _' b9 F
- books.setBook_date(rs.getString("book_date"));+ O+ o. s1 n( ~$ M1 F- Y; G% F9 Y: ]
- list.add(books);
3 M* l5 G' \9 `( X4 J - }" H6 z$ t* S5 R7 |
- } catch (SQLException e) {
3 \3 _; {8 O- V6 y - // TODO Auto-generated catch block( k: e9 o# s# P0 D: M
- e.printStackTrace();( c' V) Q; f) c# _- g
- }! o/ ~3 n# k# I3 h
- return list;
% q! w* F% [* m7 q( S - q: _7 }% [' y' X* B0 P
- }
+ q' f) w! d( a4 S! E. r - }
% ]- Y' F5 y) [2 i; [9 T
复制代码 源码下载地址:点击下载. @# K. J+ i5 R, V1 w4 P
0 r0 f F# H/ w; s T
4 t( K2 c$ {4 w& I2 L
|
|