该用户从未签到
|
- package s2.jsp.zhangxiao.dao;0 x# a3 M) \$ o3 D" F; d
- import java.sql.PreparedStatement;
8 O+ M: j, X& G - import java.sql.ResultSet;
; p. ~3 H+ B! x* |( N+ A% A+ m# j - import java.sql.Connection;8 }; b t; C8 l3 J8 v6 t
- import java.sql.SQLException;( P- S2 T0 v3 ^4 d" l& y3 d$ l$ p
- import java.sql.DriverManager;/ Y3 h8 C, \9 J$ b- `' J! G
- # b, c% _, |9 @2 }/ w
- /**% K1 n! ]5 Q5 l. I
- * 访问数据库2 \7 N. @) @$ _% H* v$ q
- * @author Administrator5 A1 H% q9 y w
- *7 y: ^+ b+ y% l* s+ L
- */
% i& i/ E& M' F& F - public class PhoneDao {
) R0 h' a" v& \
0 I, o) i. ]+ X u; t+ x- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";) ~9 D1 @3 f' P) ?+ q
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";
' p* d' X2 {1 B9 S; \- ` - private static final String DBNAME="sa";
. `7 D$ A4 J- M5 y* _ - private static final String DBPASS="1234";
7 _; x2 a0 p% k4 r1 G: [- |: l, P! h& D -
) A+ q( ]9 k" \2 l -
$ e1 s( a% k2 d3 d# E3 j - public Connection getConnection(){
6 M0 U/ B, {5 M: s- b# `3 S. D - Connection con=null;1 O5 e- G/ ]# p! X0 s; h0 T
- try {
; U0 [3 s" a1 U - Class.forName(DRIVER);/ y$ j$ i9 n' O2 _* ^6 \& p+ e
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);
/ e# b& w0 K. d9 L7 o$ y r2 P u - } catch (ClassNotFoundException e) {
$ m% y0 O' X2 C* t' K - // TODO Auto-generated catch block1 l$ G7 V2 [9 s! O2 M! o9 [' {' G
- e.printStackTrace();2 N1 C) t, q+ R$ q: D/ U; S
- } catch (SQLException e) {8 g7 f" \) {! x: }- J. i1 M
- // TODO Auto-generated catch block
" [' J, v/ \5 X3 h - e.printStackTrace();* H* j& s* Y& P
- }
) {: a. o: u7 d' a3 X - 7 z% Q- i/ m" O8 C5 S2 p
- return con;
3 O* K5 d/ C0 U ] - 4 t* y$ `. n" w0 _! c8 `8 E k8 r
- }2 ]* |3 D3 s$ p! S1 Z
- public void closeAll(Connection con,PreparedStatement past,ResultSet rs){5 i6 u( d" O+ I. e- B
- try {
a3 S& b/ @' A( g: r; h - rs.close();/ a) M5 p$ w7 Z0 }/ X
- } catch (SQLException e) {* K3 V4 }9 N+ I g
- // TODO Auto-generated catch block
! B- c0 p/ Y5 r; n" \: N - e.printStackTrace();3 Z+ [; N% k5 L
- }: o; a7 N* w$ j/ @
- try {3 h0 [9 c/ }& u; u5 b U" t& W
- past.close();
+ ^3 M8 z& f/ S. B: ~8 U; ^ - } catch (SQLException e) {
; t0 [& S. D) ]6 O0 u O. u! l - // TODO Auto-generated catch block3 q: J8 E5 i) s/ b9 k
- e.printStackTrace();
3 j4 E+ i: n; o4 L/ Z4 Y - }' e- v" q. g1 u2 b( c- X* f5 ]
- try {5 z1 ?' S3 M, |" T- y; r u
- con.close();' e: {9 W# m# W; A9 j4 b
- } catch (SQLException e) {
% y0 z0 `3 u8 f, T* t+ |; ~8 R - // TODO Auto-generated catch block
9 \4 i0 [0 j9 u6 K& ?) b - e.printStackTrace();7 A) h5 N/ S @ }# P
- }/ X; J% O, a/ Q0 j
- }2 o. V1 V1 k) R" G+ ]
-
! g* f/ P" N) T5 g5 ? - public int update(String sql,String[]getValues){" _2 S( }$ A0 s# x4 s w& k) L0 D
- int i=0;
, c0 X. J1 p; h: f- R - Connection con=null;1 T/ D" k2 F3 f5 A9 \
- PreparedStatement past=null;
. \, y; |8 a6 T$ d8 [. ?" C - con=getConnection();
& g; G% o7 n1 g - try {' L) j: l; ?5 C
- past=con.prepareStatement(sql);
& S" O: l5 p5 ^0 Q4 F; S0 X' P1 A - if(getValues!=null){% H& M5 q, V5 |+ e- L0 w
- for (int j = 0; j < getValues.length; j++) {+ ?( S% g# Y/ {# W
- past.setString(j+1, getValues[j]);
2 C5 ]% ~" Y/ e- O; U. f - }( j0 F* D, ?) r3 L8 y
- }, l" \* i" K7 G
- i=past.executeUpdate();
5 s+ G) n- L0 v. ^1 A. ^ - } catch (SQLException e) {
! W2 `2 F. k7 G6 s' K$ E4 I - // TODO Auto-generated catch block
9 b5 ], a( s l$ k# m - e.printStackTrace();
3 J7 [/ J; M1 z( c - }! ?; F8 e6 k* M
- return i;
' [% w% L m. _ - ) w6 D5 a6 W7 z, S
- }
2 m" F) X% u: r$ Z2 t% U8 p - }
# V2 p6 S" K6 R% G( q
复制代码- package s2.jsp.zhangxiao.dao;4 U" h1 O8 T" D. X/ w) w4 h- N
) G1 d: B2 L5 f8 m+ c" h- import java.sql.PreparedStatement;
% i; ~, q# v, l; [ - import java.sql.ResultSet;
# o7 D) j0 A% ^ - import java.sql.Connection;
( v/ E# M* G) `! v - import java.sql.SQLException;6 @* q" ]) i. B: C. v
- import java.util.ArrayList;/ S) O3 Q( S+ z% o& p* y& _% B( W
- import java.util.List;
. u E# `; _5 I8 S
7 L: Z/ H, K; M- import s2.jsp.zhangxiao.entity.Phone;5 S0 s3 i$ G# S/ |
- public class PhoneImpl extends PhoneDao {
) V: _+ F E( N; ` - * ?3 H$ ~; k& z4 ?* f J) W
- public List listAll(){* I2 y" n. J9 {: ~2 N
- List list=new ArrayList();
1 u ^ K# v1 p) g* x8 k0 J/ N - Connection con=getConnection();' i# z% S) v- O; S6 m
- PreparedStatement past=null;
! j2 V8 e, f2 [& z - ResultSet rs=null;
: [$ A, q ~" _9 P" B - String sql="select * from phone";9 l) s/ P5 g0 Z* ^- D
- try {: I$ A0 R( M# ~' M' H; O! Y* l+ Y
- past=con.prepareStatement(sql);
' Q' F9 a6 _+ S* h) S }2 b - rs=past.executeQuery();" q) ?2 U$ ?# s; t; d
- while(rs.next()){
+ S! Z3 D, w+ K1 O: ? - // id, type, brand, company, introduction" e# ~5 x; h+ l" P
- Phone phone=new Phone();
, _# ?3 E$ @4 z9 ~( M! [7 ] - phone.setId(rs.getInt("id"));
7 o3 E& r8 E4 S7 |1 C( E& z - phone.setType(rs.getString("type"));- b4 w' C0 n# |& _
- phone.setBrand(rs.getString("brand"));4 ^- ]% ]" u) ^* |5 E; o% u+ {
- phone.setCompany(rs.getString("company"));
- f% C+ o: i( Y, z - phone.setIntroduction(rs.getString("introduction"));4 n. z, {9 _6 N; D! J" v* M* T, S
- list.add(phone);
% ?" Q" A' m: @# u9 y - }; q+ T; ^ [8 i: L$ {
- } catch (SQLException e) {& {) y, `4 K1 b& a8 v
- // TODO Auto-generated catch block
# E; U- Z) x9 M4 c" e - e.printStackTrace();
' s* o g! m1 p6 Q5 h" P - }% ^% L0 ]" B2 W% {" \
- return list;
: b; K! k" |* ^( x& @: D7 p0 g& f - }
0 y% m. r& E, n. D - public int deleteId(int id){, V4 t a5 R/ B; j
- String sql="delete phone where id="+id;+ k* n! {* j5 W4 c) S
- return update(sql,null);7 s* e, z" `: w) S' U
- }0 H% v: ?9 C% z1 y
- }
" Z, N; ^; G* ~% j5 s; c% _2 L
复制代码 9 }; l1 D: D r+ n4 y" {4 ]
代码案例下载:点击下载
$ p: ~3 k/ L# V4 _% R
% e' O7 d2 z! c& T) X |
|