1.导入mysql-connector-java-8.0.15.jar
commons-dbcp-1.4.jar
commons-pool-1.5.6.jar
2.配置dbcp.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/bookshop?useSSL=false&serverTimezone=UTC
username=root
password=123456
initialSize=10
maxActive=20
maxWait=10000
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class JDBCPoolUtil1 {
private static DataSource dataSource;
static {
Properties p = new Properties();
InputStream in =
JDBCPoolUtil1.class.getClassLoader().getResourceAsStream("dbcp.properties");
try {
p.load(in);
dataSource = BasicDataSourceFactory.createDataSource(p);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn=null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}