文件结构
配置信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybank?useUnicode=true&characterEncoding=utf8
username=root
password=root123
initialSize=3
maxActive=7
maxWait=3000
连接工具类
package tools;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class DbUtils {
// 变量的定义
// 集合
private static Properties properties = new Properties();
// 连接池
private static DataSource ds;
// 静态代码块
static {
// 获取配置流文件
InputStream resourceAsStream = DbUtils.class.getClassLoader().getResourceAsStream("tools/base.properties");
try {
// 集合加载配置
properties.load(resourceAsStream);
// 给变量ds重新赋值
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取连接池
public static DataSource getDs() {
return ds;
}
// 获取连接对象
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
// 归还连接对象
public static void close(Connection connection) {
if (connection == null) {
return;
}
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
测试类
import tools.DbUtils;
import javax.sql.DataSource;
import java.sql.Connection;
public class Test {
public static void main(String[] args) {
// 获取连接池
DataSource ds = DbUtils.getDs();
System.out.println(ds);
// 获取连接对象
Connection connection = DbUtils.getConnection();
System.out.println(connection);
// 归还连接对象
DbUtils.close(connection);
System.out.println("game over");
}
}
最终效果
"C:\Program Files\Java\jdk1.8.0_172\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=65300:C:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_172\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\Program_java\untitled30\out\production\untitled30;D:\Program_java\untitled30\lib\druid-1.0.9.jar;D:\Program_java\untitled30\lib\mysql-connector-java-5.1.0-bin.jar" Test
{
CreateTime:"2023-03-16 23:49:56",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
三月 16, 2023 11:49:56 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@e73f9ac
game over
Process finished with exit code 0