当前位置:   article > 正文

JavaWeb错误java.sql.SQLNonTransientConnectionException: No operations allowed after connection closed.

java.sql.sqlnontransientconnectionexception: no operations allowed after con

在这里插入图片描述
错误名称:连接关闭后不允许操作
无法使用调用的关闭资源,暂时把关闭资源方法注掉了
求大佬指导:
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();
        }
    }
}





  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021012421450326.png)

  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/591127
推荐阅读
相关标签
  

闽ICP备14008679号