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

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed是一个数据库连接错误,它表示在连接MySQL数据库时,公钥检索被禁止。 这个错误通常在使用MySQL 8.x版本时出现。产生这个错误的原因是因为MySQL 8.x版本默认使用了新的身份验证插件caching_sha2_password,而旧版本的MySQL JDBC驱动程序不支持这个插件。 解决这个问题的方法是更新MySQL JDBC驱动程序到最新的版本,或者在连接字符串中明确指定使用旧版的身份验证插件mysql_native_password。具体步骤如下: 1. 首先,确保你的项目中使用的是最新版本的MySQL JDBC驱动程序。 2. 如果你使用的是Maven或Gradle管理项目依赖,可以在pom.xml或build.gradle文件中添加相应的依赖项来引入最新版本的MySQL JDBC驱动程序。 3. 如果你的项目没有使用构建工具,可以手动下载最新版本的MySQL JDBC驱动程序,并将其添加到项目的类路径中。 4. 在连接数据库的代码中,修改连接字符串,将使用的身份验证插件指定为mysql_native_password。例如: ```java String url = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false"; Properties info = new Properties(); info.setProperty("user", "myuser"); info.setProperty("password", "mypassword"); info.setProperty("useSSL", "false"); info.setProperty("allowPublicKeyRetrieval", "true"); info.setProperty("useSSL", "false"); info.setProperty("characterEncoding", "utf-8"); info.setProperty("serverTimezone", "GMT+8"); info.setProperty("autoReconnect", "true"); info.setProperty("useSSL", "false"); info.setProperty("allowPublicKeyRetrieval", "true"); Connection conn = DriverManager.getConnection(url, info); ``` 在连接字符串中加入allowPublicKeyRetrieval=true参数即可解决这个问题。 通过更新MySQL JDBC驱动程序版本或者指定使用旧版身份验证插件,你应该能够成功解决sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed](https://blog.csdn.net/qq_47433566/article/details/123581123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [JDBC 报错 java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed](https://blog.csdn.net/weixin_30298497/article/details/101414048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [基于JavaWeb学生宿舍管理系统毕业设计项目源码(完美注释+高分必看).zip](https://download.csdn.net/download/weixin_55305220/85624213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值