1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 - import java.sql.*;1 M3 g4 Y* n7 |- M) H' K
-
5 g$ _ X; Z# y - public class Access2Database{
T0 o/ \# d3 Z n' t0 M0 Y9 d5 n - public Connection getConn(){' y( N2 `4 Y! ]/ ^' t3 |0 U
- Connection conn=null;7 \: Z% W2 V, i9 L
- try{
6 }, o8 w$ x2 ^6 l5 Q" [& Z - Class.forName("com.mysql.jdbc.Driver");( ]6 b4 R j/ W7 U
- String url="jdbc:mysql://localhost:3306/mytest";2 U' X0 C7 @% |
- String user="root";1 \# ?# M' d$ ]; A& Q$ \8 o1 x
- String password="111";& p* _2 P* }/ H, I
- conn=DriverManager.getConnection(url, user, password);
+ M" [. z/ r" W3 A - if(conn!=null){" ^" n6 ]% g) F. j5 O
- System.out.println("The connection to database is successful!");
x2 e2 h& V# q9 X3 c6 M - }5 V& W9 k5 L; ]+ j
- }catch(Exception e){; u2 F! x( n4 ]7 K) K' b, e) W1 N
- e.printStackTrace();
* @3 q+ V/ ~+ V - }
" g9 I! W* E( Z: G# d: d/ ? - return conn;. Q A! \5 W$ _, D; _9 v. H
- }* o; H R/ c% v- l% M' H! |
-
# T5 G4 J8 a0 h5 o/ E$ s5 R" d4 M* f X - public ResultSet getResultSet(Statement stam,String sql){ R8 Z. u: F z3 r
- ResultSet res=null;
/ ]2 a Q' ~/ N7 c" A& V! u6 j2 F - try {
7 ]4 p, S" P( C6 D( e" H' j - res=stam.executeQuery(sql);( q0 E _# B# \. `/ k$ O+ l6 r
- } catch (SQLException e){
0 z o- ~ `9 j - e.printStackTrace();! g: [6 e, |' I. D. G0 v: x
- }, \; s9 L) {1 a( C
- return res;
; x3 i4 P* ?' m$ x - }1 @+ o1 T/ @' q
- void showResultSet(ResultSet res){}
9 X2 i ]$ ^/ ]8 e: p1 b - }
# H/ W1 w+ k6 M/ Y% ?! k. Z - import java.sql.*;0 H4 i8 v9 s4 \- |
- ! W& F' g: p: T/ v9 ]/ k
- public class GetConnection{$ l/ E% e0 N& p( Z
- public static void main(String[] args){
( w7 ?$ v" b0 `9 @ Z - Access2Database adb=new Access2Database();! ^9 D6 `0 \9 `+ U8 t$ K' N }' e. @% {
- Connection conn=adb.getConn();+ H0 B4 J; p! F2 O" a" ^
- Statement stam=null;
8 Z! x/ i# D; t( e& i9 ~ - try {& ^) Y" |: q+ M5 p4 R$ z
- stam = conn.createStatement();
# M3 W2 w; P- J; T - } catch (SQLException e1) {
, p2 K. _2 |$ b0 w z" P - e1.printStackTrace();: G1 G+ |6 N& r; D
- }/ ]! S: q2 N! A# f* h
- 5 {/ k* D q6 f" ]( Z
- //show resultset: h( W( f; U ~
- String sql="select * from student;";; i" x. i. @ g' T. Y( A
- ResultSet res=adb.getResultSet(stam, sql);" V' G! T# J! ~+ H( |
- try {
U0 ~8 U+ U: j8 N+ t N# e3 r& [ - System.out.println("name\tmajor\tscore"); \& }5 V T$ J/ t% s! z7 Z
- while(res.next()){9 }/ d: W1 y, \) H% n0 Q
- String name,major;
/ R; E1 W) n* D8 i( n( I+ ] - int score;
h+ b- e( @8 n5 k( v# T - name=res.getString(1);
- s2 K) {( \) [4 W1 g. L& G - major=res.getString(2);& `9 h+ s9 ^8 s+ n% C `, H
- score=res.getInt(3);
+ A" s9 C& Z+ F$ S+ Q - System.out.println(name+"\t"+major+"\t"+score);+ _6 o' B+ y9 b k0 \5 o
- }- y6 Q3 p8 l" q$ |) R
- } catch (SQLException e) {$ U( S0 L5 t2 W9 u/ J( f& Q# x3 Z/ A
- e.printStackTrace();7 Z! M, @% U$ X" [
- }
/ }+ |4 J# `; L3 W( l - try{
2 k. n' {% m5 M& h2 _ R, a - res.close();3 G. W. ?, v! `( g. V/ l2 m+ D
- }catch(SQLException e){
" W; y1 f7 m4 P* m6 I! c - e.printStackTrace();/ E) G' x1 c2 h3 U; ]2 t9 q
- }
6 }9 x- C- c* Z X' b. V4 A4 \5 w - 9 A/ P& @' l; {! Y( V, C
- //insert something into table
7 V3 `; C/ Q. y K - sql="insert into student(name,major,score) values('f','Chinese','70');";
0 C# l0 d% L) R. Y9 t+ ` - try {1 L: L* F0 U9 T6 q ^3 \
- stam.execute(sql);
( ^6 F. c. _0 e+ [+ x5 ~ - } catch (SQLException e) {% L( y0 S/ c2 A3 }( B5 i0 [: m
- e.printStackTrace();% q8 t1 l& k- |; t) t5 L2 y3 U
- }- M& Q+ _+ Z! s& G9 T
- ' Q# x7 g x( W
- //delete something from the table
# i9 r! y* t. P- S8 p0 b% r - sql="delete from student where name='f';";, t* g) {6 F5 {* c O& q
- try{
. y" i; p! s: ^/ b: b, L. a - stam.executeUpdate(sql);
9 ~: _) H4 y1 k# o0 d2 @ - }catch(SQLException e){
) M4 {8 O* R) E' q, n: r - e.printStackTrace();. q- W$ A! a; J w" M
- }
' S: n/ X4 {( I( _ - $ G% r" C7 p# w2 `5 i
- //change the data int the table
% x6 S" W& a. l$ w - sql="update student set score=100 where name='a' and major='Chinese'";
* U4 p- P! K; u+ b3 V) M - try{
6 M/ e5 x6 I2 ^- k8 n7 K - stam.executeUpdate(sql);
( i' W+ M& ]$ p3 ?; L/ Q: T& I - }catch(SQLException e){* [* A" G, c, Y
- e.printStackTrace();
! r: ^7 f u# q$ p' a) X8 u - }
- y# X! z5 O2 `& S" O( m - 0 D% f! y2 D! _6 I
- //prepared statement
8 ]2 o; w' H8 Y+ p0 t; Z' O - sql="select * from student where name=?";3 ]. K' ^/ i9 b2 I* e
- PreparedStatement pstam=null;
3 Z9 T( { K9 I( { - try {
' p, I6 E3 z5 f% h0 I - pstam=conn.prepareStatement(sql);
2 u" f) G5 `6 K7 a - pstam.setString(1, "a");
$ H |! k* \# u4 R - res=pstam.executeQuery();0 v7 `% }* U: J8 H' k! F5 y
- System.out.println("**********************");
6 m6 I4 G) |6 U$ e4 R3 k - while(res.next()){
* |8 p" Z5 r2 E. X) W# l- U - String name,major;
: p# O0 |1 ~: a - int score; n2 t' j) k9 h$ E9 C/ ?3 f
- name=res.getString(1);
2 T% q) U+ Q0 n0 ~3 G- g - major=res.getString(2);# G9 U8 `' g& q+ }
- score=res.getInt(3);- a" \7 n- ~) M- z! ^
- System.out.println(name+"\t"+major+"\t"+score);; x S8 I+ e0 q+ B, S1 ~! t
- }
! M5 g3 w/ P; A# U! j& H) I* W - } catch (SQLException e) {
% h/ K. g2 i0 u2 ]% O) O$ u8 _. e1 H - e.printStackTrace();
2 S$ s2 T4 p7 {) r+ J6 ` o - }. r2 d, K" S$ s2 o. O" j- b
- 1 ~* u5 f" E s; n: V7 N/ h
- //release the resource of the program& ^7 Z$ W/ s: y& C3 U5 D1 l* l1 I6 `
- try{% m) X. ~) {. _1 R* J, p5 t" w, c( |
- res.close();7 _% I" C! _8 q" E
- pstam.close();
; B% g% J. ^4 o1 F- \ - stam.close();* n- c4 Z3 I. D
- conn.close();
- V+ S* P8 {7 W. @$ C - }catch(SQLException e){
* ?' ^1 ~! z% S* {0 ^0 p' Q - e.printStackTrace();* N2 v! y6 i7 B. J2 I
- }8 P$ n0 Z y- _) X
- }$ D% l7 s' t" g8 a: B& v9 a& {
- }
复制代码
3 t; K, m0 q- ?" z, u- P1 B5 T$ W U/ J$ l
. J. @: \, v+ c+ c% l |