我的日常

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

动态微博

查看: 1187|回复: 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.*;
    2 ^6 S- E+ D$ l

  2. 4 J) K- ^1 \; }! v2 g$ D! a
  3. public class Access2Database{
    9 j- g) D$ o5 K/ S* ?
  4.     public Connection getConn(){# g5 ~# k% A4 ~- m
  5.         Connection conn=null;
    9 o2 K- J' |8 |
  6.         try{+ _3 {* j& }: U$ b8 h
  7.             Class.forName("com.mysql.jdbc.Driver");* _+ m# a# M  e
  8.             String url="jdbc:mysql://localhost:3306/mytest";
    & l8 U& Y: y' f4 M3 ?
  9.             String user="root";" L. u# G* V: t3 {; h8 P# t
  10.             String password="111";
    , U( V9 p. \, g3 P- D8 x
  11.             conn=DriverManager.getConnection(url, user, password);
    / w5 y% w  z: h: D
  12.             if(conn!=null){8 l6 Y$ g! ^5 D7 z5 S' u
  13.                 System.out.println("The connection to database is successful!");- \* d# M6 i7 m% k. }
  14.             }  _( @! H1 s/ g) Q1 V! G
  15.         }catch(Exception e){4 N$ ]" [& h# t
  16.             e.printStackTrace();, l& W4 @$ {1 d; K6 c
  17.         }
    5 R8 x, u9 ~: V/ j% J' o6 T; q
  18.         return conn;
    ) l0 _# f: K# `! |0 ^. V6 g
  19.     }- r, B% p& V6 k! Z( z
  20.      
    + A" V. o9 H( o
  21.     public ResultSet getResultSet(Statement stam,String sql){0 a6 U/ b7 G! r4 S' c4 @" p7 e0 l, ^$ Y
  22.         ResultSet res=null;
    " @- Y- [% y8 p* @+ |' B) X( ~! j/ L
  23.         try {
    8 F. M# P6 c5 o% p. r) i
  24.             res=stam.executeQuery(sql);3 h: X' R8 k2 c* c" H% d3 k
  25.         } catch (SQLException e){
    4 m$ D* N0 X+ v
  26.             e.printStackTrace();: \7 k; p4 |' r6 t
  27.         }
    4 K) V  I& t# ~. G; a: r9 s  ?
  28.         return res;
    " a  q2 y5 h" ^+ q+ R; s* H2 g; u
  29.     }
    7 @: y( L3 h& W% A$ n+ C
  30.     void showResultSet(ResultSet res){}2 F2 R+ _9 b1 X0 f
  31. }
    0 f4 o  b3 R) D; K- ?. ^
  32. import java.sql.*;
    $ P, ]9 H6 ]; O
  33. 5 U6 T8 H' W$ A9 b) |& d8 y
  34. public class GetConnection{  J% H, {1 \& e8 i
  35.     public static void main(String[] args){1 Z2 K% g9 y) F0 v
  36.         Access2Database adb=new Access2Database();8 E3 S5 r, Y" R* J" S; B
  37.         Connection conn=adb.getConn();
    7 w& `8 ]& H! H! h" X
  38.         Statement stam=null;5 V- I8 Y# U* z" S; [2 C& N8 k
  39.         try {; Y1 W# ?. {& u- x( i
  40.             stam = conn.createStatement();" _& R# p/ W4 Y7 [+ u
  41.         } catch (SQLException e1) {
    % `" y5 v: i. \7 `; [" s3 D
  42.             e1.printStackTrace();+ g3 z+ |) i  H9 e, B. x! |
  43.         }& B: p% z: q) `% k/ Y, m, b6 C
  44.          ) w2 w5 i( N1 O" Z& c
  45.         //show resultset- W/ W6 y" w$ @3 x* P1 B3 m
  46.         String sql="select * from student;";
    ( {( x, F* B% l  x' ^! F
  47.         ResultSet res=adb.getResultSet(stam, sql);! @# \# g- O* J2 a/ t& d4 q
  48.         try {/ I3 `2 R, P+ W2 R% \5 ]
  49.             System.out.println("name\tmajor\tscore");
    , |" k# ]. [. Z( w
  50.             while(res.next()){! f: P' ^) A( Y2 `" m% Z
  51.                 String name,major;
    3 V! ]) a: ?- ~3 N4 o3 ?0 x
  52.                 int score;; @1 j6 y& Z7 f2 v' j
  53.                 name=res.getString(1);
    ' ~# N8 y7 r7 X, o/ z  T8 u
  54.                 major=res.getString(2);
    5 ]. z' w1 O# G; B) B1 {/ Q
  55.                 score=res.getInt(3);! f; u, J1 O! {! Z8 g4 S  _8 P
  56.                 System.out.println(name+"\t"+major+"\t"+score);
    ' L/ n; w. j; K
  57.             }: ?- q8 m/ U8 g- ?
  58.         } catch (SQLException e) {
    / C$ A8 q& N2 [9 Q/ ^6 l5 u% M
  59.             e.printStackTrace();
    , @9 [4 k8 [4 j3 O* @
  60.         }
    & {6 r% d- D. A& G3 w. l
  61.         try{
    / g, c5 ~, f6 G7 j( v2 {
  62.         res.close();
    ; k+ k% u& G' D& z
  63.         }catch(SQLException e){
    1 v" |+ o2 f0 y' k5 T
  64.             e.printStackTrace();
    8 u  O0 ?1 D* ?# K
  65.         }6 y- G/ Y' T1 B4 u( F2 _
  66.          
    ; L% h) ?! v% D
  67.         //insert something into table
    # X8 m+ x- u  ]/ Y* ]  h1 D
  68.         sql="insert into student(name,major,score) values('f','Chinese','70');";) R5 o( |2 n' {. e3 [
  69.         try {
    ; O# ?1 S2 x9 M1 W) J. @4 G  w. k- U- |
  70.             stam.execute(sql);. K! u9 y2 K1 k; c0 A
  71.         } catch (SQLException e) {9 z8 s4 f- M: K2 |1 b! s/ F6 J- N8 O
  72.             e.printStackTrace();4 D$ @6 I% |. R+ I; u' o- n4 C
  73.         }1 i5 {1 b# o) w! B
  74.          
    * g1 D% o: C! k, y/ Y. |- |
  75.         //delete something from the table
    - p, o' o, @  p- ]2 H6 s
  76.         sql="delete from student where name='f';";0 S# S0 k; K7 j( d
  77.         try{7 _8 B$ Q) a/ c; }  I
  78.             stam.executeUpdate(sql);
    2 L9 z' R! ^" F; F0 D9 r6 x1 \0 t
  79.         }catch(SQLException e){
    / |: m! }% i6 y. l% f3 V
  80.             e.printStackTrace();3 f0 z  d0 {& c
  81.         }) g5 b% e1 Z0 y1 P# ~* E( `
  82.          8 W* ], T! q3 ?# ]: z& D
  83.         //change the data int the table( ?: O0 v; g' f% N5 W" F. q
  84.         sql="update student set score=100 where name='a' and major='Chinese'";
    " x  f) M* M) ~2 g9 }( a, L
  85.         try{
    ; L+ p; Y& u+ \2 Z7 G6 U
  86.             stam.executeUpdate(sql);
    1 O+ y' Y( a* q9 u+ h' I
  87.         }catch(SQLException e){
    1 j# N/ E" b' u! n
  88.             e.printStackTrace();& W4 P* M; K5 X: g. t/ ^
  89.         }
    : e! N& W: h' [7 S  y: ^* Q3 Z
  90.          " Z0 A& S+ s+ P/ s  C. Y3 m
  91.         //prepared statement3 m# R6 j: L1 R, l2 E
  92.         sql="select * from student where name=?";
    % b. b* r. @. w; N% v% E
  93.         PreparedStatement pstam=null;) I! {8 ~8 f4 h7 v* m, i
  94.         try {
    * V8 O) {- t6 X
  95.             pstam=conn.prepareStatement(sql);
    & e! D& c3 m( h" D! ^: [. ^+ u+ }
  96.             pstam.setString(1, "a");
    + M! _/ x$ O4 a$ y9 |  U$ X( b6 @
  97.             res=pstam.executeQuery();
    $ ]' R; H8 P0 k& ], I$ E1 N3 B
  98.             System.out.println("**********************");* @: K' ]7 `3 P* T( G. i, K
  99.             while(res.next()){8 G5 d# z4 W0 B7 [* `/ s3 T
  100.                 String name,major;
    4 _! @0 n" J% _. T/ Z( f. r
  101.                 int score;% o. U! x1 U+ E% {" Y! k) l
  102.                 name=res.getString(1);
    % A+ a4 J; j6 t0 Z6 y& Y9 Y) x6 {
  103.                 major=res.getString(2);
    9 ~8 `  Y; h, ?) g
  104.                 score=res.getInt(3);. k1 U2 m1 }! E; ]" Z9 k! W8 a  ?) v
  105.                 System.out.println(name+"\t"+major+"\t"+score);3 C& E2 p5 F. Z8 D' j2 \. a' S% \0 W
  106.             }
    % R8 |7 R. J1 X  F% q3 M% F( N, `" n+ e
  107.         } catch (SQLException e) {
    * V) n) a. r) F: ^& l9 i
  108.             e.printStackTrace();; R  V: ?0 ]6 V9 b0 g% v0 j: ?. K
  109.         }
    : x7 i9 g+ t% r! m
  110.          , ]6 q% Q3 o7 S# ~
  111.         //release the resource of the program+ p3 [# R  N8 ^% O
  112.         try{7 K& b3 M+ ~  A# P) z) ]4 x& K
  113.             res.close();* P4 S. d4 a9 K) ?0 h% M& i8 B4 Q
  114.             pstam.close();
    8 o! }4 g% E( ^
  115.             stam.close();
    9 N! L% E# B. n7 |4 b. B
  116.             conn.close();/ n7 B& U  v8 U$ A) E2 _5 E" {
  117.         }catch(SQLException e){1 q  U2 t) B. k1 r. v
  118.             e.printStackTrace();+ \" `. D# C# r% K
  119.         }3 E1 y# P5 \7 G% t" T
  120.     }* p( d9 U3 c4 p1 j
  121. }
复制代码

: R) q5 \6 z9 p+ h1 U: V1 [# P  b) c6 o& }% t


' A! S' L# F' r) ~& I' W) [: y

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


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

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

   

关闭

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

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