配置文件 config.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://主机名:3306/数据库名
username=用户名
password=密码
JDBCUtils工具类
public class JDBCUtils {
private JDBCUtils() {} //构造函数私有化
private static String driverClass;
private static String url;
private static String username;
private static String password;
private static Connection con;
static {
try {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream(“config.properties”);
Properties properties = new Properties();
properties.load(is);
driverClass = properties.getProperty(“driverClass”);
url = properties.getProperty(“url”);
username = properties.getProperty(“username”);
password = properties.getProperty(“password”);
Class.forName(driverClass);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() { //获取连接对象
try {
con = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//关闭连接(有查询结果集)
public static void close(Connection con, Statement stat, ResultSet res) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (res != null) {
try {
re
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭连接(无查询结果集)
public static void close(Connection con, Statement stat) {
close(con, stat, null);
}
}
实现连接池类
======
定义一个连接池类并实现 java.sql.DataSource 接口。
Connection getConnection(); //获取数据库连接对象
public class MyDataSource implements DataSource{
//定义集合容器,用于保存多个数据库连接对象
//使用Collections 工具类实现集合的线程同步
private static List pool = Collections.synchronizedList(new ArrayList());
//静态代码块,生成10个数据库连接保存到集合中
static {
for (int i = 0; i < 10; i++) {
Connection con = JDBCUtils.getConnection();
pool.add(con);
}
}
//返回连接池的大小
public int getSize() {
return pool.size();
}
//从池中返回一个数据库连接
@Override
public Connection getConnection() {
if(pool.size() > 0) {
//从池中获取数据库连接
return pool.remove(0);
}else {
throw new RuntimeException(“连接数量已用尽”);
}
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
@Override
public T unwrap(Class iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
自定义连接池的测试
=========
public class MyDataSourceTest {
public static void main(String[] args) throws SQLException {
MyDataSource dataSource = new MyDataSource();
System.out.println(“使用前连接池数量:” + dataSource.getSize());
Connection con = dataSource.getConnection();
String sql = “select * from emp”;
PreparedStatement pst = con.prepareStatement(sql);
ResultSet res = pst.executeQuery();
while (res.next()) {
String ename = res.getString(“ename”);
String job = res.getString(“job”);
String hiredate = res.getString(“hiredate”);
System.out.println(“ename:” + ename + “\t job:” + job + “\t hiredate:” + hiredate);
}
res.close();
pst.close();
con.close();
System.out.println(“使用后连接池数量:” + dataSource.getSize());
}
}
输出:
使用前连接池数量:10
ename:SMITH job:CLERK hiredate:1980-12-17
ename:ALLEN job:SALESMAN hiredate:1981-02-20