The most detailed explanation of the Druid database connection pool connection method

directory :

Method 1: Use the druid factory to initialize the connection pool

Method 2: Create a druidDatasource first, and then manually complete the initialization of the data source

Test Results:

Properties file:

Details that need attention (emphasis):


Method 1: Use the druid factory to initialize the connection pool

Specific steps:

  • Import the druid jar package
  • Import mysql connection driver
  • Complete the initialization of the data source in the code block
public class Utils { 
    private static DataSource dataSource; 
    static { 
        //Method 1: Create a data source through the druid factory 
        //Create an input stream 
        InputStream stream = Utils.class.getClassLoader().getResourceAsStream("jdbc.properties"); 
        Properties properties = new Properties(); 
        try { 
            //Load the information in the configuration file into properties 
            properties.load(stream); 
            //Create a druid data connection pool and complete the initialization of the data pool 
            dataSource = DruidDataSourceFactory.createDataSource(properties); 
        } catch (IOException e) { 
            e. printStackTrace(); 
        } catch (Exception e) { 
            e. printStackTrace() 
        ; 
    }
    public static Connection getConnection() { 
        try { 
            return dataSource.getConnection(); 
        } catch (SQLException e) { 
            //If the connection is abnormal, throw a runtime exception 
            throw new RuntimeException("The connection pool has an exception."); 
        } 
    } 
}

Method 2: Create a druidDatasource first, and then manually complete the initialization of the data source

public class Utils { 

    private static DruidDataSource druidDataSource; 
    static { 
        try { 
        //Method 2: By creating a druidDatasource, manually complete the initialization of the data source later 
            druidDataSource=new DruidDataSource(); 
            druidDataSource.setDriverClassName("com.mysql.cj.jdbc. Driver"); 
            druidDataSource.setUrl("jdbc:mysql://localhost:3306/bookdb?serverTimezone=UTC"); 
            druidDataSource.setUsername("root"); 
            druidDataSource.setPassword("hjl123"); 
            //Optional setting 
            druidDataSource.setMaxActive(20); 
            druidDataSource.setInitialSize(10); 
            druidDataSource.setMaxWait(5000);


        } catch (Exception e) {
            e.printStackTrace(); 
        } 
    } 

    public static Connection getConnection2() { 
        try { 
            return druidDataSource.getConnection(); 
        } catch (SQLException e) { 
            //If the connection is abnormal, throw a runtime exception 
            throw new RuntimeException(" Connection pool exception."); 
        } 
    } 
}

Test Results:

Properties file:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/bookdb?serverTimezone=UTC
username=root
password=hjl123
initialSize=10
maxActive=15
minIdle=5
maxWait=5000

Details that need attention (emphasis):

Details 1:

  • In the mysql driver imported in the second step, you need to select it according to your own database version. Currently there are two mainstream versions, one is about 5.7 and the other is about 8.0 . My own mysql database is version 8.0, so I need to use a mysql data driver above 8.0. If the version is wrong, an error will be reported

Details 2:

  • Depending on the driver, the values ​​corresponding to the driverClassName and url attributes are also different.

    Version 5.7 is as follows:

   driverClassName=com.mysql.jdbc.Driver

  //characterEncoding=utf8 The database is connected to transmit UTF8 data this time, the project is UTF8 and the database is other encodings

  url=jdbc:mysql://localhost:3306/books? characterEncoding=utf8

    The 8.0 version is as follows:

                driverClassName=com.mysql.cj.jdbc.Driver

                //serverTimezone=UTC must be added

                url=jdbc:mysql://localhost:3306/bookdb?serverTimezone=UTC

Details 3:

  • After mysql driver 5.1.6, there is no need for Class.forName(“com.mysql.cj.jdbc.Driver”); for the attribute driverClassName, it can be omitted. The bottom layer will automatically complete the driver registration according to the driver version.

Detail 4: When using the druid factory mode and reading the corresponding properties through the properties file, the property names are fixed and must be consistent (must pay attention!!!!!!!!!). And if you use the second one, you can name it more according to your preferences.

Finally: If there is something wrong, please advise!

Guess you like

Origin blog.csdn.net/weixin_45533131/article/details/126810974