zoukankan      html  css  js  c++  java
  • 使用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;
    
        }
  • 相关阅读:
    Python Day14
    Python Day13
    Python Day12
    Python Day11
    Python Day10
    Python Day9
    Python Day8
    Python Day7
    Python Day6
    Python Day5
  • 原文地址:https://www.cnblogs.com/Gazikel/p/14021757.html
Copyright © 2011-2022 走看看