當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

JDBC---連接
2022-04-18 11:01:08

package connection;

import java.io.IOException;
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.Test;

public class ConnectionTest {
	//方式一
	@Test
	public void testConnection1() throws SQLException{
		//獲取Driver實現(xiàn)類對象
		Driver driver=new com.mysql.jdbc.Driver();
		
		//jdbc:mysql:	協(xié)議
		//localhost		ip地址
		//3306			端口號
		//test			數(shù)據(jù)庫名
		
		String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
		
		//將用戶名和密碼封裝在Properties中
		Properties info = new Properties();	
		info.setProperty("user","root");
		info.setProperty("password","abc123");
		
		Connection connect = driver.connect(url, info);
		
		System.out.println(connect);
		
	}
	
	//方式二:對方式一的迭代:在如下的程序中不出現(xiàn)第三方的API,使得程序具有更好的移植性
	@Test
	public void testConnection2() throws Exception{
		//1.獲取Driver實現(xiàn)類對象:使用反射
		Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		
		//2.提供要連接的數(shù)據(jù)庫
		String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
		
		//3.提供連接需要的用戶名和密碼
		Properties info=new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "abc123");
		
		//4.獲取連接
		Connection connect = driver.connect(url, info);
		System.out.println(connect);
		
	}
	
	
	//方式三:使用DriverManger替換Driver
	@Test
	public void testConnection3() throws Exception{
		//1.獲取Driver實現(xiàn)類的對象
		Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		
		//2.提供另外三個連接的基本信息:
		String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
		String user="root";
		String password="abc123";
		
		//注冊驅(qū)動
		DriverManager.registerDriver(driver);
		
		//獲取連接
		Connection connection = DriverManager.getConnection(url, user, password);
		System.out.println(connection);
	}
	
	
	//方式四:只加載驅(qū)動,不再顯示的注冊驅(qū)動
	@Test
	public void testConnection4() throws Exception{
		//1.提供三個連接的基本信息
		String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
		String user="root";
		String password="abc123";
		
		//2.加載Driver
		Class.forName("com.mysql.jdbc.Driver");
		
		//3.獲取連接
		Connection connection = DriverManager.getConnection(url, user, password);
		System.out.println(connection);
		
	}
	/*
	方式五:將數(shù)據(jù)庫連接需要的4個基本信息聲明在配置文件中,
	通過讀取配置文件的方式,獲取連接
	 	此種方式的好處?
	 	1.實現(xiàn)了數(shù)據(jù)與代碼的分離,實現(xiàn)了解耦
	 	2.如果需要修改配置文件信息,可以避免程序重新打包
	 * 	
	 */
	
	@Test
	public void getConnection5() throws IOException, ClassNotFoundException, SQLException{
		//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.加載驅(qū)動
		Class.forName(driverClass);
		
		//3.獲取連接
		Connection connection = DriverManager.getConnection(url, user, password);
		System.out.println(connection);
		
	}
	
	
	
}

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >