1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 - import java.sql.*;
2 ^6 S- E+ D$ l -
4 J) K- ^1 \; }! v2 g$ D! a - public class Access2Database{
9 j- g) D$ o5 K/ S* ? - public Connection getConn(){# g5 ~# k% A4 ~- m
- Connection conn=null;
9 o2 K- J' |8 | - try{+ _3 {* j& }: U$ b8 h
- Class.forName("com.mysql.jdbc.Driver");* _+ m# a# M e
- String url="jdbc:mysql://localhost:3306/mytest";
& l8 U& Y: y' f4 M3 ? - String user="root";" L. u# G* V: t3 {; h8 P# t
- String password="111";
, U( V9 p. \, g3 P- D8 x - conn=DriverManager.getConnection(url, user, password);
/ w5 y% w z: h: D - if(conn!=null){8 l6 Y$ g! ^5 D7 z5 S' u
- System.out.println("The connection to database is successful!");- \* d# M6 i7 m% k. }
- } _( @! H1 s/ g) Q1 V! G
- }catch(Exception e){4 N$ ]" [& h# t
- e.printStackTrace();, l& W4 @$ {1 d; K6 c
- }
5 R8 x, u9 ~: V/ j% J' o6 T; q - return conn;
) l0 _# f: K# `! |0 ^. V6 g - }- r, B% p& V6 k! Z( z
-
+ A" V. o9 H( o - public ResultSet getResultSet(Statement stam,String sql){0 a6 U/ b7 G! r4 S' c4 @" p7 e0 l, ^$ Y
- ResultSet res=null;
" @- Y- [% y8 p* @+ |' B) X( ~! j/ L - try {
8 F. M# P6 c5 o% p. r) i - res=stam.executeQuery(sql);3 h: X' R8 k2 c* c" H% d3 k
- } catch (SQLException e){
4 m$ D* N0 X+ v - e.printStackTrace();: \7 k; p4 |' r6 t
- }
4 K) V I& t# ~. G; a: r9 s ? - return res;
" a q2 y5 h" ^+ q+ R; s* H2 g; u - }
7 @: y( L3 h& W% A$ n+ C - void showResultSet(ResultSet res){}2 F2 R+ _9 b1 X0 f
- }
0 f4 o b3 R) D; K- ?. ^ - import java.sql.*;
$ P, ]9 H6 ]; O - 5 U6 T8 H' W$ A9 b) |& d8 y
- public class GetConnection{ J% H, {1 \& e8 i
- public static void main(String[] args){1 Z2 K% g9 y) F0 v
- Access2Database adb=new Access2Database();8 E3 S5 r, Y" R* J" S; B
- Connection conn=adb.getConn();
7 w& `8 ]& H! H! h" X - Statement stam=null;5 V- I8 Y# U* z" S; [2 C& N8 k
- try {; Y1 W# ?. {& u- x( i
- stam = conn.createStatement();" _& R# p/ W4 Y7 [+ u
- } catch (SQLException e1) {
% `" y5 v: i. \7 `; [" s3 D - e1.printStackTrace();+ g3 z+ |) i H9 e, B. x! |
- }& B: p% z: q) `% k/ Y, m, b6 C
- ) w2 w5 i( N1 O" Z& c
- //show resultset- W/ W6 y" w$ @3 x* P1 B3 m
- String sql="select * from student;";
( {( x, F* B% l x' ^! F - ResultSet res=adb.getResultSet(stam, sql);! @# \# g- O* J2 a/ t& d4 q
- try {/ I3 `2 R, P+ W2 R% \5 ]
- System.out.println("name\tmajor\tscore");
, |" k# ]. [. Z( w - while(res.next()){! f: P' ^) A( Y2 `" m% Z
- String name,major;
3 V! ]) a: ?- ~3 N4 o3 ?0 x - int score;; @1 j6 y& Z7 f2 v' j
- name=res.getString(1);
' ~# N8 y7 r7 X, o/ z T8 u - major=res.getString(2);
5 ]. z' w1 O# G; B) B1 {/ Q - score=res.getInt(3);! f; u, J1 O! {! Z8 g4 S _8 P
- System.out.println(name+"\t"+major+"\t"+score);
' L/ n; w. j; K - }: ?- q8 m/ U8 g- ?
- } catch (SQLException e) {
/ C$ A8 q& N2 [9 Q/ ^6 l5 u% M - e.printStackTrace();
, @9 [4 k8 [4 j3 O* @ - }
& {6 r% d- D. A& G3 w. l - try{
/ g, c5 ~, f6 G7 j( v2 { - res.close();
; k+ k% u& G' D& z - }catch(SQLException e){
1 v" |+ o2 f0 y' k5 T - e.printStackTrace();
8 u O0 ?1 D* ?# K - }6 y- G/ Y' T1 B4 u( F2 _
-
; L% h) ?! v% D - //insert something into table
# X8 m+ x- u ]/ Y* ] h1 D - sql="insert into student(name,major,score) values('f','Chinese','70');";) R5 o( |2 n' {. e3 [
- try {
; O# ?1 S2 x9 M1 W) J. @4 G w. k- U- | - stam.execute(sql);. K! u9 y2 K1 k; c0 A
- } catch (SQLException e) {9 z8 s4 f- M: K2 |1 b! s/ F6 J- N8 O
- e.printStackTrace();4 D$ @6 I% |. R+ I; u' o- n4 C
- }1 i5 {1 b# o) w! B
-
* g1 D% o: C! k, y/ Y. |- | - //delete something from the table
- p, o' o, @ p- ]2 H6 s - sql="delete from student where name='f';";0 S# S0 k; K7 j( d
- try{7 _8 B$ Q) a/ c; } I
- stam.executeUpdate(sql);
2 L9 z' R! ^" F; F0 D9 r6 x1 \0 t - }catch(SQLException e){
/ |: m! }% i6 y. l% f3 V - e.printStackTrace();3 f0 z d0 {& c
- }) g5 b% e1 Z0 y1 P# ~* E( `
- 8 W* ], T! q3 ?# ]: z& D
- //change the data int the table( ?: O0 v; g' f% N5 W" F. q
- sql="update student set score=100 where name='a' and major='Chinese'";
" x f) M* M) ~2 g9 }( a, L - try{
; L+ p; Y& u+ \2 Z7 G6 U - stam.executeUpdate(sql);
1 O+ y' Y( a* q9 u+ h' I - }catch(SQLException e){
1 j# N/ E" b' u! n - e.printStackTrace();& W4 P* M; K5 X: g. t/ ^
- }
: e! N& W: h' [7 S y: ^* Q3 Z - " Z0 A& S+ s+ P/ s C. Y3 m
- //prepared statement3 m# R6 j: L1 R, l2 E
- sql="select * from student where name=?";
% b. b* r. @. w; N% v% E - PreparedStatement pstam=null;) I! {8 ~8 f4 h7 v* m, i
- try {
* V8 O) {- t6 X - pstam=conn.prepareStatement(sql);
& e! D& c3 m( h" D! ^: [. ^+ u+ } - pstam.setString(1, "a");
+ M! _/ x$ O4 a$ y9 | U$ X( b6 @ - res=pstam.executeQuery();
$ ]' R; H8 P0 k& ], I$ E1 N3 B - System.out.println("**********************");* @: K' ]7 `3 P* T( G. i, K
- while(res.next()){8 G5 d# z4 W0 B7 [* `/ s3 T
- String name,major;
4 _! @0 n" J% _. T/ Z( f. r - int score;% o. U! x1 U+ E% {" Y! k) l
- name=res.getString(1);
% A+ a4 J; j6 t0 Z6 y& Y9 Y) x6 { - major=res.getString(2);
9 ~8 ` Y; h, ?) g - score=res.getInt(3);. k1 U2 m1 }! E; ]" Z9 k! W8 a ?) v
- System.out.println(name+"\t"+major+"\t"+score);3 C& E2 p5 F. Z8 D' j2 \. a' S% \0 W
- }
% R8 |7 R. J1 X F% q3 M% F( N, `" n+ e - } catch (SQLException e) {
* V) n) a. r) F: ^& l9 i - e.printStackTrace();; R V: ?0 ]6 V9 b0 g% v0 j: ?. K
- }
: x7 i9 g+ t% r! m - , ]6 q% Q3 o7 S# ~
- //release the resource of the program+ p3 [# R N8 ^% O
- try{7 K& b3 M+ ~ A# P) z) ]4 x& K
- res.close();* P4 S. d4 a9 K) ?0 h% M& i8 B4 Q
- pstam.close();
8 o! }4 g% E( ^ - stam.close();
9 N! L% E# B. n7 |4 b. B - conn.close();/ n7 B& U v8 U$ A) E2 _5 E" {
- }catch(SQLException e){1 q U2 t) B. k1 r. v
- e.printStackTrace();+ \" `. D# C# r% K
- }3 E1 y# P5 \7 G% t" T
- }* p( d9 U3 c4 p1 j
- }
复制代码
: R) q5 \6 z9 p+ h1 U: V1 [# P b) c6 o& }% t
' A! S' L# F' r) ~& I' W) [: y |