该用户从未签到
|
- package s2.jsp.zhangxiao.dao;- Y3 z3 B f1 W) @) Y- ^
- import java.sql.PreparedStatement;8 v. k1 i; b7 |5 [$ Z6 E
- import java.sql.ResultSet;
- x3 J# g: p. c+ `! w7 K0 b# R( n - import java.sql.Connection;, k p( y4 q# E
- import java.sql.SQLException;1 k8 m7 n. w" P( h8 P/ _
- import java.sql.DriverManager;
. j" A, L- X1 a8 V: k
$ m! Y0 G3 @/ \: T% E+ L3 p' R0 p- /**
9 t0 y0 H/ s3 q( w - * 访问数据库
1 D$ |( W0 N# p - * @author Administrator
7 Y' q9 w& `+ e+ e( _/ F - *( i5 v# |. s& z& Z$ @& s2 D4 i6 Y
- */4 j% @: m( R% H0 M. W
- public class PhoneDao {
+ Y* [" V5 B0 `4 A9 _+ l; A
1 j- {; q$ B, U- }- P- private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";$ |* K$ C9 k4 y0 I
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=phone";( b; r3 k3 m' `" t
- private static final String DBNAME="sa";5 s( @! d3 V' e( v
- private static final String DBPASS="1234";
' L3 t( F* p4 p0 I7 }( C - 8 L: U& R8 J) H/ v# |* m3 {
- 0 n4 G& v- H+ c- p
- public Connection getConnection(){8 z5 g/ a; u9 H7 W& |* b
- Connection con=null;, e( l Q. P+ c- P- M$ f0 I
- try {
( [. D! G' d! }, t0 H - Class.forName(DRIVER);
# [2 \5 ^! C8 V' P - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
: P2 | X8 ]+ D2 A) H9 L - } catch (ClassNotFoundException e) {
) i0 n% P8 o( y7 n0 Q0 r6 x - // TODO Auto-generated catch block0 _# A: h" C: T2 [/ Y m
- e.printStackTrace();
5 R" }) ?: d$ C2 S, R. W - } catch (SQLException e) {3 ]. O- w& h( A4 |, b' o; k
- // TODO Auto-generated catch block% J3 U5 {( R# h4 a L
- e.printStackTrace();
2 N {$ b4 J5 b6 j, s- V - }' }% E; v" F& A% [: c$ A
- 5 B" @- Q, q- v% n U
- return con;. z5 l1 |; P3 c" \
- 1 a1 g" v6 @- U
- }3 g7 N+ u0 [+ O! c7 i# \+ [
- public void closeAll(Connection con,PreparedStatement past,ResultSet rs){
. A6 P7 M' P2 R6 ^# c - try {
, |8 j. e7 ?6 S - rs.close();
1 K0 J, `1 W1 y7 | - } catch (SQLException e) {0 W9 g7 {! y0 M0 K. B
- // TODO Auto-generated catch block( I$ w9 W/ T& _- s' o b+ S
- e.printStackTrace();
( r) Q6 G2 B) U" x& _! w7 O: q - }
3 ]; v. m$ U, k) y* p - try {
5 ?. y) T3 k8 S) L% n - past.close();4 d# I" M0 c6 p5 q' `- f; W
- } catch (SQLException e) { |5 `8 i5 q+ \* G) d
- // TODO Auto-generated catch block4 |# x( u8 p, f# d) V" b
- e.printStackTrace();
% U; s5 _9 N: H0 j - }% k6 b0 W7 D0 T# x! F1 u
- try {
% ^- G. G A* }/ {& I7 Y. @ - con.close();
0 o. a5 F3 U! v/ z& O5 g - } catch (SQLException e) {
; K$ w( k3 I" p8 p - // TODO Auto-generated catch block
: p5 B9 [$ W# c6 X1 @/ F - e.printStackTrace();
2 |$ T: Q x0 Y - }
$ }4 l; }; N: { - }
6 h' w; M, b+ M0 D k& @3 d - 7 {7 q( L2 O( y; z: H
- public int update(String sql,String[]getValues){
' E3 e" r& L9 @6 q - int i=0;% M! S1 P% v+ }- f: m
- Connection con=null;
$ [' `7 E- a6 K8 G: r2 v - PreparedStatement past=null;
9 d# ]/ s% J8 ~: M - con=getConnection();
' {/ ~! R; @+ ~8 h - try {5 _1 o- Z: P z
- past=con.prepareStatement(sql);
7 \1 X7 T, A; B! \5 P+ ~+ ~/ B; u0 f* s/ D - if(getValues!=null){4 u6 D/ V6 F) _$ F9 P; n
- for (int j = 0; j < getValues.length; j++) {
& i7 W6 _- L$ {! L - past.setString(j+1, getValues[j]);' k1 C) F8 L! J8 P1 w
- }8 ^# U" X: N' l
- }8 n' u o9 z. d3 z# i3 n6 s
- i=past.executeUpdate();7 y/ s0 r9 W3 F) m: G1 H6 I5 D
- } catch (SQLException e) {
7 \( @. `! o; G6 G0 N4 G0 s - // TODO Auto-generated catch block
$ e0 G( Z; r7 I* N4 m3 Y) y - e.printStackTrace();
+ w" i8 I5 f& ` - }
) H& V7 R: J* E# E! E+ m. x9 q - return i;( l n* B. l4 }" M
- 8 e3 k( F6 |' H
- }
) |3 q* T3 k0 g& U$ _: F' b+ ~) q - }; W. }- o9 b1 `8 {* U; r' T* t3 m; B
复制代码- package s2.jsp.zhangxiao.dao;& [* Q% \; F- |1 r) e
2 y0 M+ U" M( F- import java.sql.PreparedStatement;7 Q3 i# |6 |: L4 y0 S' v8 j/ \
- import java.sql.ResultSet;
) W( S% ]- `5 Y5 h) Y4 i' A/ G- b - import java.sql.Connection;
- z) y! s: ?' o) C3 z - import java.sql.SQLException;
& F& E4 ]$ r' S7 q, X/ h( I - import java.util.ArrayList;3 {( r8 [+ K, Y- O s" h4 G
- import java.util.List;
# F0 d# v) A; a& U. J: C( Q - ! M( O) }8 u }# e/ l( Z
- import s2.jsp.zhangxiao.entity.Phone;
) ]. U9 o* s, z0 r ] - public class PhoneImpl extends PhoneDao {
" h D' R( I5 r6 N# N3 I -
+ I9 c7 m2 L( Q1 d4 q - public List listAll(){
8 V" A5 `7 u" D! a. x - List list=new ArrayList();% f$ z4 m( {8 f" g* i
- Connection con=getConnection();+ Z8 o- N8 i2 ^/ _+ b: s
- PreparedStatement past=null;
7 h/ L( c/ X- @, D/ k2 s0 S - ResultSet rs=null;- O/ o3 x& P5 h
- String sql="select * from phone";, `7 y$ k! c/ y+ m9 S$ k
- try {
+ J2 }# u* g% C* s+ P - past=con.prepareStatement(sql);$ w1 W( y# y. M+ j4 G9 J
- rs=past.executeQuery();
3 M1 m: C- P! j+ M - while(rs.next()){2 m9 P( E" o! e7 A! ]' r0 H, ~
- // id, type, brand, company, introduction
* f8 a' k8 {" c0 V& ` - Phone phone=new Phone();$ o: L2 c; Z0 Z& b j& m
- phone.setId(rs.getInt("id"));
7 E! f$ K3 `( Q- b - phone.setType(rs.getString("type"));8 {% r& a% F2 U5 D) A+ S0 Q0 g% |
- phone.setBrand(rs.getString("brand"));
6 b6 ]# b4 j, S. ~3 [ - phone.setCompany(rs.getString("company"));* j1 L+ O: l) g8 z+ Y: ^8 G
- phone.setIntroduction(rs.getString("introduction"));
/ `) z- `0 j. y; ` - list.add(phone);
$ m! I* n+ @! R) S6 a7 R& U - }
$ z4 } P1 ^" k# w+ V3 m* p% V8 Y, C - } catch (SQLException e) {
( X6 G8 j: I2 x+ E8 | - // TODO Auto-generated catch block/ z0 }2 F/ ~4 i: ^( R" o( g
- e.printStackTrace();
1 V7 O" c# T& i" k2 V9 M' U - }/ m. y, I: P) U: w ~
- return list;
6 I) W- @ u6 u+ M - }
7 B, Z5 D3 }0 H - public int deleteId(int id){
- H" d$ M( ?2 p$ ^ - String sql="delete phone where id="+id;# I E% j, Q: `( f0 `0 v
- return update(sql,null);
: K# y4 z- s: a - }
8 p7 Y* N( |, a9 z - }
% _, O X$ M9 s6 @
复制代码 - f3 c$ W$ X! P6 ]/ I
代码案例下载:点击下载3 S' E& n1 [& ?4 l0 x
4 ]$ W8 O8 b- g' B; @$ q B |
|