该用户从未签到
|
- package s2.jsp.zhangixao.dao;, }& M5 V/ D5 p) n
3 f; |( m) v6 `- import java.sql.Connection;
9 ]3 a6 S/ \; R6 T% {/ m) h - import java.sql.DriverManager;
8 R' L* G E8 ^' y5 ^1 @; k: J8 { - import java.sql.PreparedStatement;
: W9 y8 T1 k- x( B8 M$ c - import java.sql.ResultSet;
$ U B5 S- R0 }& P @7 H8 G) ` - import java.sql.SQLException;
' a8 t; v& y# \$ s0 z" n$ C7 q. L - /**: [/ s6 L. [$ ~
- * 访问数据库
8 Y) ?! o1 L* L C* I( K0 t" l - * @author Administrator' j9 _# R, g, ^9 P1 A8 { G
- *
" g! {! e9 R$ {) N9 ? - */5 b" [7 t7 n2 V ^9 f
- public class MessageBase {
0 h! L& R6 n# @" } - private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";9 D1 e( O: p, t
- private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=messageboard";
7 D: F$ R5 s4 g( U' j' f - private static final String DBNAME="sa";1 b0 d5 S+ T! i* z- }% ~
- private static final String DBPASS="";
! ]5 d, @$ A, o1 E - /**
0 s7 b" C7 V- Y7 u, R - * 连接数据库
, A. ]5 b+ T4 m, f% H) y" `/ u - * @return L; g+ V _$ y# ?# f/ g5 _
- */' K1 @0 @& T" f
- public static Connection getConnection(){
# }" D) U7 q2 z" z* b2 m; C7 T - Connection con=null;
, t( b2 _8 _. |. k - try {
8 B' m& I, p+ ?: o0 n - Class.forName(DRIVER);
6 B/ N [6 d* H8 L7 v( U* R8 a: G - con=DriverManager.getConnection(URL,DBNAME,DBPASS);
' o$ z) v1 ^3 v# z+ W C, h - } catch (ClassNotFoundException e) {* L6 t3 n" s7 K/ C; w, J" }
- System.out.println("加载驱动出现异常");, ^( ]) K9 v4 `: @/ F7 f
- e.printStackTrace();
2 o4 k5 k0 g% D9 Z K9 R! } - } catch (SQLException e) {
" A1 Y/ @! M& V1 ]$ D. B$ x) \ - System.out.println("数据库连接出现异常");# S. _( o+ L+ g
- e.printStackTrace();( O7 V7 t) Y2 h1 T" w; T) a
- }: b, f; E7 i$ o8 S5 y
- return con;" ~8 ~/ ` S( z1 Q
- }
5 K! W" W; ^) T: I - /**
: Y$ s3 g q2 l$ U# j7 y - * 关闭数据库" v9 Z/ H# R) F, x0 |. j7 s
- * @param con2 }8 ?1 A' n5 r, S
- * @param past, X/ |, f% i0 Q% T
- * @param rs/ F4 e6 r8 d( J4 z
- */
$ g1 \# w9 A1 X- T) g! b# ] - public static void CloseAll(Connection con,PreparedStatement past,ResultSet rs){0 Q0 ~3 G6 d3 _' H s% i
- i5 R/ Y, u: _$ \
- if (rs!=null) {
p) b5 ^7 [, o) K/ H2 ]1 _2 M1 Y - try {7 L1 _ s% k7 M G7 D t
- rs.close();" g3 f A9 o: x
- } catch (SQLException e) {1 L8 y2 X' I- q( t# @0 C
- System.out.println("rs 关闭出现异常");* b# A" s7 ]# E' G! K
- e.printStackTrace();
; s& W, E/ ^% l: t - }; h9 i( F: ?2 t* t$ i! r2 Z
- }. @1 _& ~2 N9 Q, V
- if (past!=null) {5 m0 G5 k, U4 k/ B e- t
- try {
$ }8 A' v8 @" [! K - past.close();
# q w) a/ h- ?4 z# w( ]4 a- }& V! v( g - } catch (SQLException e) {
' |: M6 P2 O v6 B" ^6 Q, W2 z - System.out.println("past 关闭出现异常");. g' }* I8 J$ z
- e.printStackTrace();
}3 D2 P/ ]4 R8 Q - }
+ o. _; o: p* Z; {; z - }
6 F* E6 A/ m6 q5 B4 x: F - if (con!=null) {
- S1 { n, u/ o2 E' m2 ` - try {9 E6 Q: {9 a- M, B$ i9 C
- con.close();
& Y9 `! ~1 z8 {5 H* Y - } catch (SQLException e) {& s" ]( T3 W; q3 ^ ^7 ^
- System.out.println("con 关闭出现异常");
: \' `% o4 P! e# n - e.printStackTrace();
9 W# H9 A- u2 z$ [1 w - }! u) R& Q' G: n' c/ I
- }6 }4 Y9 j) l! c. e) I8 ?' A
- }; v( m, B8 y- [2 K( I- o
- /**
0 \1 t e+ } P5 M; H3 o - * 执行数据的增删改操作
0 E( K+ J) I, {6 }; r - * @param sql! I1 V, x1 C, S/ W6 w6 i9 _9 S
- * @param getValues
# i* }$ X# n" b, X - * @return' u) |7 p4 U# x
- */" j" c' y o' u3 Y U9 d/ Y3 I4 o
- public static int executeUpdate(String sql,String[]getValues){9 {& W5 D' q+ I W& A, ^5 E+ ~
- int t=0;
, c- }$ C) w2 ]) a - PreparedStatement past=null;, K. w- ?6 x1 _' \; v3 r
- Connection con=null;
, R0 R6 ^% K& y1 X- w* ^) R - try {
7 _' y h V5 L* V - con=getConnection();
1 V1 C! j, L x2 L7 X - past=con.prepareStatement(sql);
& n6 t/ y( Z$ v) L" P - if (getValues!=null) {# F8 ^! ]1 u# B$ S5 n) G3 C
- for (int i = 0; i < getValues.length; i++) {; X7 @8 P9 s1 p( F) Q7 O
- past.setString(i+1,getValues[i]);
( J0 f4 X8 C3 Y" A% E3 v: [! T - }
. i5 F2 K% q% G" Y$ F! ` - }
) H+ w9 }. ?6 O3 p - t=past.executeUpdate();; M) ^; W8 s) G. f' l% Y! R
- } catch (SQLException e) {; g, E/ t6 y& [& }( k& e- D
- System.out.println("执行数据库操作出现异常");6 G3 D6 L, K3 W1 V
- e.printStackTrace();, k0 j7 H/ v4 T
- 5 [1 E7 q0 t& ^9 r
- }finally{5 o9 {7 P( o: `9 o: q" n
- MessageBase.CloseAll(con, past, null);
! p* ]8 J& M4 q' y. V7 e9 ~3 Q; z - }
8 U' \/ f3 S, E - return t;
& K; v8 I ~1 i - }
: v* I3 x0 o+ H% I: x n; e - /**# H( F2 P5 A$ N8 e
- * 测试连接
' j9 B. E: B* C' j9 Q: B. `1 c+ C* Z/ V% U - * @param args; V. f+ `0 d$ u) S3 ]3 ~
- */
' W( F( ` t$ X( C( c7 @( } - public static void main(String[] args) {
4 G* J6 M a8 L - Connection conn=getConnection();; P. v- y# m0 J: R+ h
- if(conn!=null){" e" m' y8 W' y$ E: c9 V
- System.out.println("连接成功");
' `) V+ ?0 f3 [5 q - }else{
4 ?. O6 H) t% n - System.out.println("连接失败");4 v: ^9 Y! D1 o) N
- }2 Q; x0 C( }1 G# n5 q ?- I
- }+ k4 ~$ E/ E+ N4 i
- }7 @' ~" W" Z) N5 b$ f( H
复制代码- package s2.jsp.zhangixao.dao;
. C( b) D1 c' G" G1 m0 W9 c - / }4 w' k1 |* k# i
- import java.util.List;% _* ]1 Y5 E9 K9 L6 I
' G; `- {: S) Q+ u- import s2.jsp.zhangxiao.entity.Message;
" h5 k8 I, V8 c& E9 G
- K; M T! v6 o% o! B1 ]- /**" B1 Q; H- ^8 n( x- O! Q- M
- * 留言板接口8 ]" G% I0 z, e5 h$ H9 q
- * @author Administrator1 V2 `8 U6 W$ h
- *
8 ^+ |8 r; y% \" g - */; j3 w5 N7 s5 a6 J8 ?1 L
- public interface MessageDao {% M, K1 H$ K* }. Y
- //添加留言
( Z# x" F: O( f! \: G" n! ~2 C - public int addMessage(Message message);* o9 v; f( e6 Z6 h
- //查询全部留言( Q) ~" A% O* Q0 m, t' y$ G
- public List selcetMessage();& F! R+ Y6 o0 U6 K! O0 [) k6 c
- }4 q: D" T" p3 E3 U1 |
复制代码 项目源码下载地址: - s6 a* A& Y1 D3 @2 n9 v5 i
& f* C$ P6 B x8 w @ A: P. f3 x: o" f# e) M1 K1 B" A2 P
, u' I6 N' z& h' g- i
6 u& _& q2 ~8 T) I6 R, i/ _ |
|