1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 - import java.sql.*;( H) J" w) q) E# ]: y1 ?
- " s* l8 |2 P% v& C6 C1 m' I. z6 v
- public class Access2Database{5 T1 E2 v5 R9 T9 D" u) D
- public Connection getConn(){
% V4 U1 R' z( Q; Z - Connection conn=null;' u8 }' K5 C( R! @6 L4 }" e
- try{: n8 W o& v$ U
- Class.forName("com.mysql.jdbc.Driver");
2 v+ ^- H) k# o& {, Q - String url="jdbc:mysql://localhost:3306/mytest";
}1 H! i# N( z. J - String user="root";
! ?& U4 ]/ e2 O) \ - String password="111";
6 H5 D( C3 O" r7 A( ^2 M - conn=DriverManager.getConnection(url, user, password);$ v3 d4 O8 j8 F& F# a5 D3 H
- if(conn!=null){+ |7 |: i% F) o8 o+ x- [
- System.out.println("The connection to database is successful!");9 ]6 C. z. n6 J" h9 J- V6 b
- }
~8 b* o$ m2 X2 v5 F - }catch(Exception e){7 q) }2 q* t+ g$ D$ n2 H }
- e.printStackTrace();) Y! x" L) \. w# s
- }) n9 g- P. K0 I, ~7 q) ~
- return conn;5 o) ]# M9 D* p
- } V8 @* A, V; N2 P8 n
-
0 R+ S* @) ]1 k7 E& n) H - public ResultSet getResultSet(Statement stam,String sql){1 t/ V# f0 s0 G! g7 M
- ResultSet res=null;' S" W6 t) F! _, K. `+ Y: Q& @4 |
- try {( I1 S. L, \' m6 ~3 ^' C+ E- g
- res=stam.executeQuery(sql);
% K* u' h1 b% x3 p, G - } catch (SQLException e){9 S: B: r7 O2 ~! V9 A; P/ b- c
- e.printStackTrace();
7 X! U& H% y# w0 _; a! a - }
! S$ ~) U" J4 l0 C' l' P8 Q0 ~ - return res;
% E0 Z b' ^. |& ]0 t - }
# v5 o- J* t9 |9 e4 U - void showResultSet(ResultSet res){}7 X! K! _, D; `5 n; I
- }
* ^7 [# x8 [# {' Y. e - import java.sql.*;9 k( b1 e, S9 ~
- * h5 o. A: h2 `% e' B- L' [5 j
- public class GetConnection{
7 n8 p+ Q* j2 V7 D; ?! t - public static void main(String[] args){
2 A; P2 m* X. j# }) _ - Access2Database adb=new Access2Database();' T9 p$ z4 y! O2 B' F5 ?) r) H- T: s
- Connection conn=adb.getConn();
, b' J5 g- F1 S" V6 d- k - Statement stam=null;
. D% n( j, E5 `' \% f) Y8 M - try {
7 v" ?- {2 ~/ d* x4 r - stam = conn.createStatement();& F- c8 S; @' w! h
- } catch (SQLException e1) {) }. ^; K! |0 D0 L! d# d7 {
- e1.printStackTrace();+ ^ E: U- V; M/ d8 h& x
- }0 `! g' _" f$ o( w6 @
- ; V& R5 d* x* h B1 T# b: b. }
- //show resultset
% ~' x$ q3 Y6 b# Z - String sql="select * from student;";
# j. ^& N- l! b2 C! u+ y - ResultSet res=adb.getResultSet(stam, sql);
4 s% S6 E& }9 a9 e& m A - try {
# N+ l: @# C0 ~, ]0 Z; R- ? - System.out.println("name\tmajor\tscore");
% \: @7 ^: [$ N - while(res.next()){
9 P! y+ @: M% `$ B7 W) r - String name,major;: P5 R2 ~3 }3 v& r, I5 ?4 o
- int score;
' c5 f- F9 N- B - name=res.getString(1);0 u5 b- W0 m" h' M) w+ k d
- major=res.getString(2);
- M. ]. n3 ~: C2 Z, t - score=res.getInt(3);
, @* {7 N' I# K k8 d( x b( f - System.out.println(name+"\t"+major+"\t"+score);
( L. n8 d5 g4 J4 J, p& p& |$ }: H - }
4 s7 Q" D9 f; M% G+ j# x - } catch (SQLException e) {8 f; ^1 Q0 M' Y4 `: w" B
- e.printStackTrace();) x3 |! T8 l5 O: o2 \
- }" z$ p% i6 R8 z5 x
- try{8 {$ U: }: |" r; a$ R* T
- res.close();: u- w: B- ]* F* a
- }catch(SQLException e){/ o& e% A; x! z, ]" b
- e.printStackTrace();
: J. y, ?- b. Z4 t# A; M- i - }' s1 h% b+ {' q6 @
-
_2 o+ K. [* h4 r2 O4 u: O - //insert something into table% r Y1 \2 X" T; p' O
- sql="insert into student(name,major,score) values('f','Chinese','70');";
6 D4 S% n% [- _ - try {) g2 Z0 [0 {4 s" n6 B J
- stam.execute(sql);
$ Y7 K4 e% Q' b4 c7 M" ^' I2 K - } catch (SQLException e) {
+ c/ a* f) n# ~# U - e.printStackTrace(); @ g0 Z& S- l( G! }" [
- }; a( P0 j4 m: j
- $ O! j" p9 m- b. h
- //delete something from the table
+ Y$ H: ?3 E7 z6 F - sql="delete from student where name='f';";
# L, L4 s# E7 |- _9 `8 ] - try{' \5 ]5 `$ x \- k* Q" @: D4 C2 G
- stam.executeUpdate(sql);
4 V) p+ s4 q; q' I. P, Y - }catch(SQLException e){
# w' m" a% z# D - e.printStackTrace();: Z, u; I) Y7 b% G% F5 l
- }
2 C/ q) z# x0 W: I -
: W5 l) F9 N4 i& f" u! D - //change the data int the table; u# t& o4 x' n, U4 y0 u
- sql="update student set score=100 where name='a' and major='Chinese'";: j3 f; o2 N" B/ [- z
- try{
: b, P' M6 I m - stam.executeUpdate(sql);
* T) h' E# o2 `2 h* F: s# _0 y! B s - }catch(SQLException e){
, S$ F. e' J6 g- }& p - e.printStackTrace();0 E9 S) b: U& T" q0 s
- }/ Z8 Z/ _, L' m' S/ N. o4 J/ e
- 3 W! Z/ L6 Y1 @" m2 P2 c! E
- //prepared statement8 w$ `- U1 n1 ?6 v/ I {
- sql="select * from student where name=?";8 V- A. Z5 |, P( P+ y% s
- PreparedStatement pstam=null;
& M) I) o0 c) O: x; H7 } - try {
& E5 Q% M d) b' r9 D( q - pstam=conn.prepareStatement(sql);
5 H( m' T1 ~& X4 d - pstam.setString(1, "a");
7 I; Z+ ]0 J- H7 g8 J, y. | - res=pstam.executeQuery();
' @) N W- `+ `# q- p - System.out.println("**********************");
3 N. [* K1 v# S" U- u - while(res.next()){1 B4 e8 p! ]) p
- String name,major;
- {: k6 e9 E5 U - int score;
5 D! E3 w! Q/ J - name=res.getString(1);
. f+ Q# i5 s( k1 L - major=res.getString(2);
9 s a; y! \+ _2 ? `% y - score=res.getInt(3);9 m, K4 |9 V! t J
- System.out.println(name+"\t"+major+"\t"+score);
7 l/ x; s7 G* H5 [1 c/ b4 m0 D& ?+ a - }& v6 |4 x% p7 ]% A9 c& m
- } catch (SQLException e) {
8 k, a$ x0 b3 c& @6 c! t - e.printStackTrace();8 f) D% B7 N+ L/ U( F# L
- }
) z( |0 l) @+ q# N: T. n -
4 {) P3 D9 ^$ j: k6 y7 ~ - //release the resource of the program! i" M* Q3 Z' @* x
- try{
2 a( H; P ]' ? _* o" ]$ ^9 F - res.close();) R/ M+ b" J- r5 |7 P0 l
- pstam.close();8 V! G# I4 s0 Z9 }; I
- stam.close();
- c! G8 S% S4 k! \! N6 R. b2 [ - conn.close();
' N' N0 p/ } T/ [( A+ ` - }catch(SQLException e){- W+ R! M! A4 A; N
- e.printStackTrace();( u2 t3 }. l5 |5 u5 [' f# Q& T7 ?
- }8 Z9 f$ M" e7 e- k; a! a* `( O9 d
- }
# \% ^6 n7 x" ~5 L4 ~3 L6 t0 ?- I - }
复制代码 : R- f; ]4 `! X
7 T8 t" J9 b9 k4 r& K) @ r
* X7 U8 q0 ]; m4 L5 _, A. v
|