首页 > 其他分享 >037.Druid

037.Druid

时间:2022-09-23 00:01:20浏览次数:33  
标签:java druid Druid 037 sql import com properties

1.首先在创建druid.properties文件

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/lesson11?serverTimezone=UTC
username=root
password=******
initialSize=5
#初始化连接数
maxActive=10
#最大连接数
maxWait=3000
#连接最大等待时间 3000 ms

2.创建JdbcUtil类

package cn.yiliedu.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtilNew
{

   private static DataSource dataSource = null;

    static
    {
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Properties properties = new Properties();
            /*
             * 1.JdbcUtilNew.class  返回JdbcUtilNew类的一个对象
             * 2.getClassLoader 类加载器,在程序运行的初始阶段加载一些Java类
             * 3.getResourceAsStream()  从文件中读入流
             */
            InputStream inputStream =  JdbcUtilNew.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(inputStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public static Connection getConnection() throws SQLException
    {
        return dataSource.getConnection();
    }




}

3.步骤

 

 

 

 

 

 4.一些代码的注释

 

标签:java,druid,Druid,037,sql,import,com,properties
From: https://www.cnblogs.com/LLL0617/p/16721278.html

相关文章