读取配置文件对数据库初始化,使用静态代码块在类加载前调用
()
| 23 | * 读取配置文件对数据库初始化,使用静态代码块在类加载前调用 |
| 24 | */ |
| 25 | public static void init() { |
| 26 | InputStream resourceAsStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties"); |
| 27 | Properties properties = new Properties(); |
| 28 | |
| 29 | try { |
| 30 | properties.load(resourceAsStream); |
| 31 | } catch (IOException e) { |
| 32 | e.printStackTrace(); |
| 33 | } |
| 34 | driver = properties.getProperty("driver"); |
| 35 | url = properties.getProperty("url"); |
| 36 | user = properties.getProperty("user"); |
| 37 | password = properties.getProperty("password"); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * 此方法获得一个connection连接 |