阿里druid数据库连接及配置文件

public class JDBCDruid {
     
    private static Properties properties;
    private static  JDBCDruid druid;
    private static DruidDataSource ds;
     
    //静态代码块加载配置文件 
    static {
        properties=new Properties();
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 创建单列模式
     * @return JDBCDruid实例
     */
    public static synchronized JDBCDruid getInstance() {
        if(druid==null) {
            druid=new JDBCDruid();
            return druid;
        }
        return druid;
    }
     
    private JDBCDruid() {
 
        ds=new DruidDataSource();
        ds.setDriverClassName(properties.getProperty("driverClassName"));
        ds.setUrl(properties.getProperty("url"));
        ds.setUsername(properties.getProperty("username"));
        ds.setPassword(properties.getProperty("password"));
        ds.setMaxActive(Integer.parseInt(properties.getProperty("maxActive")));
         
         
    }
     
     
      public  Connection getConnection() throws SQLException {
          Connection connection = ds.getConnection();
          return connection;
      }
      
 
}

下面是druid.properties文件的配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybase
username=root
password=abc123
initialSize=5
maxActive=10
maxWait=3000
maxIdle=8
minIdle=3

拿去白嫖吧,记得点个赞哦 谢谢!

你可能感兴趣的:(java)