该用户从未签到
|
- package s2.jsp.zhangixao.dao;- y" n# W F: T4 C m- T
) v0 c) ?- L: T2 [" S) {, _8 b- i- import java.sql.Connection;
) E( w0 a- G$ a' W; D: @/ h - import java.sql.DriverManager;
0 r. c0 ~! P3 H. o2 R - import java.sql.PreparedStatement;
' E( D" P+ T& y k2 H/ n2 K - import java.sql.ResultSet;6 ]0 h2 ^+ Z9 b! `. x D
- import java.sql.SQLException;
, Y, W/ l$ n5 m& w. B - /**
" d5 V" p* K2 |; d% c - * 访问数据库% L" q9 W+ F5 C! M
- * @author Administrator
# V1 r8 K& X7 D# t, q - *
) i$ I- T2 q; H - */- k. m! e( L. o0 S) c
- public class MessageBase {
* k" m( s' d" V r t8 e - private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
; {0 I: {- {! W - private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=messageboard";7 A4 D5 x' I; R+ x* j; I
- private static final String DBNAME="sa";2 H0 t2 }% _# A# r
- private static final String DBPASS="";
3 g1 x# e1 Z, \, J3 \ t. { - /**
5 S; a5 S- \4 D5 r0 ~1 n2 ~' E - * 连接数据库( S; @% H/ _; T9 y( ]; V9 a \
- * @return+ @: \1 Q8 P2 H9 A6 J
- */
. o! @1 [! P: s: C- _$ y$ n' q - public static Connection getConnection(){
* E3 |, j0 ]" `/ e2 ~' n - Connection con=null;
2 ~( N2 r% E0 i ]: } - try {
* F9 O$ i9 N1 | - Class.forName(DRIVER);
! r, s0 N# Y$ n! B - con=DriverManager.getConnection(URL,DBNAME,DBPASS);) J. O. I# V) y: w5 O1 C5 j# N
- } catch (ClassNotFoundException e) {
; Y5 U( d1 Y% _8 n0 |7 u - System.out.println("加载驱动出现异常");7 z/ H2 i/ U' M0 J: P
- e.printStackTrace();
0 N- ?( }+ @, x1 ?+ c2 |+ J) S - } catch (SQLException e) {
1 \, A( _; g6 R3 ] _2 m - System.out.println("数据库连接出现异常");0 }$ i9 Z2 m/ }$ m* B+ e5 D
- e.printStackTrace();: z/ R% L, H- W0 K% k2 i
- }
6 a0 p A+ x; b; z- Y0 @5 h - return con;
4 g2 c; y3 Q$ O3 T5 c4 K - }
2 {: k4 x8 P4 d9 A1 V, K - /** B# m" y7 `* C' J7 s1 |8 I2 o f
- * 关闭数据库
7 T1 h) h' g8 Y3 d; ]& [ - * @param con
' H6 R" `9 d! g$ O - * @param past
% n. `* h( e) _- I( U, P- V - * @param rs
& s8 I/ v& `6 H8 k! l* o! N/ X' F - */
$ f% n+ d" n" O$ K" w - public static void CloseAll(Connection con,PreparedStatement past,ResultSet rs){8 _+ x$ O: c/ h5 B& _
-
$ l4 L3 B+ z, O0 | - if (rs!=null) {
/ S1 a+ C3 e z% `# I - try {8 R: t& G& |- r( i! l
- rs.close();$ V, N5 i w. M% i+ G
- } catch (SQLException e) {
* p: s% p: W1 f4 y* W& ` - System.out.println("rs 关闭出现异常");
# X$ O) _6 O" @5 ^1 p( A) q - e.printStackTrace();$ D. W* q6 S; f' _# z' Z# j8 O
- }
2 B, i# n# y$ I* A! D- C - }
5 P9 C5 J1 c8 B - if (past!=null) {" ?# _9 Y( R& z9 L+ K! M
- try {+ a, P/ L' A2 K
- past.close();4 r3 _" `; l2 m0 P1 t/ a5 ?
- } catch (SQLException e) {2 n$ d6 ?; G+ U* P a' A$ F
- System.out.println("past 关闭出现异常");" U+ m! Y) h7 H
- e.printStackTrace();
, E+ z1 B3 L! j1 t9 h* ~# M5 u - }2 T+ l f j; a3 n* p7 F
- }
% D: P' k: J0 Y- K3 _& b6 a - if (con!=null) {
" `$ G5 \# l7 r/ \# F, Q/ L8 `( X - try {
, {# S* z" C* u9 g( ^3 s - con.close();8 h& S0 M% s3 |) |$ t" u
- } catch (SQLException e) {8 Z+ |5 d; c( ^8 G. g
- System.out.println("con 关闭出现异常");
) E n6 k7 D" s. H9 y - e.printStackTrace();
& |6 {9 M' }: f+ _6 X$ Y) f - }
! p! | D3 C0 o9 Z; F - }$ z* ?; k+ Y S' a
- }- |8 Q! J* }8 e( O3 l8 X
- /*** \4 i. c& W9 f ^* @
- * 执行数据的增删改操作
( ?( D* j5 F+ d1 q: f - * @param sql; o& q+ y6 n. o7 u& y
- * @param getValues
0 z! F' O" w( a& V7 @2 c9 e - * @return9 R4 l1 {- g2 n# t6 k' ?
- */
7 ]/ ~4 K2 X% Q' W, I) w - public static int executeUpdate(String sql,String[]getValues){
& a- Z' M- n0 f9 y - int t=0;
8 V% Z- z" X1 G. Y; j# ` - PreparedStatement past=null;4 q7 w# e- L0 { P- l/ M$ m0 ~
- Connection con=null;3 X' j' z$ [) i( e
- try {
$ D* d F4 z7 x% `& A# F4 x q8 a V - con=getConnection();
0 @1 f, ?* @/ q! b - past=con.prepareStatement(sql);
* y. Z" o! o% ` - if (getValues!=null) {5 M5 z0 `8 A) t: L' X0 }
- for (int i = 0; i < getValues.length; i++) {2 \- R4 E: F, s0 ~4 y( {, v. c
- past.setString(i+1,getValues[i]);+ V4 } `8 V C R
- }! f7 J1 o+ K' G# K) J) j4 P; d6 m8 D/ [
- }
% D4 C6 I, ^- I: ~) O - t=past.executeUpdate();
6 i e- }6 R& R; v% o - } catch (SQLException e) {8 Z3 @0 C. p5 D2 B; U" X- @
- System.out.println("执行数据库操作出现异常");, W3 s! N- k, ^8 n% Q! }, C$ o# m
- e.printStackTrace();
* t$ T, E1 s1 E1 u4 C( W! p -
9 N. }( T' r2 [) d+ ~/ ~ - }finally{
( Q8 U$ m* C' J. R - MessageBase.CloseAll(con, past, null);6 q1 h# s ]/ z6 `: T, F7 l; s
- }
- A5 M( H1 B& t6 ?5 t B- { - return t;! P- n F5 S) ?) N! b; u4 V
- }4 K L1 Y" v& t% p7 J# e+ p* I: U
- /**
" Q$ X9 R* h, Z6 s3 H8 | - * 测试连接
) d8 |& n3 G7 d" _* [) o - * @param args% |# H" H1 Q# e, O5 L6 F
- */
" h" U( {' I2 G( |5 B( o! }1 u( Z! @( n - public static void main(String[] args) {
0 U' r7 f/ p3 O' I" O/ p. \! {; U - Connection conn=getConnection();. ` k8 H' |# q {
- if(conn!=null){
' }# o) x: j1 F8 W - System.out.println("连接成功");, @9 i9 l, @$ {0 F3 w# m
- }else{
0 X$ K* l( [: a) W9 \5 Q: r% a6 J7 n) W - System.out.println("连接失败"); R) {1 `" A. R
- }
6 S- ~7 C. O6 I/ w1 O1 ~7 `0 Y - }
( E" u3 o( k1 T3 ` - }
+ r- K+ K2 x+ J; X- L8 g6 s
复制代码- package s2.jsp.zhangixao.dao;; o2 Q. M P" b
- 3 E4 ^4 m; F3 p7 G
- import java.util.List;5 S; L4 R* D- T- W O, P( ~! [
: a0 d. [- O6 e' L- import s2.jsp.zhangxiao.entity.Message;: Q( ]1 T: c; G: w
- : K n8 f$ `9 {7 O% @5 j* B
- /**5 T/ Y0 f/ Z& u) S
- * 留言板接口
0 g/ t" V; b$ e( @. Q" m& p8 d - * @author Administrator, h7 T4 p3 R3 v7 N8 y0 m* Q7 J
- ** u' c8 O) k# T% ^1 n% `; Z
- */* V7 E1 u& s% Z( q2 p8 }4 g
- public interface MessageDao {9 ^; G6 U/ h0 y; n) e& ]( O9 L9 ^
- //添加留言
- Q! J0 _% S1 x; W" u6 o - public int addMessage(Message message);8 } R/ ^8 r) g! d! {
- //查询全部留言( z: g8 h( ?) Q0 W4 K( _$ }, @
- public List selcetMessage();
6 Z' r, d& A8 K8 b6 M - }, t) E8 A8 j' S4 O0 u. g8 @
复制代码 项目源码下载地址:
# |7 m+ E+ o& E+ D6 R2 }2 {" m" {" ~& e% K; T
% u2 f9 s, n" h% V( l! X0 L0 x. t, Y/ `5 c: J
( M- f( b( l. K Q5 B+ _
|
|