SXT:JDBC超級入門——JDBC編程步驟

來源:互聯網
上載者:User

 JDBC編程步驟:
1.Load the Driver 載入驅動(註冊驅動)

(1) Class.forName("com.mysql.jdbc.Driver");|Class.forName().newInstance()|new DriverName();
//new DriverName()這種串連方式即是 new com.mysql.jdbc.Driver();
(2) 執行個體化時自動向DriverManager註冊,不需要顯式調用DriverManager.registerDriver方法

2.Connect to Database

(1) DriverManager.getConnection()

3.Execute the SQL

(1) Connection.CreateStatement()
(2) Statement.executeQuery()
(3) Statement.executeUpdate()

4.Retireve the result data

(1) 迴圈取得結果while(rs.next())

5.Show the result data

(1) 將資料中的各種資料類型轉換為java中的類型(getXXX)方法

6.close

1.close the resultset / close the statement / close the connection

 

Demo

  1. /**
  2.  * 代碼為入門樣本,主要為熟悉JDBC編程的基本流程,不在頁面上顯示資料,異常等不做詳細考慮。
  3.  */
  4. import java.sql.*;
  5. public class TestJDBC {
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) throws Exception 
  10.     {
  11.         // 1.Load the Driver 載入驅動(註冊驅動)
  12.         new com.mysql.jdbc.Driver();
  13.         //或者使用這種串連方式//Class.forName("com.mysql.jdbc.Driver");
  14.         
  15.         //2.Connect to Database 串連資料庫.以mysql5.0為例子,資料庫名為college,使用者名稱為root,密碼是123456
  16.         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root", "123456");
  17.         
  18.         //3.Execute the SQL
  19.         Statement stmt = conn.createStatement();
  20.         ResultSet rs  = stmt.executeQuery("select * from user");
  21.         
  22.         while(rs.next()) //4.Retireve the result data 遍曆結果集
  23.         {
  24.             //5.Show the result data 顯示結果.將資料中的各種資料類型轉換為java中的類型(getXXX)方法
  25.             System.out.println("The first colum:");
  26.             System.out.println(rs.getString("userName"));
  27.             System.out.println("The second colum:");
  28.             System.out.println(rs.getString("userFlag"));
  29.         }
  30.         
  31.         //6.close 關閉串連,從後開啟的開始,逆序進行關閉
  32.         rs.close();
  33.         stmt.close();
  34.         conn.close();
  35.         
  36.         
  37.     }
  38. }

 

再把Demo完善一點,如下:

  1. import java.sql.*;
  2. public class TestJDBC
  3. {
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String[] args)
  8.     {
  9.         Connection conn = null;
  10.         Statement stmt = null;
  11.         ResultSet rs = null;
  12.         
  13.         try
  14.         {
  15.             // 1.Load the Driver 載入驅動(註冊驅動)
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.             // 或者使用這種串連方式//new com.mysql.jdbc.Driver();
  18.             // 2.Connect to Database
  19.             // 串連資料庫.以mysql5.0為例子,資料庫名為college,使用者名稱為root,密碼是123456
  20.             conn = DriverManager.getConnection(
  21.                     "jdbc:mysql://localhost:3306/college", "root", "123456");
  22.             // 3.Execute the SQL
  23.             stmt = conn.createStatement();
  24.             rs = stmt.executeQuery("select * from user");
  25.             while (rs.next()) // 4.Retireve the result data 遍曆結果集
  26.             {
  27.                 // 5.Show the result data 顯示結果.將資料中的各種資料類型轉換為java中的類型(getXXX)方法
  28.                 System.out.println("The first colum:");
  29.                 System.out.println(rs.getString("userName"));
  30.                 System.out.println("The second colum:");
  31.                 System.out.println(rs.getString("userFlag"));
  32.             }
  33.         } catch (ClassNotFoundException e)
  34.         {
  35.             e.printStackTrace();
  36.         } catch (SQLException e)
  37.         {
  38.             e.printStackTrace();
  39.         } finally
  40.         {
  41.             // 6.close 關閉串連,從後開啟的開始,逆序進行關閉
  42.             try
  43.             {
  44.                 if (rs != null)
  45.                 {
  46.                     rs.close();
  47.                     rs = null;
  48.                 }
  49.                 if (stmt != null)
  50.                 {
  51.                     stmt.close();
  52.                     stmt = null;
  53.                 }
  54.                 if (conn != null)
  55.                 {
  56.                     conn.close();
  57.                     conn = null;
  58.                 }
  59.             } catch (Exception e)
  60.             {
  61.                 e.printStackTrace();
  62.             }
  63.         }
  64.     }
  65. }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.