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
相關文章
相關標籤/搜索