The acquisition JDBC connection

I. First a new configuration file at the jdbc.properties src.
Which provides user, password, url, and driverClass
such as Here Insert Picture Description
two may be a kit JDBCUtils following new class in a new tool, you can directly call after acquiring the tools of the method of connection methods and resources to close, to avoid duplication of the code.
Code for obtaining the connection method is divided into four steps:
1, read the basic information of four profile

  	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, the loading drive
the Class.forName (driverClass);
. 3, obtaining a connection
Connection conn = DriverManager.getConnection (url, user , password);
Here Insert Picture Description

Close resource follows
Here Insert Picture Description
Here Insert Picture Description
upper deletions change search operations on the database the most basic step, after the call code inside Connection conn = JDBCUtils.getConnection () can be obtained directly connected.

Published 11 original articles · won praise 2 · Views 335

Guess you like

Origin blog.csdn.net/zds18205657013/article/details/102971349