数据库编程——JDBC 配置

来源:互联网 发布:vue.js v bind的用法 编辑:程序博客网 时间:2024/05/22 01:33

【0】README

1) 本文文字描述 转自 core java volume 2 , 旨在理解 数据库编程——JDBC 配置 的基础知识 ;
2) update timestamp: 1602022101; 更新内容有: 上传了一些图片, 而且举证说明了 为什么 jdbc.drivers=com.mysql.jdbc.Driver ? 和 为什么 jdbc.url=jdbc:mysql://localhost:3306/testCoreJava ?
3) for source code, please visit : https://github.com/pacosonTang/core-java-volume/tree/master/coreJavaAdvanced/chapter4/JDBCConfig   


【1】数据库URL

1)在连接数据库时, 我们必须使用各种与数据库类型相关的参数,如 主机名, 端口号, 数据库名;
2)JDBC 使用了一种与普通URL 相类似的语法类描述数据源。 下面是这这种语法的两个实例:

jdbc:derby://localhost:8080/corejava;create=true
jdbc:postgresql:corejava

3)JDBC URL 的一般语法为:

jdbc : subprotocol: other stuff

  • 3.1)其中 subprotocol :用于选择连接到 数据库的具体驱动程序;
    这里写图片描述

【2】驱动程序 JAR 文件

1)需要获得包含了 所用数据库的驱动程序的JAR文件;
2)在运行访问数据库的程序时, 需要将驱动程序的 JAR 文件包括到类路径中;
3)在从 命令行启动程序时,只需要使用下面的命令:

java -classpath .: driverPathProgramName
这里写图片描述


【3】注册驱动器类

1)如果驱动程序JAR文件不支持自动注册,那就需要找出数据库提供商使用的JDBC 驱动器类的名字。典型的驱动器名字如下:

org.apache.derby.jdbc.ClientDriver
org.postgresql.Driver

2)通过使用 DriverManager, 可以使用两种方式来注册驱动器。

  • 2.1)一种方式是在 java 程序中 加载驱动器类, 如:

    Class.forName(“org.postgresql.Driver”);

  • 2.2)方法二: 设置 jdbc.drivers 属性。可以用命令行参数来指定这个属性;

  • 2.3)方法三:或者在应用中设置系统属性:
    System.setProperty("jdbc.drivers", "org.postgresql.Driver");

Attention) 在方法三中可以提供多个驱动器, 用冒号将他们分开, 如

org.postgresql.Driver:org.apache.derby.jdbc.ClientDriver;


【4】连接到数据库

1)打开一个数据库连接, 如:

String drivers = props.getProperty(“jdbc.drivers”);
if (drivers != null) System.setProperty(“jdbc.drivers”, drivers);
String url = props.getProperty(“jdbc.url”);
String username = props.getProperty(“jdbc.username”);
String password = props.getProperty(“jdbc.password”);
return DriverManager.getConnection(url, username, password); // 利用驱动管理器打开一个数据库连接

2)驱动管理器(driver manager): 驱动管理器遍历所有注册过的驱动程序, 以便找到一个能够使用 数据库 URL 中指定的子协议的驱动程序;
3) 看个数据库连接荔枝:
这里写图片描述
这里写图片描述

4) source code at a glance

package com.corejava.chapter4;import java.nio.file.*;import java.sql.*;import java.io.*;import java.util.*;public class TestDB{    private static String cur_dir = System.getProperty("user.dir") + File.separator +              "com" + File.separator + "corejava" + File.separator +  "chapter4" + File.separator;   public static void main(String args[]) throws IOException   {      try      {         runTest();      }      catch (SQLException ex)      {         for (Throwable t : ex)            t.printStackTrace();      }   }   public static void runTest() throws SQLException, IOException   {      try (Connection conn = getConnection())      {         Statement stat = conn.createStatement();         stat.executeUpdate("CREATE TABLE Greetings ("                                + "Message CHAR(20))");         stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");         try (ResultSet result = stat.executeQuery("SELECT * FROM Greetings"))         {            if (result.next())               System.out.println(result.getString(1));         }         stat.executeUpdate("DROP TABLE Greetings");      }   }   public static Connection getConnection() throws SQLException, IOException   {      Properties props = new Properties();      try (InputStream in = Files.newInputStream(Paths.get(cur_dir + "database.properties")))      {         props.load(in); // 加载数据库连接信息的 .properties 文件      }      String drivers = props.getProperty("jdbc.drivers"); //数据库驱动器      if (drivers != null) System.setProperty("jdbc.drivers", drivers);      String url = props.getProperty("jdbc.url");      String username = props.getProperty("jdbc.username");      String password = props.getProperty("jdbc.password");      return DriverManager.getConnection(url, username, password); //利用驱动管理器打开一个数据库连接   }}
jdbc.drivers=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testCoreJavajdbc.username=rootjdbc.password=root

Complementary)

  • C1) 为什么 jdbc.drivers=com.mysql.jdbc.Driver ?
    这里写图片描述

    • 以上图片转自:https://dev.mysql.com/doc/connector-j/en/connector-j-installing-classpath.html
  • C2)为什么 jdbc.url=jdbc:mysql://localhost:3306/testCoreJava ?
    这里写图片描述

    • (以上图片转自: Class DriverManager 的 API)

    • 这里写图片描述
      (以上图片转自: http://www.artima.com/javaseminars/modules/JDBC/)

      上面红色标注区域的意思是说:
      subprotocol == 驱动器或数据库连接机制名称;
      subname == 识别数据库的方式;


    • (以上图片转自: https://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager)

      所以这些图片 佐证了:
      subprotocol == 驱动器或数据库连接机制名称 == mysql;
      subname == 识别数据库的方式 == 数据库标识符==your database identifier;

0 0
原创粉丝点击