JDBC學習

JDBC

  • JDBC:java database connectivity SUN公司提供的一套操作數據庫的標準規範
  • JDBC規範(掌握四個核心對象)
    • DriverManager:用於註冊驅動
    • Connection:表示與數據庫創建的連接
    • Statement:操作數據庫sql語句的對象
    • ResultSet:結果集或一張虛擬表

JDBC程序

  • 開發一個JDBC程序的準備工作

    • JDBC規範:JDK中
      • java . sql. * ;
      • javax . sql. * ;
    • 數據庫廠商提供的驅動:jar文件
        • . jar
  • 開發一個JDBC程序

    • 實現查詢數據庫中的數據顯示在java的控制台中
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      // 1.創建數據庫表,並向表中添加測試數據
      create database my_jdbc;
      use my_jdbc;

      create table users(
      id int primary key auto_increment,
      name varchar(40),
      password varchar(40),
      email varchar(60),
      birthday date
      )character set utf8 collate utf8_general_ci;

      insert into users(name, password, email, birthday) values('Vincent', '123456a', 'vn_vincent@outlook.com', '1992-03-25');

      insert into users(name, password, email, birthday) values('Eli', 'abcde0', 'eli_ming@outlook.com', '1994-11-15');

      insert into users(name, password, email, birthday) values('Ives', '09876a', 'ives_rowe@outlook.com', '1992-02-22');

      // 2. 創建java project,添加數據庫驅動

      // 3.實現JDBC操作
      // 註冊驅動
      // DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //不建議使用
      Class.forName("com.mysql.jdbc.Driver");

      // 獲取連接Connection
      Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");

      // 得到執行sql語句的對象Statement
      Statement statement = connection.createStatement();

      // 執行sql語句,並返回數據
      ResultSet resultSet = statement.executeQuery("select * from users");

      // 處理數據
      while(resultSet.next() ) {
      System.out.println(resultSet.getObject(1));
      System.out.println(resultSet.getObject(2));
      System.out.println(resultSet.getObject(3));
      System.out.println(resultSet.getObject(4));
      System.out.println(resultSet.getObject(5));
      System.out.println("--------------------");
      }

      // 關閉資源
      resultSet.close();
      statement.close();
      connection.close();

JDBC詳情

  • java.sql.DriverManager類:創建連接
  • a. 註冊驅動

    • DriverManager.registerDriver(new com.mysql.jdbc.Driver()); // 不建議使用
      • 原因:
        • 導致驅動被註冊兩次,
        • 強烈依賴數據庫的驅動jar
      • 解決辦法:
        • Class.forName("com.mysql.jdbc.Driver");
  • b. 與數據庫建立連接

    • static Connection getConnection(String url, String user, String password); // 試圖建立到給定數據庫 URL的連接
  • 方法一:

    1
    2
    3
    getConnection("jdbc://mysql://localhost:3306/my_jdbc", "root", "root");
    // URL: SUN公司與數據庫廠商之間的一種協議
    // jdbc:mysql://localhost:3306/my_jdbc; // (協議 子協議 IP: 端口號 數據庫名稱)
    • mysql:jdbc:mysql://localhost:3306/my_jdbc 或者 jdbc:mysql:////my_jdbc(默認本季連接)
    • oracle:jdbc:oracle:thin:@localhost:1521:sid
  • 注意:MySQL在高版本需要指明是否進行SSL連接;so 如上方法會報錯WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

  • 解決辦法:在jdbc.properties MySQL连接信息jdbc.url中加入ssl=true或false即可,如下所示:jdbc.url=jdbc:mysql://localhost:3306/my_jdbc?characterEncoding=utf8&useSSL=true
  • 方法二:

    1
    2
    3
    4
    Properties info = new Properties();   // 要參考數據文檔
    info.setProperty("user", "root");
    info.setProperty("password", "root");
    getConnection("jdbc.url=jdbc:mysql://localhost:3306/my_jdbc?characterEncoding=utf8&useSSL=true", info);
  • 方法三:

    1
    getConnection("jdbc.url=jdbc:mysql://localhost:3306/my_jdbc?username=root&password=root&characterEncoding=utf8&useSSL=true");

JUnit