Use of Druid Database Connection Pool

Use of Druid Database Connection Pool

I. Basic use of Druid_

Druid: database connection pool implementation technology, provided by

Steps:

1. Import the jar package druid-1.0.9.jar

* Don't forget to import database-driven jar packages

2. Define the configuration file:

* It's in the form of properties.

* Can be called any name, can be placed in any directory, recommended in the src directory

3. Loading configuration files

load() method of Properties class

4. Getting database connection pool objects: Getting them through factories

DruidDataSourceFactory.createDataSource(Properties)

Factory Design Model

5. Get the connection:

getConnection()

src/druid.properties file:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db3
username=root
password=root
# Number of Initialized Connections
initialSize=5
# maximum connection
maxActive=10
# Maximum waiting time
maxWait=3000
/**
 * Druid Demonstration
 */
public class DruidDemo {
    public static void main(String[] args) throws Exception {
        //1. Importing jar packages
        //2. Define configuration files
        //3. Loading configuration files
        Properties pro = new Properties();
        InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);
        //4. Get connection pool objects
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        //5. Getting Connections
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
}

II. Druid_Tool Class

/**
 * Druid Tool classes for connection pools
 */
public class JDBCUtils {

    //1. Define member variable DataSource
    private static DataSource ds ;

    static{
        try {
            //1. Loading configuration files
            Properties pro = new Properties();
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //2. Get DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Get connection
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    /**
     * Release resources
     */
    public static void close(Statement stmt,Connection conn){
       /* if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();//Return connection
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }*/

       close(null,stmt,conn);
    }


    public static void close(ResultSet rs , Statement stmt, Connection conn){


        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();//Return connection
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Getting Connection Pool Method
     */

    public static DataSource getDataSource(){
        return  ds;
    }

}

Tool class features:

  1. Arrays/Collections/Math
  2. Privatization of construction methods
  3. Membership methods are modified with static

Be careful:

Under what circumstances do we want throws exception, and under what circumstances do we want try..catch exception?

In the 1 static block, only try...catch exceptions are allowed.

2. When throws and tries. catches can be used in the method, the principles are as follows:

* If you want the caller to know and handle the exception, use throws.

* If this exception is not serious, it can be handled by itself, without affecting the whole code, we can choose try...catch.

In this case, the call does not need to handle exceptions, and the caller's code is clear.

3. Druid_Tool Class Testing

/**
 * Use new tool classes
 */
public class DruidDemo2 {

    public static void main(String[] args) {
        /*
         * Complete the add: Add a record to the account table
         */
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //1. Get connection
            conn = JDBCUtils.getConnection();
            //2. definition of sql
            String sql = "insert into account values(null,?,?)";
            //3. Get the pstmt object
            pstmt = conn.prepareStatement(sql);
            //4.? assignment
            pstmt.setString(1,"Wang Wu");
            pstmt.setDouble(2,3000);
            //5. implementation of sql
            int count = pstmt.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6. Releasing resources
            JDBCUtils.close(pstmt,conn);
        }
    }

}

 

 

Tags: Druid Database SQL MySQL

Posted on Mon, 07 Oct 2019 04:39:35 -0700 by emdee