我的日常

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

动态微博

查看: 1189|回复: 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.*;
    0 y% S6 }8 n1 e
  2. ; u0 a3 U7 p% N, s" ^; P
  3. public class Access2Database{
    2 N+ a* G0 B3 Y! p5 h7 E- a
  4.     public Connection getConn(){* o6 r7 `2 a! [6 x5 u
  5.         Connection conn=null;. r( }0 x8 \$ N; ]& T4 @0 a1 C
  6.         try{2 V  _& |# c3 m; I2 w5 h% V+ `' O4 F, r
  7.             Class.forName("com.mysql.jdbc.Driver");  y+ J2 k5 D% k, N4 v
  8.             String url="jdbc:mysql://localhost:3306/mytest";
    & X) h% b, _6 m5 [
  9.             String user="root";2 S* k7 Q3 E0 q) V- J: x  V
  10.             String password="111";" n. d" l+ t! s0 h4 e& {- x5 B
  11.             conn=DriverManager.getConnection(url, user, password);
    . D$ W2 J$ \' A$ [) a
  12.             if(conn!=null){* S7 W4 h7 P7 V
  13.                 System.out.println("The connection to database is successful!");4 ?) U) {7 ?) {7 _
  14.             }
    % ~# K4 ~8 ]2 D9 b
  15.         }catch(Exception e){
    % l# h0 `; @: _9 _
  16.             e.printStackTrace();- C3 v( C3 M) z* d# a4 U
  17.         }+ A5 f5 C* E" T7 e! K
  18.         return conn;- o5 L8 o2 b# p+ D& u: {
  19.     }
    4 g7 T& d6 I9 o; p, {' C
  20.      * x& h7 x- i+ {
  21.     public ResultSet getResultSet(Statement stam,String sql){- ?! O: S( V4 R+ T. u3 y
  22.         ResultSet res=null;% f; u# F- J, v# |/ V) P5 m
  23.         try {
    2 y, b6 W+ t: b
  24.             res=stam.executeQuery(sql);
    ) \4 C9 I# E$ x
  25.         } catch (SQLException e){
    6 g+ `; b1 O) E# M7 X" @9 j
  26.             e.printStackTrace();+ X7 c! a3 ]+ f% A$ l% j
  27.         }: x0 e3 B2 q, n, ^! b
  28.         return res;
    + r; D- ~9 x- V8 _$ J
  29.     }5 V3 R; R# u$ \, I
  30.     void showResultSet(ResultSet res){}
    / E2 T8 `. _; w8 @% a
  31. }
    " P  M* E2 X2 H3 Y2 l
  32. import java.sql.*;7 O. n9 y# Z0 w: G
  33. ! Y5 H; F/ ^' D: ~" w- J
  34. public class GetConnection{$ s, ]8 _8 p1 Q1 {1 S
  35.     public static void main(String[] args){: \& m# L2 O, K/ K# \7 x! P, T) i: j
  36.         Access2Database adb=new Access2Database();- S% o( d( n& l; R5 [% c1 U
  37.         Connection conn=adb.getConn();& V& n( x4 Q0 Z1 p6 W! n
  38.         Statement stam=null;
    $ d1 V$ W2 X7 H  I0 J" x; C# f! t
  39.         try {
    ' i5 P% Q% E5 x: `
  40.             stam = conn.createStatement();$ B. V' t$ V9 K& R: o; l7 u
  41.         } catch (SQLException e1) {3 r+ u1 I/ W: x) j
  42.             e1.printStackTrace();$ P$ @& Q7 p' l' |* w$ W  o
  43.         }
    ' B" e' D9 C/ A! V6 l1 ~7 K
  44.          
    : Z4 N' o2 S% T- V3 N$ q
  45.         //show resultset
    ) E7 `0 Y/ o& c( R% L
  46.         String sql="select * from student;";
    . y$ P. g2 ]% U. }5 B: U' V4 M; \
  47.         ResultSet res=adb.getResultSet(stam, sql);( [6 _" r2 i. n! H
  48.         try {
    2 }7 P, M; V; O2 {+ }
  49.             System.out.println("name\tmajor\tscore");
    ; b$ ^" u, k) }
  50.             while(res.next()){' {% n0 g4 a: W  p3 j
  51.                 String name,major;
    * g) _" B2 G" }) s
  52.                 int score;
    . h: Z0 I8 Q' d- h" P, P6 v
  53.                 name=res.getString(1);
    ; W) e* {7 Z2 F9 Y, I: V2 R" l+ p
  54.                 major=res.getString(2);
    # W% ~, I, P. _! H0 r6 G$ V7 I  E' K
  55.                 score=res.getInt(3);  t) [& @3 a) b
  56.                 System.out.println(name+"\t"+major+"\t"+score);
    3 U( J5 ]( a- C+ B/ {% a
  57.             }3 ]; E3 ~8 n$ u/ Z
  58.         } catch (SQLException e) {$ J/ C( j# P1 w9 L( D1 n0 r
  59.             e.printStackTrace();
    ' `* W( |! {+ s4 F+ d8 T' Y0 \
  60.         }7 E; L7 |8 Y4 ?+ Q$ U
  61.         try{% E; ?* `- |# G7 E
  62.         res.close();
      {- v( a2 r; H9 N/ ]% k$ x/ u8 R
  63.         }catch(SQLException e){
    . ^( [4 V# x& q  w' d0 F$ E
  64.             e.printStackTrace();
    9 i5 R7 F4 C0 B- V
  65.         }
    - Q! j7 t* A0 G' x9 H
  66.          ) L. d3 r- ?  W# M: Y8 C$ \
  67.         //insert something into table
    ' M4 N' i% {0 O6 f% W5 C
  68.         sql="insert into student(name,major,score) values('f','Chinese','70');";0 f; d- C  d) r- \  }
  69.         try {. m. X9 D( v: G' u" @
  70.             stam.execute(sql);
    " _9 x5 v" ?" x5 s+ z, J
  71.         } catch (SQLException e) {# H4 Y  X$ I5 Q0 o* ^9 Q6 i- D
  72.             e.printStackTrace();
    ! o5 l* [  n) J( d& @
  73.         }+ l% ?* `* N. ^6 N! {* v9 {
  74.          
    ) W+ A" ?6 V0 @5 Y8 @
  75.         //delete something from the table& U& f. k) N9 b( \& I! ^% J& M
  76.         sql="delete from student where name='f';";" X% W. A7 K, h3 g% s4 a3 ]+ K
  77.         try{
    ! i% o* J7 g- [, o; m0 l
  78.             stam.executeUpdate(sql);: N/ m( ?4 u" `
  79.         }catch(SQLException e){
    0 D$ t5 i. U, ^9 Y
  80.             e.printStackTrace();" A, X$ }& g9 K
  81.         }
    - C( n" A# w: }# X  O0 h
  82.          7 h+ L/ r; w0 H: Y# `
  83.         //change the data int the table
    , r$ v: I. k, ^6 b, G2 E9 f% T
  84.         sql="update student set score=100 where name='a' and major='Chinese'";9 t; a5 R& G  y+ s
  85.         try{% b0 ]6 V6 r: Z2 x- p1 f
  86.             stam.executeUpdate(sql);
    ( P) \, y( S# F, e
  87.         }catch(SQLException e){
    $ L, w" y' d+ |2 X9 Q0 {9 V9 @
  88.             e.printStackTrace();6 ?- g% f3 ]. z  p
  89.         }8 d( {) A. E1 E4 G5 E5 h! k1 V
  90.          3 c0 Y: D3 C, z
  91.         //prepared statement
    ) S- F8 f( Z' ^
  92.         sql="select * from student where name=?";1 c! m0 H2 U3 S/ j# ]; u& v
  93.         PreparedStatement pstam=null;1 E7 d) ~$ Q  W' P0 c
  94.         try {
    ! U- U9 C+ l" ^# N& [0 {6 ^; g
  95.             pstam=conn.prepareStatement(sql);
    3 e0 P' U- m' Y7 d3 l6 H
  96.             pstam.setString(1, "a");# O, c7 }* ~3 E/ g4 N2 Z: x9 B$ }
  97.             res=pstam.executeQuery();
    . V( l& R8 v' b5 V: b+ T
  98.             System.out.println("**********************");
    * f& H  V* z# g" `2 Y
  99.             while(res.next()){
    / D) P, A1 K- u# O
  100.                 String name,major;
    * y+ Z' h; H$ [/ O3 A$ ]: b: f+ g( W4 Y
  101.                 int score;
      G& F' Q) G9 y$ E
  102.                 name=res.getString(1);
      N: Z1 _" A- t
  103.                 major=res.getString(2);
    0 g1 z" P' P* ]
  104.                 score=res.getInt(3);
    % \  M7 |0 m2 H6 x
  105.                 System.out.println(name+"\t"+major+"\t"+score);0 Z! m7 K( g9 h  Q. i* c  ~
  106.             }! N1 b# N9 C6 ]
  107.         } catch (SQLException e) {
    9 o. n9 A0 C) |/ [/ p; Y$ {$ |
  108.             e.printStackTrace();' D8 u# _& l1 p" g, ^1 Z: v% }4 ~
  109.         }9 \- n  Z' e2 Z4 l: e  u3 R
  110.          ( T) T: m0 M7 W
  111.         //release the resource of the program
    $ V( j3 ^. y. Z3 N% X
  112.         try{
    - W! {% i$ P- ~7 f& I
  113.             res.close();/ K1 Y. _, a4 ^. d
  114.             pstam.close();
    . a: |  M( i1 D! p4 S9 ~$ Y9 X
  115.             stam.close();
    # Y/ ]4 J% a8 S3 v2 y
  116.             conn.close();
    ( X3 `+ T1 P- C+ ?/ J
  117.         }catch(SQLException e){
    3 M, S( c: ~- z' p; p9 K
  118.             e.printStackTrace();
    7 O& C5 t% U7 Y# z, |
  119.         }
    & y, ]5 R' R: p& l; k8 |
  120.     }
    2 P9 C+ v: K! p+ a) n1 K/ D$ n1 @
  121. }
复制代码

, p/ `* Y$ J. G8 ^
. j8 {' W7 f- M) T- R2 p

7 a) }/ j. ^! t; L7 ]% `

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


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

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

   

关闭

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

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