JDBC-Connection


新建Maven工程

pom.xml

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!-- 指定jdk -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

数据库信息

driver=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://192.168.8.136:3306/jdbc
user=root
password=root

获取链接

Driver

/**
 * Driver 是一个接口: 可以通过 Driver 的实现类对象获取数据库连接
 */
@Test
public void testDriver() throws SQLException {
    // 创建 Driver 实现类对象
    Driver driver = new com.mysql.cj.jdbc.Driver();

    // 准备连接数据库信息: url, user, password
    String url = "jdbc:mysql://192.168.8.136:3306/jdbc";
    Properties info = new Properties();
    info.put("user", "root");
    info.put("password", "root");

    // 调用 Driver 接口的 connect(url, info) 获取数据库连接
    Connection connection = driver.connect(url, info);

    System.out.println(connection);
}

@Test
public void getConnection() throws Exception{
    // 读取类路径下的 jdbc.properties 文件
    InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
    Properties properties = new Properties();
    properties.load(in);

    String driverClass = properties.getProperty("driver");
    String jdbcUrl = properties.getProperty("jdbcUrl");
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");

    //通过反射 Driver 对象
    Driver driver = (Driver) Class.forName(driverClass).newInstance();

    Properties info = new Properties();
    info.put("user", user);
    info.put("password", password);

    //通过 Driver 的 connect 方法获取数据库连接
    Connection connection = driver.connect(jdbcUrl, info);

    System.out.println(connection);
}

DriverManager

/**
 * DriverManager 是驱动的管理类
 * 1). 可以通过重载的 getConnection() 方法获取数据库连接. 较为方便
 * 2). 可以同时管理多个驱动程序: 若注册了多个数据库连接, 则调用 getConnection()
 * @throws Exception
 */
@Test
public void testDriverManager() throws Exception{
    // 准备连接数据库
    String driverClass = "com.mysql.cj.jdbc.Driver";
    String jdbcUrl = "jdbc:mysql://192.168.8.136/jdbc";
    String user = "root";
    String password = "root";

    // 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块
    Class.forName(driverClass);
    // 通过 DriverManager 的 getConnection() 方法获取数据库连接
    Connection connection = DriverManager.getConnection(jdbcUrl, user, password);

    System.out.println(connection);
}

@Test
public void getConnection2() throws Exception{
    Properties properties = new Properties();
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
    properties.load(in);

    String driver = properties.getProperty("driver");
    String jdbcUrl = properties.getProperty("jdbcUrl");
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");

    // 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块)
    Class.forName(driver);
    // 通过 DriverManager 的 getConnection() 方法获取数据库连接.
    Connection connection = DriverManager.getConnection(jdbcUrl, user, password);

    System.out.println(connection);
}

oracle官方JDBC文档


原文链接:https://www.cnblogs.com/jhxxb/p/10436018.html