JDBC——获取数据库链接的5种方式

JDBC链接数据库有五种方式,在这里推荐第五种

方式一:

public class ConnectionTest {
	//方式1:
	@Test
	public void testConnection1() throws SQLException {
		//获取Driver的实现类对象
		Driver driver=new com.mysql.jdbc.Driver();
		//jdbc:mysql:协议
		//localhost:ip地址
		//3306:默认mysql端口号
		//test:test数据库
		String url ="jdbc:mysql://localhost:3306/test";
		//Properties 将用户名和密码封装在Properties里
		Properties info=new Properties();
		info.setProperty("user","root");
		info.setProperty("password", "123456");
		
		Connection conn=driver.connect(url, info);
		System.out.println(conn);
	}

方式二:

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

@Test
	public void testConnection2() throws Exception {
		//1 获取Driver实现类对象:使用反射
		Class clazz =Class.forName("com.mysql.jdbc.Driver");
		 Driver driver=(Driver) clazz.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);
	}

方式三:

使用DriverManager替换Driver

@Test
	public void testConnection3() throws Exception {
		//1 获取Driver实现类对象
		Class clazz =Class.forName("com.mysql.jdbc.Driver");
		Driver driver=(Driver) clazz.newInstance();
		
		//2 提供另外三个连接的基本信息
		String url="jdbc:mysql://localhost:3306/test";
		String user="root";
		String password="123456";
		
		//注册驱动
		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="123456";
						
			//2 加载Driver
			Class.forName("com.mysql.jdbc.Driver");
//			Driver driver=(Driver) clazz.newInstance();			
//			//注册驱动
//			DriverManager.registerDriver(driver);
			/*为什么可以省略上述操作呢?
			 * 在mysql的Driver实现类中,声明了如下的操作
			 * statac{
			 * 		try{
			 * 			java.sql.DriverManager.registerDriver(new Driver());
			 * 		}catch(SQLException E){
			 * 			throw new RuntimeException("Can't register driver!");
			 * 		}
			 * 		}
			 * 
			 */
			
			
			//3 获取链接
			Connection conn=DriverManager.getConnection(url, user, password);
			System.out.println(conn);
		}

方式五(推荐):

将数据库链接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式:获取链接
优点
1、实现数据与代码的分离 ,实现了解耦
2、如果要修改配置文件信息,可以避免程序重打包
第一步:在项目src下创建配置文件
在这里插入图片描述
第二步:内容如图所示
在这里插入图片描述

@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);
		}
posted @ 2020-06-01 13:14  秋弦  阅读(1024)  评论(0编辑  收藏  举报