//Jdbc工具类
public class JdbcUtils {
private static DruidDataSource dataSource;
static {
try {
Properties properties = new Properties();
//读取properties文件
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("com/jdbc.properties");
properties.load(inputStream);
//创建数据库连接池
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
System.out.println(dataSource.getConnection());
} catch (Exception e) {
e.printStackTrace();
}
}
//获取数据库连接池
public static Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭连接
public static void close(Connection conn) {
if(conn !=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
JdbcUtils工具类,采用Druid连接池
最新推荐文章于 2023-02-21 21:57:49 发布