该用户从未签到
|
- package s2.jsp.zhangxiao.dao;
1 S, T9 ?# x5 w0 H, n' Z - import java.sql.PreparedStatement;
6 o( q% F) p1 b- c: }) i5 B/ }+ R" J - import java.sql.ResultSet;) {8 C! h# y% v: |6 a! N
- import java.sql.Connection;
$ `/ g' o K7 B+ O5 w1 v2 \+ c - import java.sql.SQLException;
/ g# o: U/ j+ `4 C$ K - import java.sql.DriverManager;
- L/ A% K- |7 h" X2 H- B4 P - ; x' B+ n a, p/ T9 s; x8 T
- /**
0 o- |+ a8 t* s$ `( x - * 访问数据库
/ }4 }+ \$ a0 X: z0 B: W - * @author Administrator
( J6 S) d7 p8 u( {) R5 Q C - *
6 i1 ^1 r1 |1 @, j) d - */. M H- i! y* x5 t. ^8 J
- public class PhoneDao {2 h% P+ H- h- ^$ L
- % K) H9 O9 {3 |9 a! G2 F7 X
- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";, l/ e# Y8 C. K3 o9 @# u* t
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";
* f. q6 z2 K- [0 A - private static final String DBNAME="sa";
; g0 V0 C+ J; o+ D) r* F X+ T' I - private static final String DBPASS="1234";
! m' t1 P& p4 j6 X9 s/ u - `. E4 [2 i4 |8 ~1 F
-
9 H {5 |; p- k) ] - public Connection getConnection(){
( e0 U- H( h7 v8 P+ [- f! } - Connection con=null;- n9 \# g7 ^0 C7 F8 W+ C, D
- try {2 K- M0 @; r( l9 S8 z
- Class.forName(DRIVER);
; k) |( l; P5 `) }9 R" \7 \ - con=DriverManager.getConnection(URL, DBNAME, DBPASS);9 j2 j* S1 M3 n6 p% Y2 |- \+ ?4 p" q
- } catch (ClassNotFoundException e) {
8 \! H5 C0 u: N1 @- C. I - // TODO Auto-generated catch block
0 @ e% k) g# I2 o - e.printStackTrace();
8 |9 ]: }0 t+ r+ M- t; T3 I3 I7 n - } catch (SQLException e) {
9 S E. y. f% E7 `8 ^ - // TODO Auto-generated catch block
- ^* A: A/ f* R - e.printStackTrace();' b0 M2 s. y8 w9 F, p& y3 p" @/ K
- }1 {3 M7 X3 u C0 d( k, g1 L4 X
-
" K Y$ w' O3 z& [" O7 h9 U - return con;
& n7 T+ O. v! L* F, R - % E w" h2 y5 I5 N& R4 L/ C2 B
- }
. ~9 Y% U+ X; _4 q8 w, W/ R - public void closeAll(Connection con,PreparedStatement past,ResultSet rs){
8 C7 K+ H9 }' g3 Y1 U - try {
2 a' C+ i, a/ j, x7 K - rs.close();1 @1 L l. Y3 q
- } catch (SQLException e) {
, \- ?& T! D, R9 v# W/ d - // TODO Auto-generated catch block
5 ]) p0 S- \* |* ?7 V6 r - e.printStackTrace();' q" U# x5 \' }: R2 ?. G/ O# J- P
- }- I, X2 d8 B, L1 z% x: F
- try {
( D/ U7 Y+ e7 Z" q) e; D - past.close();
: ?& t/ x" D8 U% R- ~ - } catch (SQLException e) {
5 Q7 E9 |! R# X5 W: U+ R - // TODO Auto-generated catch block1 w4 F; g4 A0 ~9 T* Q3 E
- e.printStackTrace();: y; o+ f$ P4 }; Q+ g1 L& T
- }
# h1 G& y' C& B# P7 ]& V - try {2 z4 g0 c4 O/ A
- con.close();+ }- Z- i! `) ?$ z \# q
- } catch (SQLException e) {
% ]3 }* U+ n4 ]5 ]) M7 V - // TODO Auto-generated catch block
X2 \) L5 U Q- @/ }; R P# ? - e.printStackTrace();
8 _: L( j" ^8 ~; a. q0 d" a1 S - }9 _6 _ F1 `# U4 Y
- }
3 t5 n8 D. q: u/ t -
0 ^3 }; n# Z5 ?* s1 n - public int update(String sql,String[]getValues){
, }! K# |: u, N# ~ - int i=0;
; m* b. V0 A6 r( g1 ?+ x - Connection con=null;
$ i; T" A- U, O& a - PreparedStatement past=null;
. M5 [2 {3 @& U, o9 Y - con=getConnection();
' m8 F# I: S" {; ] - try {
2 \/ r1 A( ?1 w - past=con.prepareStatement(sql);
3 g P* B% Z* P- f - if(getValues!=null){0 [8 ]; d, h% I& F1 O. c
- for (int j = 0; j < getValues.length; j++) {1 p" {7 Q" H) z* q
- past.setString(j+1, getValues[j]);
2 t7 F, |% n) j* X# ]1 X - }# V c( |8 i) T5 h
- }
+ \0 g Y9 h( _! ] - i=past.executeUpdate();2 ~& k" g% ?5 ?2 W X0 A
- } catch (SQLException e) {$ h0 p9 M; i2 |& f
- // TODO Auto-generated catch block
, J1 z' @2 _- k5 _6 B# M - e.printStackTrace();
) z+ i* e4 I3 ]$ p2 W - }
: P3 B& W. \1 @( g# n$ e - return i;
' W: h: S( U/ w" c+ D% f6 @ - 3 j. d% D+ P% C5 O( j
- }
/ [; j8 {- p3 P - }8 L1 F6 y( |# {: S0 c4 v
复制代码- package s2.jsp.zhangxiao.dao;" O8 i. s+ s; t) k- O
7 c1 {! D: A# v& D) C- import java.sql.PreparedStatement;
; i) F L' l/ u - import java.sql.ResultSet;, Q0 I- w$ L8 l1 i$ |! O
- import java.sql.Connection;9 Y7 J* [) G/ k/ Q4 ~1 m$ d3 J" i
- import java.sql.SQLException;3 H q) g6 a n
- import java.util.ArrayList;; F- x7 e. F) K; [
- import java.util.List;: ~! U8 k r I
- / ~7 K3 G0 I/ D8 `+ ~; ~4 S s( p
- import s2.jsp.zhangxiao.entity.Phone;7 k0 f8 ^# R$ ?! q0 `
- public class PhoneImpl extends PhoneDao {
! H( L, H& `% J -
. q5 e5 S: i6 w) |7 b - public List listAll(){ B" V- a$ S* F0 O% J5 ~
- List list=new ArrayList();
- y, z+ F* ]/ w, | - Connection con=getConnection();& O+ k; G: \% M4 n; d7 h
- PreparedStatement past=null;
/ y5 G d$ P2 _9 _ - ResultSet rs=null;; F. H" {2 U+ k& w. \, M
- String sql="select * from phone";
+ u5 Y. }+ U6 t' `- r - try {; }* b1 P2 U- @
- past=con.prepareStatement(sql);# f+ T g+ ?! k" M
- rs=past.executeQuery();
+ e @, J, a+ K. K% t; C - while(rs.next()){4 e/ j2 K* I4 C8 a5 |: `$ `
- // id, type, brand, company, introduction
/ Z% N4 U, S- ~3 I* ~& _' o - Phone phone=new Phone();
8 D2 f& e% ~4 o- M - phone.setId(rs.getInt("id"));
0 z4 R; T) ?7 g0 i+ z3 r - phone.setType(rs.getString("type"));$ J' v9 }8 Q% ^2 o
- phone.setBrand(rs.getString("brand"));. }; V Z: X( P+ A/ v
- phone.setCompany(rs.getString("company"));
9 C0 B/ s) Y" [* y- K - phone.setIntroduction(rs.getString("introduction"));
8 f) }/ h. ?9 \" Z- h - list.add(phone);
, M' i6 S( }% `! I% k6 p, z - }# U9 V$ |! e* [/ d0 F
- } catch (SQLException e) {% W E5 M l0 ?
- // TODO Auto-generated catch block
. W: }- I/ w+ f1 T% |4 N3 g. z# _ - e.printStackTrace();1 g, u! o i' d% B
- }+ @7 @: Q, ~& ^' b3 X( v
- return list;
% u! d D) |5 F F( A- V% N* b; u - }) R: L9 D9 a% ]4 T* \& N1 h2 n
- public int deleteId(int id){
+ [3 `+ E, o( I! c$ M( V* e - String sql="delete phone where id="+id;: R' _! p" v2 |+ x8 g' }0 a
- return update(sql,null);
0 M0 @1 C5 E: `7 T - }: b$ ?9 |0 Z' P! S# I" \
- }
6 k- \4 P2 b6 x* u/ `7 w
复制代码
9 d1 a* \; ~0 o! L4 x. X" X代码案例下载:点击下载
1 |0 x8 e3 e) d, y" q5 t% y
6 ~3 ]) e( a; K" R7 L3 ` |
|