使用Druid连接数据库

配置文件

使用properties文件配置相关数据

--driverClassName= com.mysql.cj.jdbc.Driver            驱动加载

--username=root                       连接数据库的用户名

--password=                         连接数据库的密码

--url=jdbc:mysql://127.0.0.1:3306/库名?characterEncoding=utf-8  注册驱动

--initialSize=                         初始化池中建立的物理连接个数

--maxActive=                         最大可活跃连接池的数量

导包

在lib目录下导入包:

druid-1.1.5.jar mysql-connector-java-5.1.41-bin.jar

数据库连接

复制代码
private static DataSource source;
    static {
        try {
            Properties properties = new Properties();

            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("Druid.properties");
            properties.load(is);
            source = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        Connection connection = source.getConnection();
        return connection;

    }
复制代码
原文地址:https://www.cnblogs.com/lmygq1728/p/14160807.html