jdbc获取链接

获取链接(final版):将数据库链接须要的4个基本信息声明在配置文件中,经过读取配置文件的方式java

@Test
	public void getConnection5() throws Exception{
		
		//1.读取配置文件中的4个基本信息
//		InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
		//获取系统的类加载器的另外一种方式
		InputStream is = ClassLoader.getSystemClassLoader().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);		
	}

jdbc.propertiesmysql

user=root
password=123456
url=jdbc:mysql://192.168.220.11:3306/test?rewriteBatchedStatements=true
driverClass=com.mysql.cj.jdbc.Driver
相关文章
相关标签/搜索