(1:获取数据库连接的五个方式)

1:点开java项目,new一个Folder文件,取名为lib

2:将sum公司制定的MySQL驱动,复制进我们的lib文件中

3:将MySQL驱动Build Path->Add to Build Path

4:之后就可以使用里面相关的API了

 方式一:

package com.tian.connection;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

public class ConnectionTest {
	@Test
	public void testConnection1() throws SQLException {
		Driver driver = new com.mysql.cj.jdbc.Driver();
		//url:http://localhost:8080/gmall/keyboard.jpg
		//jdbc:mysql:协议
		//localhost:ip地址
		//3306:默认mysql端口号
		//test:test数据库
		String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
			
		Properties info = new Properties();
		info.setProperty("user", "root" );
		info.setProperty("password", "abc123");

		Connection conn = driver.connect(url, info);

		System.out.println(conn);
	}
}

方式二:对方式一的迭代:在如下的程序中不出现第三方的api,使得程序具有更好的可移植性 


	@Test
	public void testConnection2() throws Exception {
		//获取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", "abc123");

		//4.获取连接
		Connection conn = driver.connect(url, info);
		System.out.println(conn);
	}

方式三:使用DriverManager替换Driver

@Test
	public void testConnection3() 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 = "abc123";
		
		// 注册驱动
		DriverManager.registerDriver(driver);

		// 获取连接
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);
	}

方式四:可以只是加载驱动,不用显示的注册驱动过了。


		@Test
		public void testConnection4() throws Exception {
			//1.提供另外三个连接的基本信息
			String url = "jdbc:mysql://localhost:3306/test";
			String user = "root";
	        String password = "abc123";
			
			// 2.加载Driver
			Class.forName("com.mysql.cj.jdbc.Driver");
			//相较于方式三,可以省略如下的操作:
//			Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
//			// 注册驱动
//			DriverManager.registerDriver(driver);
			//为什么可以省略上述操作呢?
			//答:因为在MySQL的Driver实现类中,静态代码块帮我们处理了这些操作
			
			
			// 3.获取连接
			Connection conn = DriverManager.getConnection(url, user, password);
			System.out.println(conn);
		}

方式五(final版):将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接
此种方式的好处?

1.实现了数据与代码的分离,实现了解耦
2.如果需要修改配置文件信息,可以避免程序重新打包。

在src下创建一个配置文件jdbc.properties,并配置如下信息

user=root
password=abc123
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.cj.jdbc.Driver
@Test
		public void testConnection5() throws Exception {
			//1.读取配置文件中的4个基本信息
			InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
			Properties pros = new Properties();
			pros.load(is);
			
			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);
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值