一 首先要有数据源 ,这里采用
DruidDataSource dataSource = new DruidDataSource();
二 读取配置文件,写入Properties prop
三 从 prop读取参数写入 dataSource
String property = prop.getProperty("url"); if (property != null) { dataSource.setUrl(property); } property = prop.getProperty("username"); if (property != null) { dataSource.setUsername(property); } property = prop.getProperty("password"); if (property != null) { dataSource.setPassword(property); } property = prop.getProperty("drivername"); if (property != null) { dataSource.setDriverClassName(property); }
四 得到数据库链接源
Connection conn = dataSource.getConnection()
五 通过数据库链接源 Connection conn 各种操作数据库