1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 - import java.sql.*;
7 ]' I; _6 b0 X) i' n9 E# G" N# M, O - 4 S1 H: {/ L: ?9 m3 m# a8 c9 ?( s/ Y
- public class Access2Database{
" y: w9 f7 k2 x+ L2 s. `" v - public Connection getConn(){
1 m4 a. g0 r/ Q, O - Connection conn=null;
1 Y) v: d8 s J$ @& A M - try{8 w1 y2 B5 k7 k( Y- ]$ }' K8 U
- Class.forName("com.mysql.jdbc.Driver");
) i5 X6 f ~6 ^* T - String url="jdbc:mysql://localhost:3306/mytest";) d' X* U8 T4 O0 X) }
- String user="root";) l% ~0 c2 R* q& m( y; G" G" J
- String password="111";1 \, E8 @3 g2 u
- conn=DriverManager.getConnection(url, user, password);
; `1 w$ M N R! e - if(conn!=null){
& ?. P5 X1 V9 ~4 k4 a C* r; s - System.out.println("The connection to database is successful!");* L, |* a! L( ~' z
- }4 D$ k) M r3 ?! t$ g) K$ P6 e: z
- }catch(Exception e){7 ^9 g: i- }- C3 `4 s( a0 V5 d2 r. P* z
- e.printStackTrace();
( U/ G) m5 } o - }
- A Z3 Y/ |% \: o - return conn;
* c: |/ @7 n3 _' N) b. O6 ` - }
& g$ F# p* i( A# ?; ]& W) j& s6 A B - 0 B* Q' E. @$ P% v! [! z
- public ResultSet getResultSet(Statement stam,String sql){
% M$ W, T" M! n6 U8 @, l - ResultSet res=null;8 v) Z8 q5 p- Q: l3 U- v6 \
- try {" ^1 d1 D( ]1 T$ a4 @
- res=stam.executeQuery(sql);
1 y: u, s5 o. W0 ~% Z: H - } catch (SQLException e){2 V6 {" z3 Z- O) z
- e.printStackTrace();
6 s7 z1 R/ Z2 x5 {5 M7 o" {1 j% H* G - }
U5 E3 G, |( ]* o( {2 k" o - return res;
$ }& e2 |3 `; m: W - }0 X7 D; j# l! ?8 [) p( M6 w3 M
- void showResultSet(ResultSet res){}1 Z$ Y7 s9 L- o: }& D
- }
7 X$ e5 x) a6 X - import java.sql.*;' e. q& k4 D1 {/ Q
-
+ @* K* B5 q r! a v3 L1 g - public class GetConnection{6 l2 b; u: |& t' e
- public static void main(String[] args){: n) F* _5 m6 X) G+ J4 j
- Access2Database adb=new Access2Database();
0 N: b a8 F; x' l - Connection conn=adb.getConn();
+ N& B9 S* T2 a) v6 c) v - Statement stam=null;
6 q7 q0 s! _% A: c6 [$ h" ~9 h - try {# {% o' d8 S, ^; W
- stam = conn.createStatement();0 v J( s; B% D0 W, E& w( q3 Q ]+ Y
- } catch (SQLException e1) {( y7 w4 \- T/ F+ W7 W+ i4 t6 r
- e1.printStackTrace();7 ]) A( Q- k) m x; D) W, ]
- }
* `/ C. F& X( R" |' S2 i+ Y - % o# }$ t7 V$ W. g
- //show resultset
- `4 K: V# j& g; s3 A) q - String sql="select * from student;";
: d7 p3 I8 e4 J - ResultSet res=adb.getResultSet(stam, sql);
, ~$ Q e3 |- k$ Y; x7 U5 O - try {
- V5 v; y- r: F5 B* d1 X6 w; N+ T - System.out.println("name\tmajor\tscore");! r% u, o l" E" o7 ~, U$ o
- while(res.next()){! P1 ]3 h6 f; Q2 | v
- String name,major;0 f2 P J! @( ]& y4 E9 J$ F
- int score;
' y& c: |4 x) a+ R - name=res.getString(1);0 u: E' u) V9 J# l. ~& q& M
- major=res.getString(2);) g) [( A+ H' E1 { [
- score=res.getInt(3);
& _# M, W, z0 R% Y - System.out.println(name+"\t"+major+"\t"+score);
1 ^' b+ T+ Z+ Z; i% o/ l - }
) t( i$ Y. E+ Y! [ - } catch (SQLException e) {
' X. t. ?6 a }4 t1 q; o' w( _ - e.printStackTrace();
5 U/ F* L% j ^2 h - }
. S8 k9 Z R- y/ l/ B' W - try{
+ o! T5 Y; w" M o8 P" C; [, h - res.close(); j/ U) M: i& R( w! Z
- }catch(SQLException e){ t/ @. B. t: L' ]' B( @% G5 ]. T m
- e.printStackTrace();
2 N' D5 \# b6 F4 e) M% ~, P0 U - }
8 D+ w K K. L) `& T - 4 X5 R' k4 ^2 ~7 R- b+ J
- //insert something into table
4 [; k! u6 f9 c2 z. M# \% D, ^ - sql="insert into student(name,major,score) values('f','Chinese','70');";
" g0 r- ` q3 P4 b e - try {
3 n: t* W5 n8 H9 X" ^- | - stam.execute(sql);
3 L, K* o0 T0 t/ M- x; A - } catch (SQLException e) {
/ X* H: @+ ^7 k, L4 T# Y1 y - e.printStackTrace();
& _; s* q, f e+ X. R1 I - }
$ e" L2 k- t+ A - " a4 t% g+ ]/ }
- //delete something from the table/ n/ w' D1 [7 K. Z. }2 _! k- {
- sql="delete from student where name='f';";% i& k. p* ]5 O) C9 E& b
- try{9 P. I. m8 E( }, v, O% f0 }; ^
- stam.executeUpdate(sql);3 j* g, N$ B& _1 g8 b
- }catch(SQLException e){( O6 B5 p* V$ T9 o! O
- e.printStackTrace();
' R: @& l7 o) N, F - }% z" ?% L) I# t- I( ~& f
- 4 P& E( t5 m4 L0 C1 _( _; F
- //change the data int the table
, w0 F! a9 F' ^" o3 q9 O - sql="update student set score=100 where name='a' and major='Chinese'";
. U+ c7 y( f; V* p - try{
- ~# E0 l+ h3 w# p/ E9 y% m0 x/ G - stam.executeUpdate(sql);
& l9 K! ?; S1 }" o% v- q5 b2 T - }catch(SQLException e){
~; \( _/ s" A4 N6 o# n - e.printStackTrace();
5 \# ?' C8 J/ {( S" J - }
" P1 H6 h* N, |1 _0 b -
$ d8 ~! F5 Y- F - //prepared statement- @6 m6 C$ z9 X
- sql="select * from student where name=?";6 E: a1 }2 M( e S
- PreparedStatement pstam=null;
) a$ n6 R: @4 z1 J: Q s% M8 Z - try {# F7 ]. m/ X; T- k/ Q9 g0 W
- pstam=conn.prepareStatement(sql);" U1 W$ n1 _& x0 x6 l- U3 B
- pstam.setString(1, "a");" V! E. S: C; i! a
- res=pstam.executeQuery();. ~0 G6 |6 u4 f3 f T0 ]
- System.out.println("**********************");5 e# Z/ H1 V) j2 |7 q. G: b
- while(res.next()){
9 V( [& D9 I. ?1 d - String name,major;0 z. E0 F9 {5 c0 p5 C" P
- int score;
/ K6 m l' _! o5 @& ~" Q8 Z- P - name=res.getString(1);
: P" }" g2 ?- Q0 O: J- S - major=res.getString(2);
. S6 Z, ]/ F2 c( F5 X K8 F" e - score=res.getInt(3);
. q7 |0 g5 O7 m - System.out.println(name+"\t"+major+"\t"+score);0 J+ N* K# R; _& V% v; R" U9 d
- }$ I% U5 j' ]* \9 [; h- A+ p g, p
- } catch (SQLException e) {1 ^: G- t7 D; D Q
- e.printStackTrace();$ ], Z8 \* l$ S
- }( ?4 }! U6 }7 T* `7 v$ p
- 1 L2 N) g# X) y5 x3 L! M. k
- //release the resource of the program6 q' M- b8 M# J3 w& v, {# x
- try{
$ g+ {5 _6 C5 T# ] - res.close();9 Y8 q" \* a# r. ]
- pstam.close();0 ^5 V6 V7 x% ?3 r% @
- stam.close();8 ^2 ?6 W) a! X. E
- conn.close(); @0 x* b# t- u6 Z6 G B' j2 k( K. T
- }catch(SQLException e){
# n* i3 o' C4 J+ y - e.printStackTrace();
( z4 O+ R4 z: n% Z - }+ Q& Q* v2 {# X
- }" |( n' U$ A( b E# z6 G
- }
复制代码
! a3 m0 U9 x5 O R! F# H0 D) m) p" t: h6 H" w
1 T" T" Z2 Z+ C+ @, [0 ]7 x
|