3.获取数据库连接方式
2022/1/20 2:20:57
本文主要是介绍3.获取数据库连接方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 1.方式一(了解)
- 2.方式二(了解)
- 3.方式三(了解)
- 4.方式四(了解)
- 方式五:最终版
1.方式一(了解)
1)数据库版本8.0.25
import org.junit.Test; import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; public class ConnectionTest { @Test public void connection() throws SQLException { Driver driver =new com.mysql.cj.jdbc.Driver(); //ctrl + h查看Driver接口的实现类 //url:http://localhost:8080/gmall/keyboard.jpg //jdbc:mysql:协议 //localhost:ip:地址 //3306:默认mysql的端口号 //test:test数据库 String url = "jdbc:mysql://localhost:3306/test"; //将用户名和密码封装在Properties中 Properties info = new Properties(); info.setProperty("user","root");//数据库用户名 info.setProperty("password","123456");//数据库密码 Connection conn = driver.connect(url,info); System.out.println(conn); } }
2)"jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8";
表示获取时区,为东八区,执行完上面代码出错可以试试添加时区,再出错就先打开数据库,再执行上面代码。
2.方式二(了解)
1)IDEA版本:1.8
@Test public void connection1() throws Exception { //1.获取Driver实现类对象:使用反射 Class clazz = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance(); //2.提供要连接的数据库 String url = "jdbc:mysql://localhost:3306/test"; //3.提供连接需要的用户名和密码 Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","123456"); //4.获取连接 Connection conn = driver.connect(url,info); System.out.println(conn); }
2)使用了反射,未使用第三方API,提高了可移植性。
3.方式三(了解)
1)使用DriverManager替代Driver
@Test public void connection2() throws Exception { //1.获取Driver实现类的对象 Class clazz = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance(); //2.提供另外个连接的基本信息 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //3.注册驱动 DriverManager.registerDriver(driver); //4.获取连接 Connection conn = DriverManager.getConnection(url,user,password); System.out.println(conn); }
4.方式四(了解)
1)对方式三的优化:
//方式四:对方式三优化 @Test public void connection3() throws Exception { //1.提供另外个连接的基本信息 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //2.加载Driver Class.forName("com.mysql.cj.jdbc.Driver"); //4.获取连接 Connection conn = DriverManager.getConnection(url,user,password); System.out.println(conn); }
2)相较方式三来说:在mysql的Driver实现类中,声明了如下操作
ublic class Driver extends NonRegisteringDriver implements java.sql.Driver { public Driver() throws SQLException { } static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } } }
方式五:最终版
1)在要操作的Moudule下创建file文件,名字为jdbc.properties
user=root password=123456 url=jdbc:mysql://localhost:3306/test driverClass=com.mysql.cj.jdbc.Driver
@Test public void connection4() throws Exception { //1.读取配置文件中的四个操作 InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties pros = new Properties(); pros.load(resourceAsStream); String user = pros.getProperty("user"); String password = pros.getProperty("password"); String url = pros.getProperty("url"); String driverClass = pros.getProperty("driverClass"); //2.加载驱动 Class.forName(driverClass); //3.获取连接 Connection conn = DriverManager.getConnection(url,user,password); System.out.println(conn); }
2)好处:实现了代码与数据分离。
这篇关于3.获取数据库连接方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南