该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
B3 {1 X' i, t6 Q - import java.sql.PreparedStatement;) }. |+ ^9 K0 y' @9 c8 h" f! q$ M
- import java.sql.ResultSet;
! K9 `/ o% Q" b1 g2 ] H - import java.sql.Connection;% y# H. V( ]! S% R
- import java.sql.SQLException;$ R& v( l+ R$ E; ? Y" h! ` \
- import java.sql.DriverManager;
: F% f* W9 l" ]* O1 l ~
I' D4 W9 [6 Q/ t, Y/ i; K# X- /**
2 P6 J9 _/ R, b) F, F - * 访问数据库
4 V6 k5 ^. n1 b D8 u; c - * @author Administrator
% v; G+ b7 m8 q; R; ~# j$ [# y) | - *, O; |% U7 q, Y" N
- */8 O5 t, o; {8 X4 k* J
- public class PhoneDao {
( U1 X% H2 z' l$ C
* B3 e3 R1 e) x, N- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
' Y7 e" j8 H- k4 P9 \# G( [6 n - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";, n% [( y2 |) ?: B! c
- private static final String DBNAME="sa";! c4 H) L2 ~" f/ A/ C' Q
- private static final String DBPASS="1234";
) p$ n* p) s, G& X: [ -
. W% d0 N9 u0 _) s -
2 \ ~! l2 C5 ?3 o, F3 }. i - public Connection getConnection(){" @7 e3 t- ]4 E. v
- Connection con=null;2 ~' P0 p9 q8 K3 r" s
- try {
4 h9 X" [0 Q9 a - Class.forName(DRIVER);
: N1 Y# m/ f1 Y o2 p+ D; |/ u$ z - con=DriverManager.getConnection(URL, DBNAME, DBPASS);9 k) L) v: e5 o
- } catch (ClassNotFoundException e) {7 M+ `# F/ i8 j& U i6 j
- // TODO Auto-generated catch block9 H$ g1 _6 I+ V3 P9 ]
- e.printStackTrace();) R* ?: {0 }* ]% @: _0 t( u
- } catch (SQLException e) {9 g1 s* P9 W# R9 s
- // TODO Auto-generated catch block
4 @8 B0 M( C! |, ] - e.printStackTrace();
1 C* l- c$ f% V# w - }: \7 U& P4 [% j G
-
4 x! t$ q2 r5 J1 ? - return con;
/ k) T1 _8 |, z5 U8 a1 t% Y - & B/ f; F0 k2 ?1 r% d
- }
/ G% s! E! P. D - public void closeAll(Connection con,PreparedStatement past,ResultSet rs){
# z! q- {3 f% ]; Q, i* Y% p- E - try {6 |9 X9 q7 @4 c/ \8 ]) ?
- rs.close();
) _+ h+ G: u$ D6 D. C% @ - } catch (SQLException e) {
/ Y1 Y+ e; G* |: H - // TODO Auto-generated catch block& H- A/ F0 v, B! W# t( L
- e.printStackTrace();
' a; k) |2 t H9 C5 c# N. ] - }
1 c( i" V0 R- | Q8 Y! S - try {
7 _: o- `* @4 `" A, N$ \7 l - past.close();
4 O4 R3 x5 e$ W) |( _ - } catch (SQLException e) {
: E8 M7 G! ~6 N/ u8 Z - // TODO Auto-generated catch block
4 j% }1 |0 i. p - e.printStackTrace();
; B c, D5 s! k2 N# f- R - }
: T! y& H( i2 ` K# I - try {
& f8 s$ Y1 N. |5 P* p - con.close();6 b% u$ Q, s# i) f. n
- } catch (SQLException e) {; a5 f8 r o- E z- D
- // TODO Auto-generated catch block
" B- E9 I. ?, ?$ w. l1 h - e.printStackTrace();$ K, w M5 F% x% }/ O- H! W9 K
- }: j3 c: B7 e( n
- }
6 _2 X7 G/ s3 b8 m i+ T% P3 [ - ( @8 W/ B5 }* I4 x2 n5 M
- public int update(String sql,String[]getValues){/ U: B/ B; U' A" ^0 g4 N
- int i=0;
( j$ o0 _: A. N% ~$ p( T; T# p4 Q - Connection con=null;
# e9 |1 X" x) G$ C! @: } - PreparedStatement past=null;
; k6 S* S: o3 G- G2 e* m! a - con=getConnection();5 `/ r* B" m2 U3 c0 Z
- try {2 j7 Z- X8 c5 Z' O$ m8 [
- past=con.prepareStatement(sql);% Q5 R6 d1 P S8 C( v3 \- g" {
- if(getValues!=null){
, R- y& _/ l' O% k: E5 F - for (int j = 0; j < getValues.length; j++) {6 K4 L% B4 @) U6 E8 ]; F$ w3 O
- past.setString(j+1, getValues[j]);! h2 Q, y4 K7 d! }
- }
1 B; g* d- R; n& Q S w - }
1 [8 D3 n; m1 L - i=past.executeUpdate();, W W& z; ?: s f& a2 D
- } catch (SQLException e) {
" i5 d) S2 ~) ]$ p$ e - // TODO Auto-generated catch block
9 y6 Y" D5 @8 Q8 ^" j, A - e.printStackTrace();
$ } i" c$ N# y+ g m( U - }
" d# z( n- X: U2 R( q4 x/ l - return i;3 [4 L" s& p0 E0 K
- # K: A r$ l! |! X
- }# j" ]$ ~: V( m4 T
- }% J' [: A! X$ l5 `$ V4 [
复制代码- package s2.jsp.zhangxiao.dao;
6 b9 f1 p" q6 U1 M
9 b: d. P; E- Y- import java.sql.PreparedStatement;# E! G6 ^" v/ C/ X9 O$ h3 j
- import java.sql.ResultSet;6 `0 C+ z) {7 l# P
- import java.sql.Connection;
2 U7 A/ r2 S+ T$ \ - import java.sql.SQLException;9 q1 Y/ ~, U- U) y9 ~! Q; B
- import java.util.ArrayList;
$ Q+ |0 G& x( Y1 \+ u6 u: s" y2 G - import java.util.List;
8 i* p! }2 v% p- x - 8 V( ?# p- b( T9 Y7 X/ ~
- import s2.jsp.zhangxiao.entity.Phone;' i) R8 }1 e' W) D/ @; H" [
- public class PhoneImpl extends PhoneDao {/ k% `" P! k/ S$ k" }
-
" B+ ?0 o' l7 Z9 \ - public List listAll(){
, y; E2 E5 G( Q& d* W5 Y) {# i- X - List list=new ArrayList();
5 n+ z* E) v9 ~) D - Connection con=getConnection();
* j' L6 A& S/ g5 T5 l# ?0 A+ c9 H - PreparedStatement past=null;
0 C9 d/ a/ C1 f8 j) F/ b0 y, S' {0 _ - ResultSet rs=null;5 Y) y) T. `. G: r/ W* E7 _
- String sql="select * from phone";
8 L$ y; Z) C$ [0 Q/ k - try {6 P4 `9 f0 U+ G5 l+ P
- past=con.prepareStatement(sql);
, Y, ~7 D% ~, u - rs=past.executeQuery();; D3 s$ {; @8 H+ p% Z$ G* \
- while(rs.next()){
1 _1 A- @/ n" p( G, N - // id, type, brand, company, introduction
8 [: V$ A& H+ g- _( M( B7 C - Phone phone=new Phone();
_- M% _% Z1 I8 j! w4 C - phone.setId(rs.getInt("id")); G& T8 {% S1 |
- phone.setType(rs.getString("type"));
8 D+ [1 u8 _% J( j% G - phone.setBrand(rs.getString("brand"));5 [) L0 t$ e( r- d1 G0 w, K5 N+ R
- phone.setCompany(rs.getString("company"));7 H/ _) C5 R. o Z
- phone.setIntroduction(rs.getString("introduction"));8 M, ^0 t9 d5 U
- list.add(phone);' b! q8 e+ @) J0 y2 t
- } r: e' o! V: }- b3 G- w
- } catch (SQLException e) {6 i5 j6 x! t) j$ X8 K. ~
- // TODO Auto-generated catch block
8 w1 N# S% G- V: a$ {( i6 N2 {5 p - e.printStackTrace();
+ ~* Z0 v% k- o; ~: A" {! @: | - }
) E3 J2 B6 z# G" l8 V$ Z: { - return list;. x) l, B% a* K q' B
- }
1 |% T* e) t7 f: j; `" i3 F, i - public int deleteId(int id){/ z4 c* M, Y: f
- String sql="delete phone where id="+id;; Q1 h1 r8 c! s# `. a) n* f
- return update(sql,null);
- }- R) S& D" H - }" ?: ?; q( J1 y% s. ^) r, z d
- }. T, [7 `' {8 e+ G
复制代码 ! L+ k) N' O2 b9 ?2 ?: O
代码案例下载:点击下载
3 ^3 f9 x/ w7 b' n5 C! _9 F6 F
# y" }! T. Q; C7 E& E |
|