“获取数据库连接的方式三”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1eJ411c7rf?p=9”的新页面) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的5个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1eJ411c7rf?p=9 | https://www.bilibili.com/video/BV1eJ411c7rf?p=9 | ||
+ | |||
+ | 使用 DriverManager 替换 Driver<syntaxhighlight lang="java"> | ||
+ | import org.junit.Test; | ||
+ | |||
+ | import java.sql.Connection; | ||
+ | import java.sql.Driver; | ||
+ | import java.sql.DriverManager; | ||
+ | import java.sql.SQLException; | ||
+ | |||
+ | public class ConnectionTest3 { | ||
+ | |||
+ | @Test | ||
+ | public void testConnection() throws SQLException, ClassNotFoundException, InstantiationException, | ||
+ | IllegalAccessException { | ||
+ | |||
+ | //1.获取 Driver 实现类的对象 | ||
+ | Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver"); | ||
+ | Driver driver = (Driver) clazz.newInstance(); | ||
+ | |||
+ | //2.提供另外三个连接的基本信息 | ||
+ | String url = "jdbc:mysql://localhost:3306/guns"; | ||
+ | String user = "root"; | ||
+ | String password = "123456"; | ||
+ | |||
+ | //注册驱动 | ||
+ | DriverManager.registerDriver(driver); | ||
+ | |||
+ | Connection conn = DriverManager.getConnection(url, user, password); | ||
+ | |||
+ | System.out.println(conn); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | com.mysql.cj.jdbc.ConnectionImpl@4278a03f | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | 还有没说的:<syntaxhighlight lang="java"> | ||
+ | DriverManager.registerDriver(driver); | ||
+ | </syntaxhighlight>这行代码对于 mysql-connector-java-8.0.30.jar 来说不需要,因为 作为新版本的连接驱动,它的实现在通过反射实例化 Class 对象的时候,已经注册过驱动了,见源码:<syntaxhighlight lang="java"> | ||
+ | package com.mysql.cj.jdbc; | ||
+ | |||
+ | …… | ||
+ | public class Driver extends NonRegisteringDriver implements java.sql.Driver { | ||
+ | // | ||
+ | // Register ourselves with the DriverManager | ||
+ | // | ||
+ | static { | ||
+ | try { | ||
+ | java.sql.DriverManager.registerDriver(new Driver()); | ||
+ | } catch (SQLException E) { | ||
+ | throw new RuntimeException("Can't register driver!"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | …… | ||
+ | } | ||
+ | </syntaxhighlight>https://github.com/jihch/jdbc/blob/main/src/main/java/io/github/jihch/connection/ConnectionTest3.java |
2022年12月14日 (三) 14:42的最新版本
https://www.bilibili.com/video/BV1eJ411c7rf?p=9
使用 DriverManager 替换 Driver
import org.junit.Test;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionTest3 {
@Test
public void testConnection() throws SQLException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
//1.获取 Driver 实现类的对象
Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2.提供另外三个连接的基本信息
String url = "jdbc:mysql://localhost:3306/guns";
String user = "root";
String password = "123456";
//注册驱动
DriverManager.registerDriver(driver);
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
com.mysql.cj.jdbc.ConnectionImpl@4278a03f
还有没说的:
DriverManager.registerDriver(driver);
这行代码对于 mysql-connector-java-8.0.30.jar 来说不需要,因为 作为新版本的连接驱动,它的实现在通过反射实例化 Class 对象的时候,已经注册过驱动了,见源码:
package com.mysql.cj.jdbc;
……
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
//
// Register ourselves with the DriverManager
//
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
……
}