我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 盖世程序员 > java如何访问mysql数据库
总共48087条微博

动态微博

查看: 1186|回复: 0

java如何访问mysql数据库

[复制链接]

326

主题

72

听众

999

金钱

实习版主

该用户从未签到

优秀版主

跳转到指定楼层
楼主
发表于 2014-11-19 12:48:21 |只看该作者 |正序浏览

1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址

2、编程

(1)加载驱动

(2)编程连接操作

(3)返回结果处理

编程示例

  1. import java.sql.*;
    . f$ [' b) N% w0 ]6 q% j# ?8 |

  2. 9 J8 g/ P. W* @6 c% c
  3. public class Access2Database{
    6 h8 R. y+ Q4 Q! ?/ J
  4.     public Connection getConn(){3 W3 y  W  b: c+ I2 Z) U& i
  5.         Connection conn=null;( |, I  J6 U" K& l  b5 Z( U
  6.         try{
    7 ^" {4 n4 n3 l  e: W
  7.             Class.forName("com.mysql.jdbc.Driver");
    : V8 s8 F5 Z8 ]8 {  D5 I
  8.             String url="jdbc:mysql://localhost:3306/mytest";1 a) H1 }+ e' I7 o3 ^
  9.             String user="root";
    - A8 U" u* D/ O! ]- {0 E
  10.             String password="111";
      I& e* ]7 W, |9 L) x
  11.             conn=DriverManager.getConnection(url, user, password);) }' Z# _8 o& E! M) b2 s) M4 T
  12.             if(conn!=null){' S. V9 x/ _5 L& s" B6 L
  13.                 System.out.println("The connection to database is successful!");
    & @. y% m2 ^5 D) V) D# u# ?: E" p
  14.             }
    " V# H! v4 z! _7 X
  15.         }catch(Exception e){7 g! u6 Q- T: }
  16.             e.printStackTrace();
    0 |. q* }) V3 B* U$ b0 _
  17.         }5 Q0 W  O& ]' ]/ J3 I
  18.         return conn;
    : u/ D3 }4 i" s& }) T$ F
  19.     }5 z% M0 T7 w9 h0 K1 U4 b; n$ g& V
  20.        _4 X1 o$ E4 X: t* ]6 q
  21.     public ResultSet getResultSet(Statement stam,String sql){
    : i( u1 g: E. `/ g  ]
  22.         ResultSet res=null;) D/ m) Q" X" E
  23.         try {$ b9 w1 y2 V( V4 q
  24.             res=stam.executeQuery(sql);
    ( N' b1 j/ U& e8 V. v
  25.         } catch (SQLException e){2 D. D3 S5 V! d3 H" B% q
  26.             e.printStackTrace();
    ( w1 Z0 v$ A' F8 v( d) h
  27.         }
    ! o3 E8 {/ I0 T( o7 {# N" b' V0 a
  28.         return res;6 J8 c! ?& t; d! G- C" n0 ]
  29.     }
    + E+ P7 r- |# a. r6 h+ a
  30.     void showResultSet(ResultSet res){}
    6 h& |$ R" X0 U6 v& h7 n
  31. }) K  p' U% d# z; \
  32. import java.sql.*;
    ; ]- J9 n% P: z5 H, \

  33. ; j: p: C. E% N1 r' ~  d: Q
  34. public class GetConnection{
    ( Z' E9 I6 E1 e( n5 n1 o
  35.     public static void main(String[] args){5 Z8 @! J# l) K. ~. a. \
  36.         Access2Database adb=new Access2Database();
    * S8 [; y# \$ B' D) y
  37.         Connection conn=adb.getConn();$ u* T: D5 v7 y
  38.         Statement stam=null;
    - A0 ^0 G0 ?5 e7 y8 w' J4 _
  39.         try {# o2 V# S1 r1 P, m6 r
  40.             stam = conn.createStatement();; g+ H; U; p5 \# C# J! ]. [0 w, w
  41.         } catch (SQLException e1) {* _9 T" W* j& l# F
  42.             e1.printStackTrace();1 W/ R! B" f0 S6 t# H% }) O
  43.         }7 q1 |" W% q/ P: x$ k+ B6 _5 r( t
  44.          6 p$ E0 i; G9 U, c$ W3 f6 L3 L% S
  45.         //show resultset
    2 ?/ f; L7 ~- y4 I4 ~
  46.         String sql="select * from student;";5 p/ b2 r4 U% m/ H7 z8 m
  47.         ResultSet res=adb.getResultSet(stam, sql);
    0 M$ j, d8 }* c" S# Y
  48.         try {1 n; J) ]$ p( M' v
  49.             System.out.println("name\tmajor\tscore");
    : h( D0 m- R. o
  50.             while(res.next()){
    % Y$ a3 {; c& {! H: C
  51.                 String name,major;
    : d6 A6 c- f9 _( @0 x% ]! {
  52.                 int score;
    + H! H2 k; B( O/ U  n, d$ d
  53.                 name=res.getString(1);
    6 f! ?' B, t+ B1 s4 u
  54.                 major=res.getString(2);8 d4 @1 D/ W# t
  55.                 score=res.getInt(3);
    1 ~8 R9 l  _  u
  56.                 System.out.println(name+"\t"+major+"\t"+score);
    ' N8 d! }! X7 Y* T' f' K4 p1 G
  57.             }
    4 V/ Y6 n' u: P. B- t7 @/ k* R+ ]& t
  58.         } catch (SQLException e) {
    % U6 `$ b) h' J; D' {. w! }! i
  59.             e.printStackTrace();
    % f8 ~0 q, r7 m1 d  C
  60.         }
    / [+ o4 \8 H) a: Q7 c/ N0 }
  61.         try{" v) D. ]4 X) U  F' M2 S/ e& s
  62.         res.close();
    % M5 r+ P- U# {# v! t
  63.         }catch(SQLException e){
    3 D6 j* w  X! ?' t' _# E
  64.             e.printStackTrace();& F, O; l& j1 n+ s' F0 m
  65.         }
    * g) X* M8 J* c2 X5 K0 u
  66.          : }$ ]/ M8 N' t) `1 k
  67.         //insert something into table
    . X7 V: I2 N) n; m: z
  68.         sql="insert into student(name,major,score) values('f','Chinese','70');";
    / R$ `' N9 c/ E/ p2 g/ ?4 X8 k
  69.         try {) h, `! N- y+ g; o! e
  70.             stam.execute(sql);" x; ?2 b  c; o2 \
  71.         } catch (SQLException e) {/ D, h( S  P, p& s: \' O: b0 x* A
  72.             e.printStackTrace();
    8 p4 k/ t3 e' p
  73.         }1 W" W, E9 \: Y1 L& ~" ~
  74.            F/ K* N( w) R& Z9 T
  75.         //delete something from the table
    5 T3 P# B/ w  o1 _4 I  p
  76.         sql="delete from student where name='f';";
    3 m/ B+ ?3 @/ Z5 `% g0 O
  77.         try{# a8 U0 T7 B* F! U' t% P) V- o
  78.             stam.executeUpdate(sql);
    ) b" k% R! ?' p9 {
  79.         }catch(SQLException e){" W9 H9 }6 Z2 d. n% @
  80.             e.printStackTrace();* m5 v+ Y; F1 p+ a8 L/ [
  81.         }0 b' [3 V% ?- T
  82.          
    8 `( g2 ]9 A# y
  83.         //change the data int the table0 T, g1 L, u2 Y
  84.         sql="update student set score=100 where name='a' and major='Chinese'";$ Z( c$ k" r  O3 X- Z
  85.         try{' J+ H1 I9 w6 U
  86.             stam.executeUpdate(sql);. r( z4 G" }! }/ c/ O4 @) A
  87.         }catch(SQLException e){
    ! d4 k% f" k5 p* _
  88.             e.printStackTrace();
    ! D5 T/ y! ~. ~- `: j% ^) \1 J
  89.         }  Q6 u8 W3 e& h. U. a2 ]
  90.          $ o: ]( ^! g7 z$ k/ ^- |6 T
  91.         //prepared statement, ]  o: k8 K* N1 n: N
  92.         sql="select * from student where name=?";
    8 F* r' @7 j' ?/ b" _0 E
  93.         PreparedStatement pstam=null;# n6 C: E' t" W7 m' M& o
  94.         try {
    $ i) p8 q& A- T, K* b* C$ r
  95.             pstam=conn.prepareStatement(sql);9 I& ^9 F9 a! r! l+ a* a! p
  96.             pstam.setString(1, "a");- N: \9 R7 R; U9 T
  97.             res=pstam.executeQuery();
    & U. i' g3 L1 b& g5 ?; P6 x4 b" E
  98.             System.out.println("**********************");4 P* d" \) J4 j6 ?+ a- y9 I8 ]
  99.             while(res.next()){' g( k6 r/ W6 l
  100.                 String name,major;
    0 Q  x9 {2 Y# N2 R; I
  101.                 int score;$ Z. v  L' E) U, O/ ]
  102.                 name=res.getString(1);
    9 h! ^3 l$ j  z9 R2 `) a8 X
  103.                 major=res.getString(2);
    8 s: j3 N! o, D# E4 G7 d
  104.                 score=res.getInt(3);6 S+ c: ]5 w1 S2 l% X) }4 _
  105.                 System.out.println(name+"\t"+major+"\t"+score);% _! T$ `- Y, q; z" A
  106.             }8 |8 S- x& b6 M! T8 r  v! }( O
  107.         } catch (SQLException e) {
    + P. ^# X7 i9 R7 d, S
  108.             e.printStackTrace();/ a6 ~6 Z: L& b% I
  109.         }
    5 P/ [! p9 |# O) N7 y4 h: |
  110.          2 i( l, D; P$ o  g
  111.         //release the resource of the program0 {5 q' R; U+ [- J$ f
  112.         try{
    0 `/ T9 k. h+ R% m
  113.             res.close();
    : G5 E- u8 d0 c6 ~7 s  y
  114.             pstam.close();+ v% `) ~$ H( ~$ x
  115.             stam.close();
    2 ^/ U+ y! q% X+ S! y# B$ E5 T
  116.             conn.close();8 N" I7 _& q, }+ r/ E/ n+ [2 n
  117.         }catch(SQLException e){+ a* n5 M' L0 c7 h9 I6 T
  118.             e.printStackTrace();
    * O" @9 K8 `) \% c% B/ p  H" g7 {
  119.         }- ~5 }3 L4 p) R2 n" h) x5 u7 t
  120.     }' [0 X2 p- S5 g
  121. }
复制代码
' V6 E, x2 j+ A- L
. Z- D# j2 L# J9 P6 _1 S, O5 d

" V$ r6 h2 \2 I6 }1 @. i

科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与科帮网享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、科帮网管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

关闭

站长推荐上一条 /1 下一条

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表