该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
! o* y; o1 y# n; f& \; E4 P - import java.sql.PreparedStatement;4 L; n4 J/ l, K1 L
- import java.sql.ResultSet;
0 K4 f0 o" k( R+ f6 }& |7 P - import java.sql.Connection;
! ~% w1 B- |5 ^0 R' G# ?/ u* j. M - import java.sql.SQLException;- ]9 C' ]# _9 J* k' X* K
- import java.sql.DriverManager;' u6 O& q7 F& i9 x
f m2 ^' _: p. c- /**
. V. Q0 U" G4 Y- K+ u - * 访问数据库4 T# b; W4 Y4 |! L3 |0 g5 j
- * @author Administrator
+ r& z! l# R$ t7 k# | - *
/ D, [% c& @/ i6 {' F - */
# v) P0 B- Y' W1 p0 I - public class PhoneDao {
$ O' ]5 C: F7 Z& j
; N- s0 p! N- ^8 G- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";$ Y8 Y, K0 ^" x) @& V
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";, s6 k& m/ A8 F6 x7 ^+ H1 v8 F
- private static final String DBNAME="sa";
5 _6 i1 g3 ?4 p* I% ?/ a - private static final String DBPASS="1234";
$ z9 _2 ^+ K3 E5 c/ O" i - " j/ d' h1 V5 p6 o3 `
- * c+ u# R) d2 ]2 s( t
- public Connection getConnection(){0 ?+ l0 j2 u0 J9 p7 H9 ?
- Connection con=null;
. G8 Z& m4 o/ V& N" `' T) j - try {
7 k3 W. G5 U) D. } - Class.forName(DRIVER);7 T& `6 B" ~8 e1 ~/ I" v) D
- con=DriverManager.getConnection(URL, DBNAME, DBPASS);
C: s- F6 [$ i) I$ p) A+ z5 A - } catch (ClassNotFoundException e) {+ {( o) e6 c: _! H7 d% K) r
- // TODO Auto-generated catch block1 p+ m6 t. A. x* b% Z; k
- e.printStackTrace();% I: E9 p. w7 ?( A7 n$ A' {
- } catch (SQLException e) {8 s+ J( {+ M- J& H
- // TODO Auto-generated catch block) c6 r1 V' X& q" ^8 R
- e.printStackTrace();, w; z: ^$ V) Q4 T
- }
6 G4 }+ e. F/ Z -
9 A. Z7 ?2 v* }/ X - return con;
- `$ ~- ^; k0 q/ K1 h- { -
+ [$ r$ n9 V: W$ C: v+ B9 |, J - }' e6 B$ d1 S( m" T; S
- public void closeAll(Connection con,PreparedStatement past,ResultSet rs){4 O; ?- E7 U8 O; p, l$ s
- try {
& J1 i! Y; W6 g8 J! P/ x3 l - rs.close();
: T+ X% L. _. S" U - } catch (SQLException e) {
5 {. Q, Q( G& b; M6 @ - // TODO Auto-generated catch block
! S6 \9 h' N, f3 B) O) H. b# f - e.printStackTrace();4 ]: G& k* N/ l& k
- }4 \1 Y& Y" |9 A) ?, I- h
- try {
5 d$ @! _* d1 _7 ?; M - past.close();
9 s L5 }5 H. W+ b" \. T - } catch (SQLException e) {$ @; d( e5 Q5 T( h0 ?, C# L0 Y8 I
- // TODO Auto-generated catch block
i. V) T5 w+ I0 h$ [" K# P; C - e.printStackTrace();: m$ ~2 N2 M, ~* M( d0 `% b
- }" [0 x2 h9 M% Z1 \
- try {
! E2 R, O. h& M0 a. A& f - con.close();0 e) ]! _, J g2 r# t6 j- S
- } catch (SQLException e) {6 K7 T5 u" T! M; l4 K
- // TODO Auto-generated catch block" R$ ^* t9 J$ s- D
- e.printStackTrace();
, X: i/ g# z, E W5 M( t" v Z - }
# M8 e, n. c3 L1 f, h, v - }; w6 c6 M; t2 Y( d
-
- P" i( \( D: v* u6 e# k - public int update(String sql,String[]getValues){
* {8 k2 Q1 o$ |+ s2 b: r - int i=0;
3 X* w9 V3 w2 C. { - Connection con=null;2 c- p7 r6 G$ }/ K9 j6 k' z
- PreparedStatement past=null;1 h w9 n! e N; O1 J" w; h
- con=getConnection();
1 b) ~- U- E A. [* C - try {4 N$ r9 D. f. ^4 [* U
- past=con.prepareStatement(sql);/ {: f$ \9 G4 }- [( u
- if(getValues!=null){
7 Z3 ?" `8 X! G# \ - for (int j = 0; j < getValues.length; j++) {
9 N- I6 j; P$ t6 ~ - past.setString(j+1, getValues[j]);
3 {1 |' z1 x/ a, Y& {2 [, A5 l( c" A - }/ Y! V* S0 q, e( q8 `
- }8 ?' @$ b8 H0 h) \3 K/ v8 O
- i=past.executeUpdate();9 e% _3 y. u: j# l/ i" V: N
- } catch (SQLException e) {( N5 ~9 g- y) D; {
- // TODO Auto-generated catch block6 J( o0 [6 `/ V- E B
- e.printStackTrace();
6 J! F1 x o, N1 P3 L b! |" r - } X. x9 P! D: v: d' k ~
- return i;8 T( U0 _! Z% E$ p# @
-
, W" {7 ^0 h- C: { - }
, k$ Y7 I2 Y( T+ B - }- c" H; M6 v5 t4 i1 S
复制代码- package s2.jsp.zhangxiao.dao;
2 _4 q2 M! S( w1 v5 _5 I) J5 s, D - " B& ?8 v8 K4 _; D
- import java.sql.PreparedStatement;
0 X* f$ P* w2 d S+ g+ ]- a! W: ?6 ` - import java.sql.ResultSet;
: W) u% \* n- ]2 x7 F; j; }* k - import java.sql.Connection;
$ L2 i# L7 V) d% {% W - import java.sql.SQLException;
5 H; r! b+ _8 R5 r# X" M5 r - import java.util.ArrayList;
7 @! x* s# s$ p: q- R3 ]) P9 {+ @ - import java.util.List;
/ l% P1 u P$ ^; w3 w$ e
0 s/ @8 Z2 C- a/ u: k- import s2.jsp.zhangxiao.entity.Phone; r1 ]; X' Y5 i5 u8 w
- public class PhoneImpl extends PhoneDao {4 a. l9 N* r: G* [
-
$ b3 r* ]: U" i+ u [1 j3 M - public List listAll(){; {* f1 P7 i0 u+ K% w L) t5 n
- List list=new ArrayList();
+ b$ u$ p3 {0 l& M" E+ { - Connection con=getConnection();
0 B; k% E. E8 r% j0 j: ?# g - PreparedStatement past=null;
2 w! m8 ^& E' W) u - ResultSet rs=null;
2 w! N7 V h3 U7 \, v, h - String sql="select * from phone";
; m3 g8 g1 W6 k. }( ^2 G - try {
/ S2 v- m' ]# s: I* r - past=con.prepareStatement(sql);
2 p3 v* _! e, j G3 p- Y - rs=past.executeQuery();! Z6 {# @5 K' T z& |
- while(rs.next()){+ } w0 A% Z L6 S+ J
- // id, type, brand, company, introduction7 J) o) I7 U, F. ~- o
- Phone phone=new Phone();
8 D: h( ~) L+ w# [ - phone.setId(rs.getInt("id"));; h q: ?5 r1 q( B
- phone.setType(rs.getString("type"));
* R% M, H+ Z* \' A0 U) Q - phone.setBrand(rs.getString("brand"));* l# e: m) J, n6 w1 p
- phone.setCompany(rs.getString("company"));
1 r' J$ z( E5 l - phone.setIntroduction(rs.getString("introduction"));" J k5 l" {, n. `+ V
- list.add(phone);( `! w, b9 C6 C5 y9 g
- }
c4 g- o0 X) i. v a* j/ Q - } catch (SQLException e) {6 [5 F1 M9 E) u) J) _3 D) H9 f
- // TODO Auto-generated catch block5 M. y+ c1 _% ]! v+ w- k0 @
- e.printStackTrace();
( m2 n+ O& z5 N7 S) [ - }8 }2 g' c; P N4 V" s
- return list;# i0 Q8 L+ r r9 c" A# U8 M- `
- }
% Q4 n$ O' s6 ?3 R; V - public int deleteId(int id){
8 ]/ O! s& k: r% @ - String sql="delete phone where id="+id;
+ v/ s1 b! R; N8 L5 y - return update(sql,null);
( M% E) T! |7 d& }, Y' ?" { - }
/ [" J2 V0 c% y; u' t G - }! {! A% |7 l( S" N7 {
复制代码 _; U* n" r( m7 r! O U
代码案例下载:点击下载1 J% D3 W! i% q5 H, ~
7 x$ `0 s& p8 y. E
|
|