该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
$ c! E7 g/ `# j/ w - import java.sql.PreparedStatement;8 N) C& W* O$ a( z
- import java.sql.ResultSet;
8 }9 I( i# R; E- V- T - import java.sql.Connection;- `6 i" Q3 c$ V
- import java.sql.DriverManager;
$ ~4 U# B0 B t: m6 K! }3 j- r- [ - import java.sql.SQLException;4 m5 h* M; l6 k. n
- import java.sql.SQLException;
1 ?+ ^6 `* l' m/ | - - z( G/ m1 [0 {* d, ~8 c# \5 G
- ; w9 M& s! _2 w. c2 Z4 \( R. ^3 E
+ V% j5 l# |$ g7 |5 U- /**/ J% d+ a4 M5 o+ f6 M* J% @
- * 连接数据库1 B& v% z' d) d# Z, D
- * @author student1
) l* ~- v. x! S - *5 k6 z2 c' j2 O
- */5 j# T, L8 t+ o
- public class BookDao {
$ ^/ {8 j; w' w5 R: w7 a a - 2 I( c' b: B8 J
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
1 X' k. a% k$ \ N9 Y9 t - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";, V; i9 Q) S" p8 K/ J2 [! m( {1 b
- private static final String DBNAME="sa";: ?) r2 X$ c8 n8 H; A
- private static final String DBPASS="1234";
( Y% w! |" `6 C0 j - # Z2 p7 O# [1 e* [2 X! G% M
-
" o+ S7 w; ^% L( y, ?* W - public Connection getConnection(){: J4 d& e7 M+ l- M- G! A8 m8 ]' {
- Connection con=null;
# E6 D* g: A$ U2 H - try {5 f6 |5 f* J( m) W( ]3 b
- Class.forName(DRIVER);
6 M. f2 _( Z+ ]4 `2 v4 P - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
+ ? X, Y1 k3 P1 A' f - } catch (ClassNotFoundException e) {; e3 j& f2 z( k$ G: l' l
- // TODO Auto-generated catch block) Z; ~3 K8 a% A) w3 F9 J
- e.printStackTrace();9 `0 s4 F* Q9 x- N8 K4 ^
- } catch (SQLException e) { i% z" j' h! d5 ~& u/ J( i
- // TODO Auto-generated catch block* ?! W" n2 l$ x' X9 t5 W- H- Y
- e.printStackTrace();
7 o- a4 F, A# K) Q, ^ - }& [' p/ b: O' ^& v$ v
- 2 L P; H$ k! t' }, u
- return con;
0 v$ W" o3 |: Y - : Y& L6 }( D- I$ Q- p1 t$ \
- }; ]9 e+ r3 y" w, W _, q# c
- public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){$ R5 ^; q$ Q3 K* t
- try {
' j) e1 T2 g6 Q5 M- ]3 O$ D - rs.close();4 |5 L( o: X$ U% I! P8 n- e
- } catch (SQLException e) {) }% m0 m8 K( n5 s' `, E
- // TODO Auto-generated catch block
& O7 ^5 `) P6 B g - e.printStackTrace();
/ C3 e/ u' n5 k) b6 ]5 f2 e3 F+ K) c - }" N" l- L# j' H% s5 Q+ f
- try {: ? i Y3 Z# X- k3 J/ V
- past.cancel();) R, b' h. W4 c+ r! b& J6 M/ N( S* L
- } catch (SQLException e) {5 `- y$ w/ v. Q1 d8 R! c( ]
- // TODO Auto-generated catch block2 L# E! A$ L( D' ~9 t
- e.printStackTrace();
: U2 g2 l4 \( t6 ^ - }, L8 r5 ~( o9 \) i9 H5 n# w5 s1 Q. I
- try {8 _9 d6 P8 ^7 J' v, A
- con.close();5 N! X9 L: s0 |0 x
- } catch (SQLException e) {
+ v" A7 ]2 V1 Y k4 W8 @- o - // TODO Auto-generated catch block% y0 U, l' K& r1 f1 J
- e.printStackTrace();/ a$ {. v+ w U$ p$ t& g' G+ I! X
- }
H1 w) M7 Z" Z% q - }4 S+ i7 X. y4 t$ i( V% ]/ z
-
" H( b, O7 V) ^/ u9 n( ?# P/ I7 q - public int update(String sql,String[]getValues){
7 l; W0 a* l$ M# u3 o - int i=0;$ C$ Q$ \0 M5 z2 _ Q
- Connection con=null;
' v$ Z+ z, H2 N! j# E - PreparedStatement past =null;
+ E6 T3 ^1 X7 }$ b - con=getConnection(); N0 \( V; Z* w/ N, U0 {, Y4 q4 b
- try {, K4 B9 U8 p/ Y" Y
- past=con.prepareStatement(sql);* w- P8 |' g) [ n
- if(getValues!=null){# W r7 Z# z* S7 W/ I% K
- for (int j = 0; j < getValues.length; j++) {
7 R; O, z" E% H8 F/ F: k; F$ y, w! m. A - past.setString(j+1, getValues[j]);
$ ~" u/ y7 ^/ |) Y - }
1 R; p0 s/ n! A( W - }! H4 F. s1 p# A2 \% [
- i=past.executeUpdate();5 Z2 C% ~- w. E2 i+ ~/ }, m" r
- } catch (SQLException e) {; b* @. A. P9 z- ?; P: o( C/ j+ Z7 i
- // TODO Auto-generated catch block( ]- H7 n/ Q; w: g" h, V2 |
- e.printStackTrace();" e) Y4 C- n7 z" `/ y+ {
- }' F9 h+ H% ^* A
-
3 z4 I7 F% B; e, A& X, ^ - return i;: {! i( Z( _# {# ~
-
3 H# X3 ~# H. A7 M. h' }( t& Z$ p - }. N# ]% c5 Z4 {& _6 W' g2 N0 ^
- }: {: m. A3 L6 m3 C$ V
复制代码- package s2.jsp.zhangxiao.dao;
, h* h' T8 ?9 M3 E, a - ! p' X2 c/ I. s' ^ v* d! C" U
- import java.sql.Connection;9 u; B1 j; r, ]3 s9 s; V1 `8 D+ z
- import java.util.ArrayList;" `+ o, a6 R: ?* \! X* @ x5 T# U
- import java.util.List;
9 {: G+ J, \- ^( ~ - import java.sql.PreparedStatement;( y6 b! C4 ^: T2 s5 T
- import java.sql.ResultSet;$ C& x2 O9 |: B- ~( e# j
- import java.sql.SQLException;
3 q- e/ c% }& H9 m - % u! w: P: T) U' c" P5 U
- import s2.jsp.zhangxiao.entity.Book;
- o/ k/ U6 R* M: V" t
1 S; f6 _- Q# `* R# }- public class BookImpl extends BookDao { D0 r, N+ r& _& X& G
( y( W5 x. D( H* @5 T- $ K c, ~# A% @7 N+ z; V
- public List bookAll(Book book){$ a1 q) D3 ]% K9 A/ c+ l
- List list=new ArrayList();" N0 i# D% H. P
- String sql="select * from book where 1=1";
( e# w5 {8 z0 b4 G: P* G - Connection con=getConnection();3 i# M6 G2 B" P" `' u. i8 B
- PreparedStatement past=null;' y& d6 ^1 P, m
- ResultSet rs=null;1 [+ g/ j+ c" O E2 a
- if(book.getBook_name()!=""){
1 x$ l& r! \6 }) i& p - sql=sql+"and book_name='"+book.getBook_name()+"'";& `5 w, l+ e9 l5 ^+ F
- }$ a( D9 }. ?/ V9 N. w, P. l
- if(book.getBook_type()!=""){
, t( S; l1 ~$ }; w; Y* i - sql=sql+"and book_type='"+book.getBook_type()+"'";! f3 Y- A f) i+ U# }
- }
+ |3 G+ i, d# O8 M' O$ o4 \ - if(book.getBook_publish()!=""){( n$ L& P; p/ k. }! T* H% X- a; Y5 j
- sql=sql+"and book_publish='"+book.getBook_publish()+"'"; D( d" E# {+ P5 O' i9 _
- }# B: H% T4 r0 [2 w# D* e
-
9 \5 C# \( N" J* t9 L, ~. \4 r: Z8 p - try {6 ~; F: Y8 N( @2 V1 `% h% r
- past=con.prepareStatement(sql);
. y/ W1 _5 Y3 z. u6 ?# k - rs=past.executeQuery();
2 h9 Y7 ~% ~1 I! u, c: S - while(rs.next()){) E% Y( {; E2 x b$ A) K
- Book books =new Book();
5 q% x1 f6 ^9 o6 I" B - books.setBook_name(rs.getString("book_name"));" I; v, P8 T' v2 I$ D( F7 d
- books.setBook_type(rs.getString("book_type"));( r3 l+ n v h6 e' V
- books.setBook_publish(rs.getString("book_publish"));
; y3 ^0 i& B4 V# S$ K - books.setBook_date(rs.getString("book_date"));' o b* u% ?1 G6 b
- list.add(books);
" a; l1 x1 B9 Z! U2 V& ~; O - }
, K' b5 t0 U. H5 N3 m - } catch (SQLException e) {
# ]$ Y v6 U" Z' e: H% b - // TODO Auto-generated catch block, k# `& O* d& \ s# Q
- e.printStackTrace();& G) t# S9 I8 D& k
- }
# a z i5 N: Q2 v - return list;: i. v2 @4 O; ^! U8 l
-
6 x, E1 N- V+ H/ m2 ? - }+ E2 b4 v6 @8 S) `! d1 O* h. [
- public int bookInsert(Book book){
- C3 ]1 D7 m/ V$ ~8 a$ v* Q2 o2 N - String sql="insert into book values(?,?,?,?)";$ p6 |7 L# q& Z9 }8 C) v! W/ f) I( w) B
- String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};$ n; V9 J- J( M) T* Y+ ?. u
- return update(sql,getVlaues);
% `* V) L$ e: u# c - }
/ C0 r( x2 ^- Q1 t1 W; i5 ~ - public List listtAll(){
% r1 Z9 ?- s- i' ~3 }& J$ P - List list=new ArrayList();+ s! W: u& [# t0 a; r" o) V/ ?
- String sql="select * from book";9 t4 S% C4 q( ?: y1 y( C' f, R
- Connection con=getConnection();
3 D; m) r* K, a" ^ - PreparedStatement past=null;- f# a! C+ Z- y
- ResultSet rs=null;
! K- W9 k1 z1 v) X; ]+ u! { - try {
* t' {1 U; D7 n, {6 v+ O0 K. c - past=con.prepareStatement(sql);- @& F& u" l y
- rs=past.executeQuery();
% T( l2 ~+ ]7 N |4 R0 h& Y% w; ?* }- o - while(rs.next()){6 a4 D1 n5 M( ^6 o) ~
- Book books =new Book();
- t5 q8 N& E& J; s: T! c8 P - books.setBook_name(rs.getString("book_name"));6 N9 U$ a0 x1 |4 j" x
- books.setBook_type(rs.getString("book_type"));
0 S' k5 v: l) K - books.setBook_publish(rs.getString("book_publish"));* T% v% Q$ f+ U8 e; t
- books.setBook_date(rs.getString("book_date"));& O" Y ^1 I' h0 d
- list.add(books);
( }/ p% z, O% X# b' G2 X - }
3 Y5 K$ `5 s c( G1 E4 ?5 C - } catch (SQLException e) {! a: y6 C5 F3 s t4 U
- // TODO Auto-generated catch block' r( f1 h8 |0 j' I. s H9 i
- e.printStackTrace();. E2 q8 w7 ]9 K7 \5 X& l
- }
+ Z& v$ [& P7 f - return list;
Q1 B# e: j7 m; _8 ~ - 2 \& a) x/ h8 C
- }
& O8 G0 ^) {* L, K - }! w0 |) m% D: Y. g& y( t; U! z
复制代码 源码下载地址:点击下载
) T9 b# _! U4 q! Q1 U
5 ]; K. Q5 ~2 B% Z+ `
! j4 a6 O: O, D7 @) n2 j |
|