データベース接続プール-Druidツールクラスのコンパイルとチューニングおよび使用例(超完全なコメントは理解できないことを恐れています)

1.データベース接続プール

これは実際にはデータベース接続を格納するために使用されるコンテナ(コレクション)です。システムが初期化されると、このコンテナが作成され、このコンテナは一部の接続オブジェクトに適用されます(適用される接続オブジェクトの数は構成で変更できます)ファイル)、ユーザーがデータベースにアクセスすると、接続オブジェクトはコンテナから直接取得されます。ユーザーアクセスが完了すると、接続オブジェクトはコンテナに戻され、再利用が可能になります(従来の方法は接続オブジェクトを作成することです)。使用する場合は、使用終了後に破棄を使用するため、効率が高くありません)

  • データベース接続プールを使用する利点は次のとおりです。
    1. リソースを節約する
    2. データベースにアクセスするときの効率が向上します

2.Druidデータベース接続プールを使用する手順

  1. jarパッケージをインポートします

  2. 構成ファイルを定義します。

    • 構成ファイルは次のとおりです。*。properties、名前は無制限ですが、.propertiesで終わる必要があります。
  3. コードを書く:

    1. 構成ファイルをロードします
    2. 接続プールオブジェクトを取得します
    3. 接続を取得します

3.使用例(メモは非常に詳細です!!!)

1.druid.properties構成ファイル

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8
username=root
password=itcast
# 初始化的连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

2.ドルイド接続プールツールクラス-JDBCUtils

/**
* Druid连接池的工具类
 */

public class JDBCUtils {

// 1. 定义一个成员变量 DataSource
private static DataSource dataSource;

static {

    try {
        // 1. 加载配置文件
        Properties properties = new Properties();
        properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
        // 2. 获取DataSource
        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

/**
 * 获取连接的方法
 */
public static Connection getConnection() throws SQLException {
    // 从连接池中取一个连接对象
    return dataSource.getConnection();
}


/**
 *  释放资源
 *  执行DML语句的时候需要关闭 statement 和 connection
 * @param statement
 * @param connection
 */
public static void close(Statement statement , Connection connection){
    if(statement != null){
        try {
            statement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    if(connection != null){
        try {
            connection.close();      // 归还到连接池中
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}

/**
 * 释放资源
 * 执行DQL语句的时候需要关闭 resultSet statement 和 connection
 * @param resultSet
 * @param statement
 * @param connection
 */
public static void close(ResultSet resultSet,Statement statement , Connection connection){
    if(resultSet != null){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

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

    if(connection != null){
        try {
            connection.close();      // 归还到连接池中
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }*/
	// 我们发现上面两个关闭的过程和DML关闭的过程一样,所以为了代码的简洁可以直接调用方法关闭
    close(statement,connection);
}

/**
 * 获取连接池的方法
 */
public static DataSource getDataSource(){
    return dataSource;
}
}

3.Druid接続プールツールの使用例

/**
 * 使用新的工具类
 */

public class DruidUtilsDemo {
	public static void main(String[] args) {
    /**
     * 完成添加的操作 给 accout 表添加一条记录
     */
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        // 1. 获取连接
        connection = JDBCUtils.getConnection();
        // 2. 定义sql
        String sql = "insert into account value(null,?,?)";
        // 3. 获取
        preparedStatement = connection.prepareStatement(sql);
        // 4. 给?赋值
        preparedStatement.setString(1,"小白白");
        preparedStatement.setDouble(2,3000);
		// 执行sql,返回值是影响的行数
        int count = preparedStatement.executeUpdate();
        System.out.println(count);
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        // 6. 释放资源
        JDBCUtils.close(preparedStatement,connection);
    }
	}
}

理解できない場合は、他のブログを参照して、JDBCUtilsとログインケースを完成させ、SQLインジェクションの問題を解決することをお勧めします最も基本的なJDBCツールクラスの準備は、独立して考え、JDBCUtilsクラスの準備を独立して完了するためのガイドです。

おすすめ

転載: blog.csdn.net/qq_45796486/article/details/114640043