jdbc连接异常

记录一次jdbc连接池异常

异常原因:jdbc创建连接时出现以下异常
在这里插入图片描述
经排查发现是依赖冲突引起,排除以下依赖即可

        <artifactId>clickhouse-native-jdbc</artifactId>
       <groupId>com.github.housepower</groupId>
jdbcUtil
package com.singhand.tianwang.config;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 使用JDBC工具类,获取数据库的连接 采用读取配置文件的方式 读取配置文件,获取连接,执行一次,static{}
 */
public class JDBCUtilsConfig {

    private static Connection con;

    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;

    static {
        try {
            readConfig();
            Class.forName(driverClass);

        } catch (Exception e) {
            throw new RuntimeException("数据库连接失败");
        }
    }

    /**
     * @Title: readConfig
     * @Description:读取配置文件
     * @throws IOException
     */
    private static void readConfig() throws IOException {

        InputStream is = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pro = new Properties();
        pro.load(is);
        driverClass = pro.getProperty("driverClass");
        url = pro.getProperty("url");
        user = pro.getProperty("user");
        password = pro.getProperty("password");
    }

    public static Connection getConnection() {
        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void close(Connection con, Statement stat) {

        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException ex) {
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
            }
        }

    }

    public static void close(Connection con, Statement stat, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
            }
        }

        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException ex) {
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
            }
        }

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值