1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 - import java.sql.*;
0 y% S6 }8 n1 e - ; u0 a3 U7 p% N, s" ^; P
- public class Access2Database{
2 N+ a* G0 B3 Y! p5 h7 E- a - public Connection getConn(){* o6 r7 `2 a! [6 x5 u
- Connection conn=null;. r( }0 x8 \$ N; ]& T4 @0 a1 C
- try{2 V _& |# c3 m; I2 w5 h% V+ `' O4 F, r
- Class.forName("com.mysql.jdbc.Driver"); y+ J2 k5 D% k, N4 v
- String url="jdbc:mysql://localhost:3306/mytest";
& X) h% b, _6 m5 [ - String user="root";2 S* k7 Q3 E0 q) V- J: x V
- String password="111";" n. d" l+ t! s0 h4 e& {- x5 B
- conn=DriverManager.getConnection(url, user, password);
. D$ W2 J$ \' A$ [) a - if(conn!=null){* S7 W4 h7 P7 V
- System.out.println("The connection to database is successful!");4 ?) U) {7 ?) {7 _
- }
% ~# K4 ~8 ]2 D9 b - }catch(Exception e){
% l# h0 `; @: _9 _ - e.printStackTrace();- C3 v( C3 M) z* d# a4 U
- }+ A5 f5 C* E" T7 e! K
- return conn;- o5 L8 o2 b# p+ D& u: {
- }
4 g7 T& d6 I9 o; p, {' C - * x& h7 x- i+ {
- public ResultSet getResultSet(Statement stam,String sql){- ?! O: S( V4 R+ T. u3 y
- ResultSet res=null;% f; u# F- J, v# |/ V) P5 m
- try {
2 y, b6 W+ t: b - res=stam.executeQuery(sql);
) \4 C9 I# E$ x - } catch (SQLException e){
6 g+ `; b1 O) E# M7 X" @9 j - e.printStackTrace();+ X7 c! a3 ]+ f% A$ l% j
- }: x0 e3 B2 q, n, ^! b
- return res;
+ r; D- ~9 x- V8 _$ J - }5 V3 R; R# u$ \, I
- void showResultSet(ResultSet res){}
/ E2 T8 `. _; w8 @% a - }
" P M* E2 X2 H3 Y2 l - import java.sql.*;7 O. n9 y# Z0 w: G
- ! Y5 H; F/ ^' D: ~" w- J
- public class GetConnection{$ s, ]8 _8 p1 Q1 {1 S
- public static void main(String[] args){: \& m# L2 O, K/ K# \7 x! P, T) i: j
- Access2Database adb=new Access2Database();- S% o( d( n& l; R5 [% c1 U
- Connection conn=adb.getConn();& V& n( x4 Q0 Z1 p6 W! n
- Statement stam=null;
$ d1 V$ W2 X7 H I0 J" x; C# f! t - try {
' i5 P% Q% E5 x: ` - stam = conn.createStatement();$ B. V' t$ V9 K& R: o; l7 u
- } catch (SQLException e1) {3 r+ u1 I/ W: x) j
- e1.printStackTrace();$ P$ @& Q7 p' l' |* w$ W o
- }
' B" e' D9 C/ A! V6 l1 ~7 K -
: Z4 N' o2 S% T- V3 N$ q - //show resultset
) E7 `0 Y/ o& c( R% L - String sql="select * from student;";
. y$ P. g2 ]% U. }5 B: U' V4 M; \ - ResultSet res=adb.getResultSet(stam, sql);( [6 _" r2 i. n! H
- try {
2 }7 P, M; V; O2 {+ } - System.out.println("name\tmajor\tscore");
; b$ ^" u, k) } - while(res.next()){' {% n0 g4 a: W p3 j
- String name,major;
* g) _" B2 G" }) s - int score;
. h: Z0 I8 Q' d- h" P, P6 v - name=res.getString(1);
; W) e* {7 Z2 F9 Y, I: V2 R" l+ p - major=res.getString(2);
# W% ~, I, P. _! H0 r6 G$ V7 I E' K - score=res.getInt(3); t) [& @3 a) b
- System.out.println(name+"\t"+major+"\t"+score);
3 U( J5 ]( a- C+ B/ {% a - }3 ]; E3 ~8 n$ u/ Z
- } catch (SQLException e) {$ J/ C( j# P1 w9 L( D1 n0 r
- e.printStackTrace();
' `* W( |! {+ s4 F+ d8 T' Y0 \ - }7 E; L7 |8 Y4 ?+ Q$ U
- try{% E; ?* `- |# G7 E
- res.close();
{- v( a2 r; H9 N/ ]% k$ x/ u8 R - }catch(SQLException e){
. ^( [4 V# x& q w' d0 F$ E - e.printStackTrace();
9 i5 R7 F4 C0 B- V - }
- Q! j7 t* A0 G' x9 H - ) L. d3 r- ? W# M: Y8 C$ \
- //insert something into table
' M4 N' i% {0 O6 f% W5 C - sql="insert into student(name,major,score) values('f','Chinese','70');";0 f; d- C d) r- \ }
- try {. m. X9 D( v: G' u" @
- stam.execute(sql);
" _9 x5 v" ?" x5 s+ z, J - } catch (SQLException e) {# H4 Y X$ I5 Q0 o* ^9 Q6 i- D
- e.printStackTrace();
! o5 l* [ n) J( d& @ - }+ l% ?* `* N. ^6 N! {* v9 {
-
) W+ A" ?6 V0 @5 Y8 @ - //delete something from the table& U& f. k) N9 b( \& I! ^% J& M
- sql="delete from student where name='f';";" X% W. A7 K, h3 g% s4 a3 ]+ K
- try{
! i% o* J7 g- [, o; m0 l - stam.executeUpdate(sql);: N/ m( ?4 u" `
- }catch(SQLException e){
0 D$ t5 i. U, ^9 Y - e.printStackTrace();" A, X$ }& g9 K
- }
- C( n" A# w: }# X O0 h - 7 h+ L/ r; w0 H: Y# `
- //change the data int the table
, r$ v: I. k, ^6 b, G2 E9 f% T - sql="update student set score=100 where name='a' and major='Chinese'";9 t; a5 R& G y+ s
- try{% b0 ]6 V6 r: Z2 x- p1 f
- stam.executeUpdate(sql);
( P) \, y( S# F, e - }catch(SQLException e){
$ L, w" y' d+ |2 X9 Q0 {9 V9 @ - e.printStackTrace();6 ?- g% f3 ]. z p
- }8 d( {) A. E1 E4 G5 E5 h! k1 V
- 3 c0 Y: D3 C, z
- //prepared statement
) S- F8 f( Z' ^ - sql="select * from student where name=?";1 c! m0 H2 U3 S/ j# ]; u& v
- PreparedStatement pstam=null;1 E7 d) ~$ Q W' P0 c
- try {
! U- U9 C+ l" ^# N& [0 {6 ^; g - pstam=conn.prepareStatement(sql);
3 e0 P' U- m' Y7 d3 l6 H - pstam.setString(1, "a");# O, c7 }* ~3 E/ g4 N2 Z: x9 B$ }
- res=pstam.executeQuery();
. V( l& R8 v' b5 V: b+ T - System.out.println("**********************");
* f& H V* z# g" `2 Y - while(res.next()){
/ D) P, A1 K- u# O - String name,major;
* y+ Z' h; H$ [/ O3 A$ ]: b: f+ g( W4 Y - int score;
G& F' Q) G9 y$ E - name=res.getString(1);
N: Z1 _" A- t - major=res.getString(2);
0 g1 z" P' P* ] - score=res.getInt(3);
% \ M7 |0 m2 H6 x - System.out.println(name+"\t"+major+"\t"+score);0 Z! m7 K( g9 h Q. i* c ~
- }! N1 b# N9 C6 ]
- } catch (SQLException e) {
9 o. n9 A0 C) |/ [/ p; Y$ {$ | - e.printStackTrace();' D8 u# _& l1 p" g, ^1 Z: v% }4 ~
- }9 \- n Z' e2 Z4 l: e u3 R
- ( T) T: m0 M7 W
- //release the resource of the program
$ V( j3 ^. y. Z3 N% X - try{
- W! {% i$ P- ~7 f& I - res.close();/ K1 Y. _, a4 ^. d
- pstam.close();
. a: | M( i1 D! p4 S9 ~$ Y9 X - stam.close();
# Y/ ]4 J% a8 S3 v2 y - conn.close();
( X3 `+ T1 P- C+ ?/ J - }catch(SQLException e){
3 M, S( c: ~- z' p; p9 K - e.printStackTrace();
7 O& C5 t% U7 Y# z, | - }
& y, ]5 R' R: p& l; k8 | - }
2 P9 C+ v: K! p+ a) n1 K/ D$ n1 @ - }
复制代码
, p/ `* Y$ J. G8 ^
. j8 {' W7 f- M) T- R2 p 7 a) }/ j. ^! t; L7 ]% `
|