该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
* m; I( a! j: t: j+ k/ \6 t - import java.sql.PreparedStatement;
! A" u" F: t% W! s; `0 ?* b9 b - import java.sql.ResultSet;
. I; F8 j# x. ~+ p: Z6 N+ ^1 E# n - import java.sql.Connection;" ^ `, T" W+ Z' c) E8 G$ K
- import java.sql.SQLException;/ Z p! \) P, ?
- import java.sql.DriverManager;
) A+ I( A8 {2 `. I7 M) i4 h - . W/ H" _3 j6 K* |+ X) B$ Q7 J) \
- /**4 N: Q+ N; L1 v9 c/ j) P+ Z, e
- * 访问数据库+ t; z5 B1 l& Z
- * @author Administrator
8 F2 D+ A# [: z9 W - *
+ e% W8 t2 c: o: C/ w - */
3 Z3 G' `9 N- V5 V( C9 I' D - public class PhoneDao {
7 q' E2 K) a `5 c& Q$ I- G R2 I - 2 E! ?# C$ @! A! R6 ]
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
$ s8 {8 q! P: C# D# F w% n - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone"; ^/ I" A' m( V1 M7 j5 n+ m' X$ h
- private static final String DBNAME="sa";
4 h+ y# k7 Z" ~, S1 `# K8 R - private static final String DBPASS="1234";
& Z0 e& I1 w. W9 f% A% z7 C - + N/ [! s: C$ A, i- v
-
; l2 Q9 e3 x4 d- y- E2 y - public Connection getConnection(){9 M9 q L. F5 `" l' f3 C' @ C( g5 \
- Connection con=null;
, o. v! E5 i m2 K# }6 |9 L - try {
E9 [% Y/ \! O) s - Class.forName(DRIVER);
' B7 F' d6 g7 D# @; h" d9 v - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
# ^/ k: `4 _& _" \ - } catch (ClassNotFoundException e) {
7 ?9 G; W$ A; |2 D+ g( w - // TODO Auto-generated catch block& O+ I4 s' G* P0 o: s
- e.printStackTrace();
0 F- S5 E w" K) x& w - } catch (SQLException e) {: k& G* A' v& L% p5 t( u% c/ M
- // TODO Auto-generated catch block6 g, D0 K% ~2 w) H- Z7 V# h7 z% I
- e.printStackTrace();2 P9 c' U3 D& _' r
- }
; j% D1 ]/ y9 w - - l5 ~; |* Y/ q, L
- return con;
- H0 l8 Z1 ]4 v/ i- ] - , X+ B) T; D U/ n+ Q
- }" y5 M; X* h' h" Z3 _
- public void closeAll(Connection con,PreparedStatement past,ResultSet rs){
7 \8 o) X/ t8 E6 m5 f! u0 _ - try {" H/ `% w& r3 K9 a7 z" H2 }
- rs.close();
$ J" ]; n( n; W, a8 c: S" p - } catch (SQLException e) {
. I) M7 t/ i: m9 y - // TODO Auto-generated catch block
4 t" q" \3 ^. T- J: i& j. h* H - e.printStackTrace();
/ V) h- a! J7 Y8 M, s% e" u/ T! U- x - }: N# G) j6 [9 c' i! i
- try {7 e, J$ S% n7 R V
- past.close(); G9 \! C$ N; Z' Y8 y
- } catch (SQLException e) {
9 P2 n" \" p( @: h* {; Y - // TODO Auto-generated catch block
5 Y: {; Y1 N- m- ]3 K - e.printStackTrace();
7 n+ N) a! `3 y0 a8 c( p" B1 [$ F - }) y6 ^: K0 I$ K( D' ?% |' Y$ a1 q
- try {4 b% `5 J. Y- o
- con.close();
' A, s+ ^9 g/ T - } catch (SQLException e) {
) Y( Z) \+ }9 f9 \ - // TODO Auto-generated catch block& K) t+ v! d. M- `4 W. O3 L
- e.printStackTrace();. k3 U5 L8 h( R& n
- }
7 L0 [% o" D5 |% c9 ^6 z - }0 C. J3 w2 |* i
- - b4 d$ D# ^7 p6 D- {* ]+ p
- public int update(String sql,String[]getValues){
7 h. X2 ~) a4 f% @' R+ c - int i=0;
& _, `: ?; ]$ w8 f- C. E - Connection con=null;
7 Z8 U- c3 @7 ^& b! Z - PreparedStatement past=null;
# g4 I8 d4 I9 Y* f, o- z w - con=getConnection();
, d2 m$ `$ n- m# a$ V - try {
3 b6 d( d, j1 U0 F - past=con.prepareStatement(sql);
0 o) }* \4 u8 U - if(getValues!=null){% T! d2 \5 L) t' v" W
- for (int j = 0; j < getValues.length; j++) {
% F% `' R' \" {; u! g - past.setString(j+1, getValues[j]);: ]( z- e" L# H8 y3 ~0 h
- }, J' Z( W, |" R) ~9 N/ _8 x
- }4 `4 P; K& B" f \
- i=past.executeUpdate();2 y( N8 N5 W6 e$ n
- } catch (SQLException e) {
( v! n3 x U+ {- ?) h - // TODO Auto-generated catch block
. K; v0 I+ W! a! e( a7 m, @7 ^ - e.printStackTrace();
; ~3 e6 a4 f. o$ `3 {7 _ - }$ y' ]' v7 t/ r0 a# H2 l. `
- return i;
9 I% G; m+ S: W4 O' l7 k -
( D8 d& O8 v- x% p* }( v/ R7 E - }
6 L: K" {! ^; I( M' x% ` - }9 b# w) Y& P5 m2 ^" `3 g& d5 R
复制代码- package s2.jsp.zhangxiao.dao;
+ w V+ e( d' i/ @+ s& m' ` - ! ?0 f; k8 x8 q0 u! I, {$ Y
- import java.sql.PreparedStatement;. n8 H4 W' Q7 b! R
- import java.sql.ResultSet;
9 |& |* f R) o; ` J. }3 ~7 o" p - import java.sql.Connection;9 j' P. j2 g$ x) O B" O4 e. K f" w
- import java.sql.SQLException;! h$ \/ v. s: E4 j% e
- import java.util.ArrayList;
3 R2 M3 [. [$ m4 c# S - import java.util.List;3 K3 S: m) L: O5 [6 w
- h2 s( R& l1 { [1 @1 `+ O+ g- import s2.jsp.zhangxiao.entity.Phone;- w. S* C5 e) w# J8 h- o! A2 v
- public class PhoneImpl extends PhoneDao {
6 l% i- C% M7 t& O - * W6 I$ g' w9 |# x/ o7 `$ M0 f
- public List listAll(){7 y# u; z% f; ?6 |( Y
- List list=new ArrayList();
3 y3 c3 k' Y$ x - Connection con=getConnection();$ D; x* O5 a9 B5 P0 L
- PreparedStatement past=null;& d% a0 x# C+ v$ a+ t
- ResultSet rs=null;$ j; k5 u$ k* a: g
- String sql="select * from phone";2 A3 i1 k2 \$ ]3 d l$ H9 H; s
- try {
/ C* a J; H1 v% j4 G: r2 F - past=con.prepareStatement(sql);+ \% u& Q! |, f' I
- rs=past.executeQuery();
0 M T. r' I+ Z5 \ - while(rs.next()){
" b- v. s% r7 q - // id, type, brand, company, introduction
, l, O/ P* n" L3 l( P# Y& m - Phone phone=new Phone();
+ j7 _9 S! U# _) d5 Y& P - phone.setId(rs.getInt("id"));# X/ w6 a! N% F0 L j# a
- phone.setType(rs.getString("type"));& z; R+ k. v& [. L2 u* q [( Y
- phone.setBrand(rs.getString("brand"));/ `, v* p8 z" O3 G# G4 G1 s5 k0 A
- phone.setCompany(rs.getString("company")); B. n2 ~; Q" D$ i8 B/ m
- phone.setIntroduction(rs.getString("introduction"));
% j" o, E# d* \' Y5 i: |' O - list.add(phone);
}/ J: Y8 Z8 F6 T - }
+ y7 ]# \4 f" { - } catch (SQLException e) {4 W7 _ k" s- P6 l% y
- // TODO Auto-generated catch block
$ O7 y! Z7 u- f" R - e.printStackTrace();2 D2 {! X% S) T2 I7 E3 C
- }
) ?% f. R( X0 k$ s, K# h - return list;; r4 z$ x1 k: ~3 N' p% S
- }
D2 Q. [ w: v! C$ [9 Z0 d - public int deleteId(int id){
$ b9 i3 i- w6 A2 B' o4 [6 S - String sql="delete phone where id="+id;
' ~- z1 c) w& Y, S3 ?5 K9 U - return update(sql,null);4 B' V i& l, s* a
- }4 S$ G7 Y0 w* m1 d
- }
/ K4 u8 A' Q; f. J' M9 X. j) S% |
复制代码 " A K# ?3 G7 ~- Q
代码案例下载:点击下载7 U, t& P5 F* u7 x2 S) @% E
$ i( G. l! e+ i
|
|