データベース接続プール、DBUTILテンプレート、DBCP、C3P0

データベース接続プール、DBUTILテンプレート、ドルイド使用(キー)

A、DBUTILテンプレート

public class DBUtilTest {

    public static Connection connection;
    public static final String URL = "jdbc:mysql:///demo";
    public static final String USERNAME = "";
    public static final String PASSWORD = "";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void testQuery() throws SQLException {
        connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        String sql = "select * from emp where id = ?";
        QueryRunner runner = new QueryRunner();
        Emp query = runner.query(connection, sql, new BeanHandler<Emp>(Emp.class), 1);
        System.out.println(query);
    }

    public static void testQuery2() throws SQLException {
        connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        String sql = "select * from emp";
        QueryRunner runner = new QueryRunner();
        List<Emp> query = runner.query(connection, sql, new BeanListHandler<Emp>(Emp.class));
        query.forEach((x)->{
            System.out.println(x);
        });
    }

    public static void insert() throws SQLException {
        connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        String sql = "insert emp values(null,?,?)";
        QueryRunner runner = new QueryRunner();
        runner.update(connection,sql,1,2);
    }
    public static void main(String[] args) throws SQLException {
        testQuery2();
    }
}

第二に、データベース接続プール

データベース接続プールは、頻繁な切り替え接続の時間を短縮することで、それはいくつかのプロパティの分析を持つべきです

1.初期サイズ

2.各拡張サイズ

3.接続プールの最大数

死の4アイドル接続時間

データベース接続プールの様々な

  • DBCP
  • C3P0
  • ドルイド
  • hikariCP
2.1.DBCPテンプレート
public static void main(String[] args) throws SQLException {
        // 数据库连接池资源
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        basicDataSource.setUrl("jdbc:mysql:///demo");
        basicDataSource.setUsername("");
        basicDataSource.setPassword("");

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;

        connection = basicDataSource.getConnection();
        String sql = "select * from emp";
        preparedStatement = connection.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        while (rs.next()){
            System.out.println(rs.getInt(1)+
                    rs.getString(2)+
                    rs.getString(3));
        }
        connection.close();
    }
2.2.C3P0
public static void main(String[] args) throws PropertyVetoException, SQLException {
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver
    cpds.setJdbcUrl( "jdbc:mysql://120.78.206.78:3306/demo" );
    cpds.setUser("root");
    cpds.setPassword("Root123..");
    cpds.setMaxStatements( 180 );
    Connection conn = cpds.getConnection();
    System.out.println(conn);
    conn.close();
}

公式サイトへの第二の基準、XML設定

2.3.druid

最高のパフォーマンスデータベース接続プールをドルイド

DBCPとC3P0違い、C3P0は自動的に未使用の接続を再利用します

おすすめ

転載: www.cnblogs.com/littlepage/p/11876764.html