I. First a new configuration file at the jdbc.properties src.
Which provides user, password, url, and driverClass
such as
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);
Close resource follows
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.