Java 通過JDBC串連Mysql資料庫

來源:互聯網
上載者:User

標籤:

import  java.sql.DriverManager; import  java.sql.ResultSet; import  java.sql.SQLException; import  java.sql.Connection; import  java.sql.Statement;   public  class  MysqlDemo {      public  static  void  main(String[] args) throws  Exception {          Connection conn = null ;          String sql;          // MySQL的JDBC URL編寫方式:jdbc:mysql://主機名稱:串連連接埠/資料庫的名稱?參數=值          // 避免中文亂碼要指定useUnicode和characterEncoding          // 執行資料庫操作之前要在資料庫管理系統上建立一個資料庫,名字自己定,          // 下面語句之前就要先建立javademo資料庫          String url = "jdbc:mysql://localhost:3306/javademo?"                  + "user=root&password=root&useUnicode=true&characterEncoding=UTF8" ;           try  {              // 之所以要使用下面這條語句,是因為要使用MySQL的驅動,所以我們要把它驅動起來,              // 可以通過Class.forName把它載入進去,也可以通過初始化來驅動起來,下面三種形式都可以              Class.forName( "com.mysql.jdbc.Driver" ); // 動態載入mysql驅動              // or:              // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();              // or:              // new com.mysql.jdbc.Driver();               System.out.println( "成功載入MySQL驅動程式" );              // 一個Connection代表一個資料庫連接              conn = DriverManager.getConnection(url);              // Statement裡面帶有很多方法,比如executeUpdate可以實現插入,更新和刪除等              Statement stmt = conn.createStatement();              sql = "create table student(NO char(20),name varchar(20),primary key(NO))" ;              int  result = stmt.executeUpdate(sql); // executeUpdate語句會返回一個受影響的行數,如果返回-1就沒有成功              if  (result != - 1 ) {                  System.out.println( "建立資料表成功" );                  sql = "insert into student(NO,name) values(‘2012001‘,‘陶偉基‘)" ;                  result = stmt.executeUpdate(sql);                  sql = "insert into student(NO,name) values(‘2012002‘,‘周小俊‘)" ;                  result = stmt.executeUpdate(sql);                  sql = "select * from student" ;                  ResultSet rs = stmt.executeQuery(sql); // executeQuery會返回結果的集合,否則返回空值                  System.out.println( "學號\t姓名" );                  while  (rs.next()) {                      System.out                              .println(rs.getString( 1 ) + "\t"  + rs.getString( 2 )); // 入如果返回的是int類型可以用getInt()                  }              }          } catch  (SQLException e) {              System.out.println( "MySQL操作錯誤" );              e.printStackTrace();          } catch  (Exception e) {              e.printStackTrace();          } finally  {              conn.close();          }       }  }

Java 通過JDBC串連Mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.