1 package org.day01;
2
3 import java.io.InputStream;
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.SQLException;
7 import java.util.Properties;
8
9 public class ConnectionUtils {
10 private static String driver;
11
12 private static String url;
13
14 private static String user;
15
16 private static String password;
17
18 static {
19 try {
20 Properties props = new Properties();
21 // 从类路径下加载属性文件
22 InputStream is = ConnectionUtils.class.getClassLoader()
23 .getResourceAsStream("db.properties");
24 props.load(is);
25
26 driver = props.getProperty("driver");
27 url = props.getProperty("url");
28 user = props.getProperty("user");
29 password = props.getProperty("password");
30
31 Class.forName(driver);
32
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 }
37
38 public static Connection getConnection() throws SQLException {
39 return DriverManager.getConnection(url, user, password);
40 }
41 }