该用户从未签到
|
- package s2.jsp.zhangxiao.dao; }% K1 @* V0 s; g# ]
- import java.sql.PreparedStatement;
% u" _* f. }; M5 }0 k4 w" `1 Y - import java.sql.ResultSet;
* R! d* D f% E* P3 U - import java.sql.Connection;
3 d% c# x2 c( x% H2 O1 v7 E - import java.sql.SQLException;/ W* U5 W' q: |/ n$ X$ e1 T0 N' _
- import java.sql.DriverManager;
8 S# U% d2 h9 S, H: S9 i
3 X! \2 ^, C' r( b/ M% X0 d2 S- /**
# m6 c; e9 v* E* M* h - * 访问数据库. p4 P/ C) B2 ~
- * @author Administrator& k& }6 S9 B/ S2 B( k! O" ~9 _) S
- *
4 Y( L a- I% R# {' j& {, p3 q - */
8 g3 z% B& J7 y' w }: z - public class PhoneDao {
/ p6 V1 |5 R+ @+ A7 }/ X3 e
) E1 _, p8 O; `2 F: f4 l- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
0 I9 u% _( r, c" }' \2 X8 k - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";& B) u, y* O/ @6 s( f6 X" e
- private static final String DBNAME="sa";; P$ o! V1 v4 |
- private static final String DBPASS="1234";
0 h9 p+ T! Q) b: c -
" W' ?; l D9 |9 b4 H -
4 f: q1 N, f' @6 R3 q - public Connection getConnection(){
, X8 q+ F3 ^* d; K6 z - Connection con=null;% S/ ]* P9 d! j( S7 J: k0 h
- try {0 z7 x3 n4 }' o/ Z2 a
- Class.forName(DRIVER);+ D. Z, I- d. w$ y7 Y1 b, h
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);5 H& B& P# Q" f- U9 B* T* t. u) q
- } catch (ClassNotFoundException e) {
; V# x) q# ~; D, N% S5 V - // TODO Auto-generated catch block) F4 B2 P& B# @ F) Z
- e.printStackTrace();, v. W1 L Y2 c! l
- } catch (SQLException e) {$ C; G" c5 @# M5 ]( S
- // TODO Auto-generated catch block, c f2 p4 \6 u+ H3 G6 S& ~
- e.printStackTrace();5 B+ J' q/ K8 ^
- }& Y. G! z4 u( C6 D
-
) v' f, x/ w) f; _* x6 ^2 H4 A. p - return con;! e# s9 A: l- ]4 k* r
- 1 x' ~( ?9 N& w. `6 e; M# `1 t
- }, S8 i9 y+ A6 |0 R2 D! ?3 O' p
- public void closeAll(Connection con,PreparedStatement past,ResultSet rs){% s/ v6 }4 {* n, m8 i% ?! \
- try {
+ c+ N1 L$ F$ V; e& ~ - rs.close();
: o' O" U8 X* F! ~2 c. C - } catch (SQLException e) {1 j6 z9 l: y; G# Q% ~; j! i
- // TODO Auto-generated catch block
& I; U1 [5 p* O. d - e.printStackTrace();2 Z- B$ f6 s! ?. c
- }$ N4 C3 {( P; L0 n5 S3 ]: |4 s3 H
- try {) [/ B/ { B" N3 x; |
- past.close();
. t3 h0 W8 ?6 l; l: C - } catch (SQLException e) {
9 @+ p4 G( g& p, @& n- y' g - // TODO Auto-generated catch block _& D5 X8 y" R* V T5 O
- e.printStackTrace();1 w4 l- b! M: S1 e8 ^
- }" z. C A4 i0 ^- L2 f& x3 a
- try {2 {' H# c/ ?0 j/ M' K
- con.close();2 V/ s' M( f2 n# P% k9 { x
- } catch (SQLException e) {
$ K/ c3 C( ~8 c ~1 r6 u- O7 W: a - // TODO Auto-generated catch block' _- n6 c x% @9 o
- e.printStackTrace();
& b" L: a+ I; x8 r3 ~' B X - }
8 V0 H; C( S4 H% \; C - }/ f2 y) E1 k# [$ s% ]2 k
-
* z, H2 I! I3 p. u* x4 A - public int update(String sql,String[]getValues){
% u1 \5 o5 j6 }! M* i% Y - int i=0;
" ]/ I- I/ l. i( G1 Z - Connection con=null;
2 d& i- w. H1 d; r. S) E - PreparedStatement past=null;
4 k, e6 |; H% ^0 u2 K/ O0 P - con=getConnection();
0 H: h) V n. C2 N0 `9 B: M8 E4 A: J - try {" o4 q/ A/ v8 |0 t$ P5 G9 X2 K3 L
- past=con.prepareStatement(sql);
2 a; Q. p) I8 o# p - if(getValues!=null){
4 M6 T4 d# x4 R& k( T5 m/ \ - for (int j = 0; j < getValues.length; j++) {9 G: `4 T+ Z* H& N) N; w# K
- past.setString(j+1, getValues[j]);
. K# n" z- N4 |7 A6 K3 z, o0 B - }
# |* M7 N0 R' q/ n - }
0 ^) w9 Y7 b8 k7 M- r - i=past.executeUpdate();
* u0 r. W- ~/ V, A" y - } catch (SQLException e) {, m0 E& b' V3 a; M* k, R
- // TODO Auto-generated catch block7 u% Z2 R) E- L$ M* z
- e.printStackTrace();& ?! | c9 `+ b
- }
t& V8 ^# K) O9 k+ H; W% U9 P - return i;# I) J6 Q3 `) |6 ^6 U+ a9 A( |
-
; s D) C- d5 O! X! v - }
* E% I% B& q! R( V- d7 p - }8 [ {' l' H3 ^( B0 l, H9 n( `
复制代码- package s2.jsp.zhangxiao.dao;
* n. ?7 J9 d4 q3 o. Q3 ~ K
# F# w$ C ]! n2 [. x- import java.sql.PreparedStatement;+ E% F" ?& M: {8 R- W
- import java.sql.ResultSet;9 o; v$ k7 E! \, f* T0 `
- import java.sql.Connection;0 @. w4 O* j$ @0 G3 l8 k3 I
- import java.sql.SQLException;, W: e5 R) k" p: O( d
- import java.util.ArrayList;
) q% R* b7 n% V) V2 r& y3 m6 N - import java.util.List;4 a. Y9 a6 X/ M3 J4 M9 I
6 O6 F: L$ O9 R7 j- import s2.jsp.zhangxiao.entity.Phone;5 S: Z! V9 a7 Y: p1 p% C
- public class PhoneImpl extends PhoneDao {
: {9 q" d# ~. b% k& { - % M/ V$ G7 E, @! Z9 x7 `& o
- public List listAll(){. ^0 V5 Z/ H* V( c5 D+ O
- List list=new ArrayList();
! r4 Y7 s5 ?4 x1 w! s1 C - Connection con=getConnection();: ]7 r# i4 Z( h
- PreparedStatement past=null;# s5 N* |# B( c' o8 V0 n9 `5 f& V
- ResultSet rs=null;0 g4 l, `) C4 l' d& {
- String sql="select * from phone";4 ?2 f [5 k* _7 q2 b9 J
- try {% M) j& A( C* g8 R/ l
- past=con.prepareStatement(sql);
$ P5 i m1 g9 }; H/ ~8 y - rs=past.executeQuery();- s v0 o, N1 d5 Z8 t" M, x! a
- while(rs.next()){1 d+ D. v9 n7 k% @! Q$ b! C
- // id, type, brand, company, introduction, q' k- w1 S; }9 B
- Phone phone=new Phone();
8 q% z2 U; j8 y( F1 R9 S" X - phone.setId(rs.getInt("id"));
) q9 a+ Y v9 {0 B$ @4 C - phone.setType(rs.getString("type"));2 ^! a: u" y8 |: w$ Q
- phone.setBrand(rs.getString("brand"));
# Q2 v5 t I6 z" e$ }+ u% B) J - phone.setCompany(rs.getString("company"));
- \& U5 |- u3 M) X# `% f - phone.setIntroduction(rs.getString("introduction"));7 f' U+ u `' e4 I& v0 c
- list.add(phone);/ I6 [( `+ ]7 l2 \, b" H4 j/ L: s, T
- }
3 p# a/ U; H1 c4 s - } catch (SQLException e) {
( @" M7 G5 F( ` - // TODO Auto-generated catch block
( C- t& D( O: s: t - e.printStackTrace();! k! B( k( g2 k2 T0 y5 g
- }, }9 f% P5 B/ X
- return list;, }4 r# S0 Y* @
- }
% W: D" Z4 V- s q - public int deleteId(int id){
2 y) i Q# h( v1 | - String sql="delete phone where id="+id;9 Y7 \3 k& G0 a$ [. N* K7 {* n
- return update(sql,null); C t2 o' \# f' A6 Z; ^
- }- F6 S- t9 a" Z& R
- }7 l/ ]( ?3 G) J0 V1 k
复制代码
# k) x! N' z8 E" r: ^" F: g& b, V H代码案例下载:点击下载 N: e4 y7 Z, k( |
) S5 z( y* g6 v, z7 _$ X |
|