jdbc获取连接的五种方式

本文详细介绍了使用JDBC连接数据库的五种常见方法,包括直接使用Driver、使用DriverManager、通过反射加载驱动、利用配置文件读取连接信息等,为Java开发者提供全面的数据库连接解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jdbc获取连接的五种方式

package com.sammery.connectionTest;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.ClassRule;
import org.junit.Test;

public class ConnectionTest {
	
	//方式一:
	@Test
	public void testConnection1() throws SQLException{
		Driver driver = new com.mysql.jdbc.Driver();
		
		String url = "jdbc:mysql://localhost:3306/test";
		
		//封装用户名、密码
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "root");
		Connection connection = driver.connect(url, info );
		
		System.out.println(connection);
		
	}
	
	//方式二:方式一的迭代
	@Test
	public void testConnection2() throws Exception {
		//获取Driver的实现类对象,使用反射:在如下的程序中不出现第三方的api,使得程序具有更好的可移植性
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		
		String url = "jdbc:mysql://localhost:3306/test";
		
		//封装用户名、密码
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "root");
		Connection connection = driver.connect(url, info );
		
		System.out.println(connection);
		
	}
	
	//方式三:使用DriverManager替换Driver
		@Test
		public void testConnection3() throws Exception {
			//
			Class clazz = Class.forName("com.mysql.jdbc.Driver");
			Driver driver = (Driver) clazz.newInstance();
			
			DriverManager.registerDriver(driver);
			
			String url = "jdbc:mysql://localhost:3306/test";
			String user = "root";
			String password = "root";
			Connection connection = DriverManager.getConnection(url, user, password);
			
			System.out.println(connection);
			
		}
		
		//方式四:方法三基础之上优化
				@Test
				public void testConnection4() throws Exception {
					
					String url = "jdbc:mysql://localhost:3306/test";
					String user = "root";
					String password = "root";
					
					Class.forName("com.mysql.jdbc.Driver");
//					Class clazz = Class.forName("com.mysql.jdbc.Driver");
//					Driver driver = (Driver) clazz.newInstance();
//					DriverManager.registerDriver(driver);
					/*在MySQL的Driver实现类中,声明了如下操作:
					 static {
					 	try{
					 		java.sql.DriverManager.registerDriver(new Driver());
					 	}catch (SQLException E){
					 		throws new RuntimeException("Can't register driver!");
					 	}
					 }
					 */
					Connection connection = DriverManager.getConnection(url, user, password);
					
					System.out.println(connection);
				}
				
				//方式五:将数据库连接需要的4个基本信息声明在配置文件中,通过读取该配置文件的方式,获取连接
				/*
				 * 1.实现了数据与代码的分离。实现类解耦
				 * 2.如果需要修改配置文件信息,可以避免程序重新打包。*/
				@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");
					
					System.out.println(user+","+password+","+url+","+driverClass);
					//2.加载驱动
					Class.forName(driverClass);
					
					//3.获取链接
					Connection connection = DriverManager.getConnection(url, user, password);
					System.out.println(connection);
				}

}

jdbc.properties 配置信息文件

user=root
password=root
url=jdbc:mysqL://localhost:3306/test
driverClass=com.mysql.jdbc.Driver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值