一.Druid连接池
1.中文名:德鲁伊
2.作用: 数据库连接重复利用,提升响应速度
3.用法步骤
1> 导入jar包
2>配置文件
3>工具类DruidUtils
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
public class DruidUtils {
private DruidUtils() {
};
private static DruidDataSource ds;
static {
try {
Properties pr = new Properties();
InputStream in = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pr.load(in);
// 数据源工厂获取数据源
ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(pr);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
conn = ds.getConnection();
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//获取数据源
public static DruidDataSource getDataSource() {
return ds;
}
//释放资源
public static void close(Connection con , PreparedStatement statement , ResultSet set) {
if(set != null) {
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();