1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 - import java.sql.*;
. f$ [' b) N% w0 ]6 q% j# ?8 | -
9 J8 g/ P. W* @6 c% c - public class Access2Database{
6 h8 R. y+ Q4 Q! ?/ J - public Connection getConn(){3 W3 y W b: c+ I2 Z) U& i
- Connection conn=null;( |, I J6 U" K& l b5 Z( U
- try{
7 ^" {4 n4 n3 l e: W - Class.forName("com.mysql.jdbc.Driver");
: V8 s8 F5 Z8 ]8 { D5 I - String url="jdbc:mysql://localhost:3306/mytest";1 a) H1 }+ e' I7 o3 ^
- String user="root";
- A8 U" u* D/ O! ]- {0 E - String password="111";
I& e* ]7 W, |9 L) x - conn=DriverManager.getConnection(url, user, password);) }' Z# _8 o& E! M) b2 s) M4 T
- if(conn!=null){' S. V9 x/ _5 L& s" B6 L
- System.out.println("The connection to database is successful!");
& @. y% m2 ^5 D) V) D# u# ?: E" p - }
" V# H! v4 z! _7 X - }catch(Exception e){7 g! u6 Q- T: }
- e.printStackTrace();
0 |. q* }) V3 B* U$ b0 _ - }5 Q0 W O& ]' ]/ J3 I
- return conn;
: u/ D3 }4 i" s& }) T$ F - }5 z% M0 T7 w9 h0 K1 U4 b; n$ g& V
- _4 X1 o$ E4 X: t* ]6 q
- public ResultSet getResultSet(Statement stam,String sql){
: i( u1 g: E. `/ g ] - ResultSet res=null;) D/ m) Q" X" E
- try {$ b9 w1 y2 V( V4 q
- res=stam.executeQuery(sql);
( N' b1 j/ U& e8 V. v - } catch (SQLException e){2 D. D3 S5 V! d3 H" B% q
- e.printStackTrace();
( w1 Z0 v$ A' F8 v( d) h - }
! o3 E8 {/ I0 T( o7 {# N" b' V0 a - return res;6 J8 c! ?& t; d! G- C" n0 ]
- }
+ E+ P7 r- |# a. r6 h+ a - void showResultSet(ResultSet res){}
6 h& |$ R" X0 U6 v& h7 n - }) K p' U% d# z; \
- import java.sql.*;
; ]- J9 n% P: z5 H, \ -
; j: p: C. E% N1 r' ~ d: Q - public class GetConnection{
( Z' E9 I6 E1 e( n5 n1 o - public static void main(String[] args){5 Z8 @! J# l) K. ~. a. \
- Access2Database adb=new Access2Database();
* S8 [; y# \$ B' D) y - Connection conn=adb.getConn();$ u* T: D5 v7 y
- Statement stam=null;
- A0 ^0 G0 ?5 e7 y8 w' J4 _ - try {# o2 V# S1 r1 P, m6 r
- stam = conn.createStatement();; g+ H; U; p5 \# C# J! ]. [0 w, w
- } catch (SQLException e1) {* _9 T" W* j& l# F
- e1.printStackTrace();1 W/ R! B" f0 S6 t# H% }) O
- }7 q1 |" W% q/ P: x$ k+ B6 _5 r( t
- 6 p$ E0 i; G9 U, c$ W3 f6 L3 L% S
- //show resultset
2 ?/ f; L7 ~- y4 I4 ~ - String sql="select * from student;";5 p/ b2 r4 U% m/ H7 z8 m
- ResultSet res=adb.getResultSet(stam, sql);
0 M$ j, d8 }* c" S# Y - try {1 n; J) ]$ p( M' v
- System.out.println("name\tmajor\tscore");
: h( D0 m- R. o - while(res.next()){
% Y$ a3 {; c& {! H: C - String name,major;
: d6 A6 c- f9 _( @0 x% ]! { - int score;
+ H! H2 k; B( O/ U n, d$ d - name=res.getString(1);
6 f! ?' B, t+ B1 s4 u - major=res.getString(2);8 d4 @1 D/ W# t
- score=res.getInt(3);
1 ~8 R9 l _ u - System.out.println(name+"\t"+major+"\t"+score);
' N8 d! }! X7 Y* T' f' K4 p1 G - }
4 V/ Y6 n' u: P. B- t7 @/ k* R+ ]& t - } catch (SQLException e) {
% U6 `$ b) h' J; D' {. w! }! i - e.printStackTrace();
% f8 ~0 q, r7 m1 d C - }
/ [+ o4 \8 H) a: Q7 c/ N0 } - try{" v) D. ]4 X) U F' M2 S/ e& s
- res.close();
% M5 r+ P- U# {# v! t - }catch(SQLException e){
3 D6 j* w X! ?' t' _# E - e.printStackTrace();& F, O; l& j1 n+ s' F0 m
- }
* g) X* M8 J* c2 X5 K0 u - : }$ ]/ M8 N' t) `1 k
- //insert something into table
. X7 V: I2 N) n; m: z - sql="insert into student(name,major,score) values('f','Chinese','70');";
/ R$ `' N9 c/ E/ p2 g/ ?4 X8 k - try {) h, `! N- y+ g; o! e
- stam.execute(sql);" x; ?2 b c; o2 \
- } catch (SQLException e) {/ D, h( S P, p& s: \' O: b0 x* A
- e.printStackTrace();
8 p4 k/ t3 e' p - }1 W" W, E9 \: Y1 L& ~" ~
- F/ K* N( w) R& Z9 T
- //delete something from the table
5 T3 P# B/ w o1 _4 I p - sql="delete from student where name='f';";
3 m/ B+ ?3 @/ Z5 `% g0 O - try{# a8 U0 T7 B* F! U' t% P) V- o
- stam.executeUpdate(sql);
) b" k% R! ?' p9 { - }catch(SQLException e){" W9 H9 }6 Z2 d. n% @
- e.printStackTrace();* m5 v+ Y; F1 p+ a8 L/ [
- }0 b' [3 V% ?- T
-
8 `( g2 ]9 A# y - //change the data int the table0 T, g1 L, u2 Y
- sql="update student set score=100 where name='a' and major='Chinese'";$ Z( c$ k" r O3 X- Z
- try{' J+ H1 I9 w6 U
- stam.executeUpdate(sql);. r( z4 G" }! }/ c/ O4 @) A
- }catch(SQLException e){
! d4 k% f" k5 p* _ - e.printStackTrace();
! D5 T/ y! ~. ~- `: j% ^) \1 J - } Q6 u8 W3 e& h. U. a2 ]
- $ o: ]( ^! g7 z$ k/ ^- |6 T
- //prepared statement, ] o: k8 K* N1 n: N
- sql="select * from student where name=?";
8 F* r' @7 j' ?/ b" _0 E - PreparedStatement pstam=null;# n6 C: E' t" W7 m' M& o
- try {
$ i) p8 q& A- T, K* b* C$ r - pstam=conn.prepareStatement(sql);9 I& ^9 F9 a! r! l+ a* a! p
- pstam.setString(1, "a");- N: \9 R7 R; U9 T
- res=pstam.executeQuery();
& U. i' g3 L1 b& g5 ?; P6 x4 b" E - System.out.println("**********************");4 P* d" \) J4 j6 ?+ a- y9 I8 ]
- while(res.next()){' g( k6 r/ W6 l
- String name,major;
0 Q x9 {2 Y# N2 R; I - int score;$ Z. v L' E) U, O/ ]
- name=res.getString(1);
9 h! ^3 l$ j z9 R2 `) a8 X - major=res.getString(2);
8 s: j3 N! o, D# E4 G7 d - score=res.getInt(3);6 S+ c: ]5 w1 S2 l% X) }4 _
- System.out.println(name+"\t"+major+"\t"+score);% _! T$ `- Y, q; z" A
- }8 |8 S- x& b6 M! T8 r v! }( O
- } catch (SQLException e) {
+ P. ^# X7 i9 R7 d, S - e.printStackTrace();/ a6 ~6 Z: L& b% I
- }
5 P/ [! p9 |# O) N7 y4 h: | - 2 i( l, D; P$ o g
- //release the resource of the program0 {5 q' R; U+ [- J$ f
- try{
0 `/ T9 k. h+ R% m - res.close();
: G5 E- u8 d0 c6 ~7 s y - pstam.close();+ v% `) ~$ H( ~$ x
- stam.close();
2 ^/ U+ y! q% X+ S! y# B$ E5 T - conn.close();8 N" I7 _& q, }+ r/ E/ n+ [2 n
- }catch(SQLException e){+ a* n5 M' L0 c7 h9 I6 T
- e.printStackTrace();
* O" @9 K8 `) \% c% B/ p H" g7 { - }- ~5 }3 L4 p) R2 n" h) x5 u7 t
- }' [0 X2 p- S5 g
- }
复制代码 ' V6 E, x2 j+ A- L
. Z- D# j2 L# J9 P6 _1 S, O5 d
" V$ r6 h2 \2 I6 }1 @. i
|