1.添加jar包以及配置文件
我们需要commons-dbcp、 commons-pool这两个包
再次需要配置文件 dbcpconfig.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test01
username=root
password=123456
initialSize=20
maxActive=100
maxIdle=20
minIdle=5
maxWait=6000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultTransactionIsolation=REPEATABLE_READ
之后编写工具类DbcpUtils
public class DBcpUtils {
private static Properties properties=new Properties();
private static DataSource dataSource=null;
//获取连接池
static {
try {
properties.load(new FileInputStream("dbcpconfig.properties"));
dataSource=BasicDataSourceFactory.createDataSource(properties);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//连接函数
public static Connection getConnection() {
Connection conn=null;
try {
conn=dataSource.getConnection();
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
Dao层
后面根据具体功能便可实际运用了