jdbc连接异常

记录一次jdbc连接池异常

异常原因:jdbc创建连接时出现以下异常
jdbc连接异常_第1张图片
经排查发现是依赖冲突引起,排除以下依赖即可

        clickhouse-native-jdbc
       com.github.housepower

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) {
            }
        }

    }

}

你可能感兴趣的:(数据库,java,sql)