该用户从未签到
|
- package s2.jsp.zhangxiao.dao;6 \& V: Q% @2 Z& u
- import java.sql.PreparedStatement;
9 b9 D: R' x9 t6 ` - import java.sql.ResultSet;
7 L5 u. a" s; D; l! R - import java.sql.Connection;
3 d- ?6 a: o0 T. t% F - import java.sql.SQLException;
/ L x; u B/ M+ w - import java.sql.DriverManager;
* M. O% ?3 [- l! e$ g - ; C. w% H9 l) T$ ?* u
- /**
, Q2 s; P% m9 I. s% U% @ - * 访问数据库$ r, z% b9 P& G- {( J
- * @author Administrator% V$ H3 Q5 _- L
- *. ?% d0 i1 |; T% ^2 R
- */
9 _" R8 `# x5 h - public class PhoneDao {
" a' A, p, b3 E
7 |- G- Q3 q5 N1 R- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";: J& r' ^, M" a
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";% T4 M5 p3 z; x0 ^, w& l; a7 M
- private static final String DBNAME="sa";" W6 t1 V6 L8 ^4 L& I, i. U9 f! f
- private static final String DBPASS="1234";
. N% J: R, Z* ~5 Q: V -
4 h' C9 g, g- t" Y* i - . |) N8 I1 N I9 e
- public Connection getConnection(){
+ C7 {7 I! W# T t$ `2 p3 { - Connection con=null;
3 _ p$ b+ Y+ p: h - try { V; m2 i# w7 Z8 [1 R o
- Class.forName(DRIVER);. |1 U2 H' K, z
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);
9 q" M0 v, q. }9 i) ?- t, V- G - } catch (ClassNotFoundException e) {
( k1 g0 a$ @- I. ]( d - // TODO Auto-generated catch block
2 G6 |8 w) ]6 j - e.printStackTrace();& W, O$ V# [: ~
- } catch (SQLException e) {
3 c/ z0 x, `* [# i7 A0 r7 k+ s - // TODO Auto-generated catch block' {, U4 _: X' {6 V# q& K
- e.printStackTrace();7 R8 Y. D1 ^- V; Y4 w) Q5 g. k
- }
' X. ~1 t& p, M2 r o% H0 W% ^ -
: X( V! `$ w& Q0 f# S. x - return con;
/ d8 o+ i2 q* S - " r4 W( ?" h' T9 E P2 }
- }& ^' ?% U+ @' b/ a
- public void closeAll(Connection con,PreparedStatement past,ResultSet rs){
& p! e6 M' |" m5 P1 \" n1 L- h - try {8 |' {$ t2 `- a: S
- rs.close();+ l h0 i1 Z* D6 i
- } catch (SQLException e) {) N. Y$ D# o8 g* B
- // TODO Auto-generated catch block
( x- d7 j. G; H2 H - e.printStackTrace();/ ]7 l% c @! N: n6 k! x
- }
$ }8 }( u2 @; n0 _3 u - try {
" F# M: J/ Y6 R4 |* q2 Z. Z' |7 f - past.close();
) F9 L, a1 G' u+ X+ i8 x - } catch (SQLException e) {& _1 A" K' R7 G0 u
- // TODO Auto-generated catch block, w3 [& d( E" y1 C5 E5 l1 @
- e.printStackTrace();( s Z8 N' r$ S; B* ^ h6 ]
- }
2 F8 G% _. c b! ?6 a - try {) q1 G# q# W( ~. D4 r
- con.close();, W, ~! e- S0 q: z4 ^- \5 q
- } catch (SQLException e) {
5 `! @# H" s5 b0 Y - // TODO Auto-generated catch block
5 I; f# j1 y3 B8 D' T X" V# L. C - e.printStackTrace();0 R( b( O1 O) ~
- }3 R: a; q8 A$ B2 U! T! ? h' P- t
- }
3 y# X% f$ K$ N" W4 ^ - 5 Q7 Y9 l5 [5 r
- public int update(String sql,String[]getValues){
, B! o/ Z9 B$ J+ h - int i=0;
: a1 U9 }& |8 Y$ w. m4 v - Connection con=null;
7 l% f, l9 y( H6 U. l - PreparedStatement past=null;! l b7 v3 z( |7 B
- con=getConnection();
% p* B5 M6 Z! R/ }9 r - try {
! U! ^; e- D' P2 ^( t+ t - past=con.prepareStatement(sql);
: n5 E9 T* p1 x# O9 L R - if(getValues!=null){
3 [: b. ]$ c4 ], s! S - for (int j = 0; j < getValues.length; j++) {
' W8 D1 }! o; c8 p' ^ Y5 U" F - past.setString(j+1, getValues[j]);
% Z/ P, H8 B1 u4 M& j7 D0 F - }
" {2 i0 |; `. D - }
B1 J, l; P v; E9 K2 q6 O) n. d - i=past.executeUpdate();6 g! b# f0 s0 o- w
- } catch (SQLException e) {
2 Q% b; q0 z {' q- M4 K - // TODO Auto-generated catch block
+ g( X. \/ U5 G. O7 ~ - e.printStackTrace();
6 d8 \ D- G9 S+ s, f9 ~ - }# y* Y' k) i* G% n7 X2 ^( K2 Q
- return i;$ A6 j: A5 o* U8 y: I7 R \- t: s
- - S$ I2 U$ N% v* L& c
- }( s) S/ N; _7 J; N2 w. c/ N4 A
- }
: p) l9 `+ n( s; J% X/ X" c
复制代码- package s2.jsp.zhangxiao.dao;
7 ?" U" u# s* ?+ _/ |* p0 `
# X6 e# p; i X- import java.sql.PreparedStatement;
+ x% r3 B4 K5 g# M0 p9 U2 @ - import java.sql.ResultSet;# n% s. ~9 m3 C3 N
- import java.sql.Connection;4 \3 R8 S: K- c3 Z
- import java.sql.SQLException;
" n R3 o4 P0 H' K - import java.util.ArrayList;% S8 N# n" t! u% ?
- import java.util.List;
4 O: [0 e( k q9 |6 p& Q - 2 S" l8 Y9 o7 m7 _' r( }& P, r( \
- import s2.jsp.zhangxiao.entity.Phone;! U1 M6 o, [' b; s8 d
- public class PhoneImpl extends PhoneDao {
0 }- Q% {+ h4 {0 r4 l -
- Q6 Y- T6 V$ s - public List listAll(){
# u. M0 W; W% A& f: q& R - List list=new ArrayList();1 \3 z& o; o2 r% [$ d: L4 {
- Connection con=getConnection();+ f: X4 Q! {& K( \1 u S
- PreparedStatement past=null;; |# m3 `/ c+ `
- ResultSet rs=null;
) Y$ d) k. i1 W4 G" {/ E9 w - String sql="select * from phone";
/ v% g! X/ Y+ r - try {- X( u4 @; `# I P9 m3 c% c* Z
- past=con.prepareStatement(sql);. a1 T: s6 N; F6 a# E6 I/ s* m# r
- rs=past.executeQuery();
. H; ]) y* }2 o6 s' ^! c - while(rs.next()){
; f$ D+ I* \8 g: p! }2 } - // id, type, brand, company, introduction
4 s5 j# f: p8 u2 L- E - Phone phone=new Phone();
S0 |2 |. Z4 d8 G - phone.setId(rs.getInt("id"));4 G0 f' P! v" k4 C5 ?
- phone.setType(rs.getString("type"));
\$ r+ K3 m9 s; X- c - phone.setBrand(rs.getString("brand"));
& I& ]+ T, g" L- e1 F$ N$ W - phone.setCompany(rs.getString("company"));- u1 B* f }) o# L; w
- phone.setIntroduction(rs.getString("introduction"));
+ P& U: P+ j) ^# g: ?! Z; y/ [ - list.add(phone);% ?2 S1 i( i8 V) N
- }
( ]6 D' f3 K5 ] - } catch (SQLException e) {
, N) H7 e# a; g - // TODO Auto-generated catch block7 ^3 Y1 S$ r$ y4 b7 F
- e.printStackTrace();
0 s5 s/ \; T# J P - }) g- q1 n, {, s4 p
- return list;
|4 A* V, O/ j2 O9 X - }" ~1 H# |# Z" I, N: {' _ b
- public int deleteId(int id){6 F- ]1 ~7 H) @- W
- String sql="delete phone where id="+id;) [+ L+ f( T6 Q/ k
- return update(sql,null);
8 P% B7 p2 n9 L0 p: W - }- m. ~- r. y$ J& h) K* F
- }
) n; w) u9 w" l
复制代码
2 t0 Q$ P3 L- J& z3 _代码案例下载:点击下载$ a" }1 B3 P" R' x* h6 y: C
5 q3 e6 h, Q4 ~! P/ | |
|