该用户从未签到
|
- package s2.jsp.zhangxiao.dao;/ s4 w6 Y+ a3 K9 B; M
. ?) l& y! J3 R; J% J" m- import java.util.List;* L7 X' W7 W1 Q8 j$ R: _; B
- * w9 N7 y* c; h$ B( K- j% x
- import s2.jsp.zhangxiao.entity.Photo; r o9 }: W9 h0 E( e
' B, Z. f8 k9 X* b" X- /**; A4 H. q' ?' o' d) f
- * 照片信息的操作所需要的接口* L# ^' F' w, s! c- A
- * @author Administrator( v* J; W# a% F& X; J7 @) _
- *
6 c/ }' l- @) ^( F! A0 [# U! s8 ^- A - */' W& L6 J. R, ^7 F) E* j
- public interface PhotoInfo {8 F8 P( p, O0 Y# H8 c/ |
- //增加照片信息
8 Q0 P; E8 r) o; ^$ x% a8 [ - public int photoInfoAdd(Photo photo);
* _! }& L Z: A: v/ L; q - //查询所有照片信息* G% h1 N5 ?; z
- public List photoListAll();
- C$ T& D, |" h# d$ ~! }8 H8 ^ - }7 L# u D+ x3 j5 }: j5 I3 s. @
复制代码- package s2.jsp.zhangxiao.dao;
' n( I$ ~" n5 y) E- E6 b# U5 z# O8 k/ F - import java.sql.ResultSet;. X6 l0 C1 d% f* \$ f9 P6 @
- import java.sql.PreparedStatement;
( q5 V: a% H0 V6 e# W/ o2 j9 E - import java.sql.DriverManager;& W$ f' n5 A& f) T5 u
- import java.sql.SQLException;
# D: N- O+ y/ a+ s& e6 q6 M - import java.sql.Connection;
6 Z9 r" k- ]$ v9 E - % `5 F+ ~% c, K$ I5 a6 u- L& a3 H
D1 J* \2 I: v$ w/ o- /**& ~" \0 Q; K: X6 c0 _
- * 连接数据库+ [# H- l% Q0 c; }. I' m
- * @author Administrator) V( H% N6 u) \ S l* h) e' `( ]
- */ d) K% D; n4 i
- */% t! \9 Z' F# x3 |
- public class PhotoInfoBase {; X) b4 P4 m/ V+ s
- //链接数据库
& n- T4 L6 }# @ - private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
6 M" Q+ `5 _* V, @ - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=pictureDB";
! s/ t& ^- a2 R& Q: S( j3 ` - private static final String DBNAME="sa";! S4 m# `0 V" K1 g6 e1 q
- private static final String DBPASS="1234";0 h7 c8 {" S0 i- F
- /**7 m. r1 ]' N$ o/ N
- * 打开数据库连接
" N* ~8 M! I. Z- X* O - * @return3 D1 j3 X! R) R7 {! ^8 J
- */. n: u9 l2 h& {- ^/ n( e) P
- public static Connection GetConnection()
3 [' C$ {9 V: w( |* x) @) t) L - {
+ H% t& |! m! K9 ~$ q% |6 q - + ?- r) h8 [- {( t
- Connection con=null;) }8 G) }9 V! k# q7 `
- try {
; _( v6 i) m/ h5 E9 A - Class.forName(DRIVER);
2 d! W! f0 T# T- r0 Y, F2 e/ O" x# X - con=DriverManager.getConnection(URL, DBNAME, DBPASS);
3 L" G# r d, J9 r1 q# ? - } catch (ClassNotFoundException e) {. ?- k. v1 ^! c1 ]0 w, q4 F# b
- System.out.println("加载驱动出现异常");/ x2 m- L0 B: O, W3 x* \8 {
- e.printStackTrace();
' W' W. O4 Z6 z- R- ]9 s2 X E9 K - } catch (SQLException e) {
" _+ \, m- v' y, V - System.out.println("数据库连接出现异常");
5 g7 I4 Q9 G3 D: h7 Y% Y: Z - e.printStackTrace();
3 M, \; g4 l; F* n3 | - }
& j. N) s* L+ k. b) n - return con;$ e% l9 K" j0 X! d
- 3 z5 |" q. K9 \, f. e" r6 Q
- }1 O, y& Z3 q, t$ E& i4 I, a
- /**
% e5 ]9 k' K& @0 w1 y2 | - * 关闭数据库3 y- X# p6 j, S$ e$ `+ a
- * @param con/ \' W$ e. z& }+ B3 `0 ?. g
- * @param past
* X3 O5 ^- W' ?+ M - * @param res
% r' K2 q/ e, _ - */
, |4 H2 v5 M& q v4 ?" D - public static void CloseAll(Connection con,PreparedStatement past,ResultSet rs)% p7 g( h! J U- c% u( M9 N) ?
- {
5 I8 d8 R# `3 w* Z, i - if (rs!=null) {. W# g3 _% S6 ]% h
- try {
( {5 a8 R! Z5 E. _ K6 q - rs.close();
. n5 p7 A- k7 t0 Z- E$ s+ i! E - } catch (SQLException e) {
4 o. g0 }( R' m2 y; l* }' m - System.out.println("rs 关闭出现异常");# ?' N+ Q5 V1 ^2 d9 W
- e.printStackTrace();; F7 U. N; [& r/ E
- }- T' l' d. J3 L9 \
- if (past!=null) {' j3 d F1 S2 _4 h4 `) M
- try {7 ~3 Y/ {% u+ n
- past.close();
" f6 [9 n6 R( c9 S5 x" S - } catch (SQLException e) {. }! Q# c! k: q! q
- System.out.println("past 关闭出现异常");
& }/ s: Z6 H" V- a5 M) T8 X - e.printStackTrace();' s3 v: e; O/ _+ A( z& h2 E* N
- }
9 ]6 H& s$ q& {$ r& e5 _ - }, G' X3 L" m) }- [& i
- if (con!=null) {
. {- m9 c3 c4 `* D6 V- B - try {9 S$ e( E% i* h/ W
- con.close();: V9 q( q0 `$ f( {- x: ] _; J
- } catch (SQLException e) {' l1 H( b8 g: {6 a/ p" x
- System.out.println("con 关闭出现异常"); ~" Q# ^0 a; }
- e.printStackTrace();) T# [9 v3 e3 h: N7 _& J
- }' n% o! r9 y ~- N: _/ Q
- }
) a5 j% W& L, s4 B: |' R$ C* { - }
- p1 x, I6 y. q - }
7 _( u& M: }- U+ a9 j/ k8 p - /*** o$ B9 z; i, J& E9 C; e
- * 数据的增删改1 R1 n5 F6 {- K2 J8 V! m
- * @return/ v: S5 O( X3 P$ H5 ?) i2 F
- */
( g1 J4 |0 R. h ?- h7 D! ? - public static int ExecuteUpdate(String sql,String[] getValues)' H( v T1 ]( C% S9 Q$ w# H
- {
- ~* y) `& m) r P% ?3 N - int i=0;. Q, f2 ]1 P: P
- PreparedStatement past=null;4 T4 d9 E- b4 o" N
- Connection con=null;
; i! N$ A! ^0 f; a - //连接数据库% k9 i) r$ Z! o# N
- con=GetConnection();7 G, O. P6 v' h2 ~0 v* y& W
- try {
3 ]6 Z4 y4 o9 n. A( m7 u* v - past=con.prepareStatement(sql);
+ u* W. R/ @- y1 N - if (getValues!=null) {
* ?/ f: L$ Q6 r - for (int j = 0; j < getValues.length; j++) {
* k/ W' K, F9 H- T7 A7 k' {* W" E - past.setString(j+1, getValues[j]);
) a0 o$ M$ b4 E - }3 }2 o. q0 `# w h
- }$ N7 V; K% Q2 r3 B0 g
- i=past.executeUpdate();$ {) E( U8 o, I/ ^) o+ a9 r
- } catch (SQLException e) {
2 _5 o4 L0 E! e1 H. z& f6 C - System.out.println("执行增删改语句发生异常");
$ i" R% k2 K' |; a, Q( q- k& t - e.printStackTrace();
% Z7 T9 w$ j" v- X8 |/ A - }finally{
, ?' I( f" S3 b z6 J6 K - CloseAll(con,past,null);
1 b% E% P' s! w( O: H, u. H$ z* Z - }. z. Q$ U% @) o& @6 j. g' F
- return i;0 ~! U$ u b" f1 u, E
- & [1 B$ _- h7 }' |( I0 ^! e
- }- K; Q1 y& o* Y
- /**
' R, G F; S- d; A/ I9 s - * 测试数据库连接. F& y& h* ^+ e' {" I# h# P
- * @param args
5 x" |& b' T- ~# w6 F - */
4 E W. q D) [8 t7 H' _) d' } - /**) t5 a R# Y! t# } d8 h; q
- * 测试连接! B' D* z% { `5 R
- * @param args
1 G4 [) @! _7 i; O! | - */
0 R* d' d) z# ~' c - public static void main(String[] args) {
# m8 U. ` ^; B - Connection conn=GetConnection();
) q( x- Y8 J6 ?+ \3 g - if(conn!=null){
|0 X5 \0 D" A# v - System.out.println("连接成功");
; n% r( m* J# O) ^& i p6 ~& a, m - }else{
' K1 |9 R) q: b$ i - System.out.println("连接失败");
7 }/ q" o3 i0 r. | - }" A. S2 X- z8 E3 T" ?
- }
8 d% T V) f& `0 L* G - ! ]# K" m% ?: [
- }! `- J6 {3 Q6 u! T# O2 s! u
复制代码 源码下载地址: 点击下载 提取码 q( \& p% d2 R$ h E' c, W& m
7 s8 S' Y K/ W; H8 U
& X# P2 L: W2 Z. c! e1 T: y- ]
|
|