“获取数据库连接的方式三”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1eJ411c7rf?p=9 | https://www.bilibili.com/video/BV1eJ411c7rf?p=9 | ||
− | 使用 DriverManager | + | 使用 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> |
2022年12月14日 (三) 06:54的版本
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);
}
}