“获取数据库连接的方式四”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1eJ411c7rf?p=10”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1eJ411c7rf?p=10 | https://www.bilibili.com/video/BV1eJ411c7rf?p=10 | ||
+ | |||
+ | 额,这个说的原来就是我在前面的示例里发现的:<syntaxhighlight lang="java"> | ||
+ | import org.junit.Test; | ||
+ | |||
+ | import java.sql.Connection; | ||
+ | import java.sql.DriverManager; | ||
+ | import java.sql.SQLException; | ||
+ | |||
+ | public class ConnectionTest4 { | ||
+ | |||
+ | @Test | ||
+ | public void testConnection() throws SQLException, ClassNotFoundException { | ||
+ | |||
+ | //1.获取 Driver 实现类的对象 | ||
+ | Class.forName("com.mysql.cj.jdbc.Driver"); | ||
+ | |||
+ | //2.提供另外三个连接的基本信息 | ||
+ | String url = "jdbc:mysql://localhost:3306/guns"; | ||
+ | String user = "root"; | ||
+ | String password = "123456"; | ||
+ | |||
+ | Connection conn = DriverManager.getConnection(url, user, password); | ||
+ | |||
+ | System.out.println(conn); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | com.mysql.cj.jdbc.ConnectionImpl@4278a03f | ||
+ | </syntaxhighlight>注册驱动的实现已经在 第三方的驱动包里自己实现了,所以就只要通过类名加载类实例就可以 |
2022年12月14日 (三) 07:10的版本
https://www.bilibili.com/video/BV1eJ411c7rf?p=10
额,这个说的原来就是我在前面的示例里发现的:
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionTest4 {
@Test
public void testConnection() throws SQLException, ClassNotFoundException {
//1.获取 Driver 实现类的对象
Class.forName("com.mysql.cj.jdbc.Driver");
//2.提供另外三个连接的基本信息
String url = "jdbc:mysql://localhost:3306/guns";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
com.mysql.cj.jdbc.ConnectionImpl@4278a03f
注册驱动的实现已经在 第三方的驱动包里自己实现了,所以就只要通过类名加载类实例就可以