该用户从未签到
|
- package s2.jsp.zhangxiao.dao;+ ]# B4 | }, I
3 o! @2 s; C4 E) ?" j; h- import java.util.List;
6 I- W) N' {; z! c r! ` - / r m: }3 `& E' Y5 h* {0 e
- import s2.jsp.zhangxiao.entity.Photo;% [1 ^0 q* `8 s; N" t: }" M# r3 V. O
- - G! m8 A" Y/ x
- /**' o1 H( ?. [4 [+ F: o" i6 D2 e) m# T/ r
- * 照片信息的操作所需要的接口
4 m. `0 C3 }; H - * @author Administrator, U$ \9 j$ [& x2 q% K
- *5 ~8 l) L% I% b+ a
- */
4 j. r& Y/ v) T$ T ?* s6 Q* y - public interface PhotoInfo {
2 n4 l5 `- w' d ~+ r2 M9 q. \ - //增加照片信息5 L) d1 B( @/ c4 b' A4 v2 G' [
- public int photoInfoAdd(Photo photo);
" k) ]1 A' i4 ] - //查询所有照片信息
3 E, u; H! \" F9 m- c6 H9 g - public List photoListAll();$ e7 T' b6 L3 ?
- }: w. [/ `. _% w) Q' n! z
复制代码- package s2.jsp.zhangxiao.dao;4 C5 G i! N7 C6 G
- import java.sql.ResultSet;
" A% }& e5 e' W5 X* c% H - import java.sql.PreparedStatement;3 s; T' U2 ~2 B& C- A3 f
- import java.sql.DriverManager;
! q$ x+ z5 x& n8 l/ E - import java.sql.SQLException;
. W, L! h3 m t0 L0 F - import java.sql.Connection;
" W) }; A# v4 j- w, I! J! I+ p! S
# G0 h9 |- r; k4 _/ ]1 E6 `
/ p2 d- v! \2 D8 A- /**( E$ g8 I$ M# b1 ?6 {, H7 |1 S
- * 连接数据库* A+ {8 k5 R/ \' i/ \4 W
- * @author Administrator
! W' T+ S& g( N4 _" l, j - *
. g/ \! _- n/ ] - */
( _: n1 q7 _: O/ M# r - public class PhotoInfoBase {
2 B1 `/ Y! q7 X- C* J" r/ R% @ - //链接数据库
0 E2 T2 R6 \8 N9 g+ V" H - private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
# T9 W7 R% l) q* t. [ - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=pictureDB";
6 k9 G! K! R5 ?' ^5 O8 } - private static final String DBNAME="sa";
5 o% r1 j9 ?# b4 U - private static final String DBPASS="1234";
. l5 ~5 H2 t9 ~! p - /**
5 u8 h: ^) }6 L - * 打开数据库连接
{- Z* I5 d' i' X3 b - * @return0 Q0 w \' t; U- l0 e' X; S S
- */
. s' @- t2 G! K( E2 A - public static Connection GetConnection()! ` J/ H1 b3 r# U |9 X5 g0 z
- {- G" O7 [, R# Q8 b2 N
- $ V8 i$ m2 b: x7 f$ _4 i. V' A
- Connection con=null; @# F8 f0 r$ i4 @; P5 N
- try {
. V. \/ [9 j, @+ m - Class.forName(DRIVER);
4 \+ D, G% A5 H1 d - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
8 R" k* V* Z6 i4 S. p/ p; z1 y6 | - } catch (ClassNotFoundException e) {
8 m; r' l: d, u* q - System.out.println("加载驱动出现异常");' k# C$ b( m: E' N- V$ `0 [
- e.printStackTrace();
% L( x& |/ I: K, `2 T" C - } catch (SQLException e) {
9 r. S, K3 f8 E' X+ {& x; \ - System.out.println("数据库连接出现异常");- Y3 ]2 r( n3 `# ~
- e.printStackTrace();
' a- U/ z1 d+ l1 L4 y1 `7 k# k% E - }
6 q; X3 @% ?( T2 D1 [ - return con;
( N* e) a% I6 Y4 t+ I4 t) B - 3 g, W, w- n8 w0 a
- }
; x1 r8 F1 Q6 I% q - /**
) `* {* {8 v6 R - * 关闭数据库
: s2 [7 F, V' w - * @param con
9 P% U% d7 m$ R3 ^9 J - * @param past% L1 ^6 H; n% H- p0 k6 n+ u
- * @param res
; T& ~+ m5 p5 j4 h7 y - */
0 E& V! M" C1 X& e& |1 q7 G" m - public static void CloseAll(Connection con,PreparedStatement past,ResultSet rs)8 b( Q& i' T v/ X
- {
' [0 W! @% w+ a" X# O - if (rs!=null) {' @6 Y6 q. v3 E6 G
- try {
$ W; ?" u$ M1 ]9 u' K3 x+ e) F' U - rs.close();
$ v( D) S" u& |+ ]8 {& K, T - } catch (SQLException e) {
/ _, ~! r( s, m: R& Z6 A0 Q {& O - System.out.println("rs 关闭出现异常");; g1 B+ L7 V2 N6 k' \
- e.printStackTrace();
/ B# @5 Q) ?, l4 K; D - }3 _0 `5 U8 ]2 a3 S
- if (past!=null) { x: V; I8 V+ L1 o" E0 p
- try { |1 B+ W, o( s' k1 X
- past.close();: C' Q' D3 y* D# \+ U
- } catch (SQLException e) {( _7 C# b7 k$ C2 p) o& _* ^
- System.out.println("past 关闭出现异常");
- d( I2 k: T3 d$ ]1 _ - e.printStackTrace();
# h$ e$ u& l5 x+ @& n$ @ - }
0 h8 r0 r1 V; C. J - }
' L- A4 ~4 p* ~6 q+ o( |& Y - if (con!=null) {
* H& k6 z( ~" ^0 U - try {8 D# T. V+ X/ Q7 V
- con.close();4 |. `4 e) {1 L6 _2 t' c
- } catch (SQLException e) {" v) n" P4 R' M4 O" c0 e6 i: |
- System.out.println("con 关闭出现异常");) E* K4 b9 ]% ] M
- e.printStackTrace();' N5 V; s& ]7 l. ~! e
- }
?; W* M- O* O - }
6 b" m1 b' E ^8 F$ j! l: X( _ - }
& |' d* J& a1 j( P - }
4 `) v7 ?$ L6 ?! w: x4 _% V2 Z - /**
9 I/ S; G6 Z; a5 S, W% T - * 数据的增删改
. E {3 [9 d6 {' U/ F) ~; F - * @return' I5 ?, j3 X S
- */; M7 D9 I+ A9 f; h* E
- public static int ExecuteUpdate(String sql,String[] getValues), z+ |/ r. N! x9 `: A: U
- {& D! b H3 Z+ ^
- int i=0;
0 Z8 B7 D( C5 n0 K/ a( m& k - PreparedStatement past=null;
7 s* D5 m+ q2 W7 z - Connection con=null;/ j% C9 ]9 N, K/ ^/ j
- //连接数据库
+ T6 Y& M, ?# R7 t7 H2 B! r# E - con=GetConnection();+ x" P' G0 a- i# b; J' A
- try {! i0 [. h+ L: D) G
- past=con.prepareStatement(sql);
0 h0 M0 q, j) m! H0 O7 U; h1 U - if (getValues!=null) {
- r; z- I( V2 C# Q5 m - for (int j = 0; j < getValues.length; j++) {
3 q/ o8 i3 X# x - past.setString(j+1, getValues[j]);9 ?: }* r; X4 O" h( H9 R
- }
, j- F7 ^) B% [. @* p' R$ O7 S ~ - }( B3 T3 u6 S0 l$ E- n' ^! O
- i=past.executeUpdate();9 z/ P E! O9 k
- } catch (SQLException e) {5 _& K1 w5 W/ _+ x1 ]8 V, f! U4 v
- System.out.println("执行增删改语句发生异常");
# v7 J: U+ m1 Z - e.printStackTrace();) _6 i$ n# c) i& v( N6 r- W
- }finally{
4 B9 u# M r, G4 U6 ^! E - CloseAll(con,past,null);
3 [2 X$ E% d4 b% ~' K - }# S0 W/ P y0 w. E
- return i;% l/ O3 K' j6 C4 W
-
8 ]$ a" h3 t: Z! \& S - }- Y! a* j! C4 Y6 d: R4 j! ]
- /**, C3 n& Y/ A3 r* N2 A
- * 测试数据库连接, M/ |0 ~2 G$ l, Q
- * @param args) F7 D/ m4 w$ i! P
- */9 I( e/ L) X2 e; |9 w- r* f
- /**
- H+ l z8 y6 k1 r+ { d+ N - * 测试连接, C" a, A5 M$ z( w/ {
- * @param args
2 ^0 T) j" r/ _+ d6 I& r4 T6 Q7 S - */
& s% k& X3 \! C- c e2 o) j% q1 y - public static void main(String[] args) {7 R5 o1 E# n0 e2 b* L' a, ~' V
- Connection conn=GetConnection();
# I6 N! I; ~) |- _& B8 x - if(conn!=null){
/ s0 [; `7 N0 e5 S - System.out.println("连接成功");; h+ l' x i1 G) q/ P/ v
- }else{7 W( f9 h9 I( {9 f' V# N
- System.out.println("连接失败");9 B3 a. M- T2 R/ z6 b
- }- I' w2 v/ [, ^3 O9 _0 }) S! ^
- }
3 x; s( j! L( q5 a -
3 V; g; F6 [/ P/ V" m5 c2 I - } \0 P* e8 P! C, }6 m6 {% }; t
复制代码 源码下载地址: 点击下载 提取码 ! ]- E3 N" l# \
' f' d1 h+ z) ^+ e1 _
9 O0 W1 M6 {: E1 s2 ~7 ]
|
|