错误名称:连接关闭后不允许操作
无法使用调用的关闭资源,暂时把关闭资源方法注掉了
求大佬指导:
DBUtil代码如下
package cn.berchina.crm.util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.*;
import java.util.*;
public class DBUtil {
//druid 连接池
// private static DruidDataSource druidDataSource = null;
private static ComboPooledDataSource dataSource = null;
private static Connection connection = null;
private static PreparedStatement preparedStatement = null;
private static ResultSet resultSet = null;
static {
try {
dataSource = new ComboPooledDataSource();
// druidDataSource = new DruidDataSource();
// InputStream in = ClassLoader.getSystemResourceAsStream("db.properties");
// Properties properties = new Properties();
// properties.load(in);
// druidDataSource.setUrl(properties.getProperty("url"));
// druidDataSource.setUsername(properties.getProperty("user"));
// druidDataSource.setPassword(properties.getProperty("password"));
// druidDataSource.setUrl("jdbc:mysql://localhost:3306/crm?serverTimezone=Asia/Shanghai");
// druidDataSource.setUsername("root");
// druidDataSource.setPassword("123456");
// connection= druidDataSource.getConnection();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/crm?serverTimezone=Asia/Shanghai");
dataSource.setUser("root");
dataSource.setPassword("123456");
connection=dataSource.getConnection();
// Class.forName("com.mysql.jdbc.Driver");
// Class.forName("com.mysql.cj.jdbc.Driver");
// connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/crm?serverTimezone=Asia/Shanghai","root","123456");
} catch (Exception e){
e.printStackTrace();
}
}
/**
* @param sql sql语句
* @param obj Object数组存储占位符内容
* @return 影响的数据条数
* 添加、修改、删除一条或多条数据
*/
public static int update(String sql, Object [] obj){
int update = 0;
try {
//获取连接
// connection = druidDataSource.getConnection();
preparedStatement = connection.prepareStatement(sql);
if(obj.length > 0){
for(int i = 0; i<obj.length; i++) {
preparedStatement.setObject(i+1,obj[i]);
}
}
update = preparedStatement.executeUpdate();
return update;
} catch (SQLException e) {
e.printStackTrace();
}
return update;
}
/**
* @param sql sql语句
* @param obj 条件数组
* @return 查询到的数据结果集
* 根据条件查询一条数据
*/
public static ResultSet select(String sql,Object [] obj){
try {
// connection = druidDataSource.getConnection();
preparedStatement = connection.prepareStatement(sql);
if(obj.length > 0){
for(int i = 0; i<obj.length; i++) {
preparedStatement.setObject(i+1,obj[i]);
}
}
resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static List selectAll(String sql , Object[] obj){
List list = new ArrayList();
try {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i <obj.length; i++) {
preparedStatement.setObject(i+1,obj[i]);
}
resultSet=preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next())
{
Map map =new HashMap();
for (int i = 1; i <metaData.getColumnCount() ; i++) {
map.put(metaData.getColumnName(i),resultSet.getString(i));
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
// close();
}
return list;
}
/**
* 释放资源
*/
public static void close(){
try{
if(resultSet != null){
resultSet.close();
}
if(preparedStatement != null){
preparedStatement.close();
}
if(connection != null){
connection.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021012421450326.png)