データベース接続プール
データベース接続プールとは
データベース接続プールは、データベース接続リソースを格納するコンテナです。ユーザーがデータベース接続リソースを必要とする場合、コンテナから直接リソースを取得できます。データベース接続リソースが使用された後、プログラムはリソースをデータベース接続プールに直接返すことができます。一般的な原則は次のとおりです。
データベース接続プールが必要な理由
従来のデータベース操作方法でデータベースを操作する場合、データベース接続リソースアプリケーションとリリースコードを繰り返し実行する必要があり、データベース操作の効率が低下します。データベース操作の効率を上げるために、繰り返しを避けてください。アプリケーションとデータベース接続リソースの解放、ユーザーアクセス効率の向上、システムリソースの節約。
オペレーションデータベース接続プールの使用方法
成し遂げる
- 標準インターフェース:DataSource javax.sqlパッケージ
メソッド
接続の取得:getConnection()
接続の解放:Connection.close()
注:接続オブジェクトConnectionが接続プールから取得された場合、Connection.close()メソッドを呼び出します。接続は再び閉じられますが、接続は返されます。
共通データベース接続プール
c3p0
c3p0データベースを使用する方法は2つあります。1つはハードコーディングを使用する方法です。通常、この方法では使用量を減らします。通常、構成ファイルを使用してデータベース接続プールを実装します。
ハードコードされている
c3p0のデータシートによると、基本的な使い方を教えてください。
ケース(ハードコードされた方法)
ハードコーディングを使用するには、c3p0を使用します
public class C3P001Demo {
public static void main(String[] args) throws PropertyVetoException, SQLException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql:///test2");
ds.setUser("root");
ds.setPassword("root");
Connection connection = ds.getConnection();
String sql = "select * from account where username = ?;";
PreparedStatement ppst = connection.prepareStatement(sql);
ppst.setString(1,"zhangsan");
ResultSet resultSet = ppst.executeQuery();
System.out.println("----------------------------");
if(resultSet.next()){
System.out.println(resultSet.getInt("id")+"------>"
+resultSet.getString("username")+"--------->"+
resultSet.getDouble("balance")+"---------->"+
resultSet.getString("password")
);
}
System.out.println("----------------------------");
resultSet.close();
ppst.close();
connection.close();
}
}
ハードコードされた実行結果:
他にもハードコードされた方法があるため、ここではあまり説明しません。
構成ファイルを採用する
xml構成ファイルを使用します。
c3p0データマニュアルで推奨されているデータ構成ファイル形式。
xmlファイル
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test2</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--超时时间-->
<property name="checkoutTimeout">30</property>
</default-config>
<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test2</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
c3p0は、複数のデータベース接続プールを実装するために同じファイルのセットをサポートしています。複数の接続プールを実装する方法は、次のようなデータソースを作成するときに値を直接割り当てることです。
ComboPooledDataSource ds = new ComboPooledDataSource("otherc3p0");
このように、データベース接続プールはotherc3p0を介して構成されます。
場合
xmlファイルを使用してデータベースを構成します。
public class C3P002Demo {
public static void main(String[] args) throws SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource("otherc3p0");
Connection connection = cpds.getConnection();
System.out.println(connection);
String sql = "select * from account where username = ?;";
PreparedStatement ppst = connection.prepareStatement(sql);
ppst.setString(1,"zhangsan");
ResultSet resultSet = ppst.executeQuery();
System.out.println("----------------------------");
if(resultSet.next()){
System.out.println(resultSet.getInt("id")+"------>"
+resultSet.getString("username")+"--------->"+
resultSet.getDouble("balance")+"---------->"+
resultSet.getString("password")
);
}
System.out.println("----------------------------");
resultSet.close();
ppst.close();
connection.close();
}
}
运行结果
C3P0采用性質配置文件的方式与XML文件类似、以下是数据手册中实现对C3P0的操作说明。
、ライブラリの組み込みのデフォルトを上書きc3p0.propertiesというファイルを作成し、「ルート」でそれを配置するにはクラスパスまたはクラスローダーの 一般的なスタンドアロンアプリケーションの場合、これは、CLASSPATH環境変数で指定されたディレクトリにファイルを配置することを意味します。一般的なWebアプリケーションの場合、ファイルはWEB-INF / classesに配置する必要があります。一般に、ファイルは、c3p0のjarファイルをロードしたクラスローダーで、/ c3p0.propertiesという名前のクラスローダーリソースとして使用可能である必要があります。これがよくわからない場合は、java.lang.Class、java.lang.ClassLoader、およびjava.util.ResourceBundleのAPIドキュメント(特にgetResource…メソッド)を確認してください。
c3p0.propertiesの形式は、キーがc3p0構成可能プロパティである通常のJavaプロパティファイル形式である必要があります。詳細については、付録Aを参照してください。c3p0.propertiesファイルの例を以下に示します。これ
は、で指定された方法で実現できます。 c3p0データベース接続プールを実現するための公式Webサイトドキュメントプロパティファイルなので、ここでは詳しく説明しません。
ドルイド
druidは、Alibabaによって開発されたオープンソースデータベースです。現在、データベースは600を超えるAPPに正常に適用されています。長期的な実践の後、データベース接続プールは安定したパフォーマンスと高効率を備えており、多くのJavaデータベースのパフォーマンスです。接続プール。最も優れたデータベースの1つ。以下を例と組み合わせて、druidデータベース接続プール関連の操作を完了します。
場合
資源
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test2
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
ツール
public class JDBCUtils {
private static DataSource dataSource;
static {
try {
InputStream res = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
Properties properties = new Properties();
properties.load(res);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
public static void close(Statement statement, ResultSet resultSet,Connection connection){
if (statement!=null){
try {
statement.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
if(connection !=null){
try {
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}
public static void close(Statement statement, Connection connection){
if(connection !=null){
try {
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
if(connection !=null){
try {
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
テストクラス
public class Druid01Demo {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
System.out.println(connection);
String sql = "select * from account where username = ? ";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"lisi");
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
System.out.println(resultSet.getInt("id")+"------>"
+resultSet.getString("username")+"--------->"+
resultSet.getDouble("balance")+"---------->"+
resultSet.getString("password"));
}
connection = JDBCUtils.getConnection();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}finally {
JDBCUtils.close(preparedStatement,resultSet,connection);
}
}
}
試験結果:
上記のケースでは、データベース接続プールに適用するためのコードとリソースをツールクラスに解放するためのコードが抽象化されているため、コードの繰り返しの記述が回避され、コード開発が簡素化されます。
springJDBC
springJDBCは、SpringフレームワークによるJDBCの単純なカプセル化です。Springフレームワークは、jdbc開発を簡素化するためのJDBCTemplateを提供します。
一般的なjdbcTemplate操作データベースメソッド
DMLステートメントの実装方法
- 更新():。文の追加、削除、変更
DQLステートメントの実装方法
- 1. queryForMap():クエリ結果は結果セットをマップコレクションにカプセル化し、列名はキーとして使用され、値はこのレコードをマップセットにカプセル化するための値として使用されます
。2。queryForList():クエリ結果は、結果セットをリストコレクション
にカプセル化します。3。queryForObject:クエリ結果、結果をオブジェクトとしてカプセル化します。通常、集計関数のクエリに使用されます。
4. query():結果をクエリし、結果をJavaBeanオブジェクト
クエリパラメータとしてカプセル化します。RowMapperは
通常、BeanPropertyRowMapper実装クラスを使用します。JavaBeanへのデータの自動カプセル化を完了できます
newBeanPropertyRowMapper <type>(type.class)
ノート:
- 1. queryForMapクエリの結果セットの長さは1のみです
。2。QueryForList()は、各レコードをMapコレクションとしてカプセル化し、MapコレクションをListコレクションにロードします。
場合
jdbcTemplateを使用してデータベースに接続し、追加、削除、変更、およびチェックします
public class JDBCTemplate01Demo {
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
private String querySql = "select * from account";
@Test
public void test(){
queryListResultPrint(jdbcTemplate.queryForList(querySql));
}
@Test
public void test1(){
String sql = "update account set balance =1000";
queryListResultPrint(jdbcTemplate.queryForList(querySql));
System.out.println("---------DML语句执行前------------");
jdbcTemplate.update(sql);
System.out.println("---------DML语句执行后------------");
queryListResultPrint(jdbcTemplate.queryForList(querySql));
}
@Test
public void test2(){
String sql = "update account set balance = 500 where username = ?";
queryListResultPrint(jdbcTemplate.queryForList(querySql));
System.out.println("---------DML语句执行前------------");
jdbcTemplate.update(sql,"zhangsan");
System.out.println("---------DML语句执行后------------");
queryListResultPrint(jdbcTemplate.queryForList(querySql));
}
@Test
public void test3(){
String sql = "insert into account values(?,?,?,?)";
queryListResultPrint(jdbcTemplate.queryForList(querySql));
System.out.println("---------DML语句执行前------------");
jdbcTemplate.update(sql,3,"wangwu",2000,"wangwu");
System.out.println("---------DML语句执行后------------");
queryListResultPrint(jdbcTemplate.queryForList(querySql));
}
@Test
public void test4(){
String sql = "DELETE from account where username = ? ";
queryListResultPrint(jdbcTemplate.queryForList(querySql));
System.out.println("---------DML语句执行前------------");
jdbcTemplate.update(sql,"wangwu");
System.out.println("---------DML语句执行后------------");
queryListResultPrint(jdbcTemplate.queryForList(querySql));
}
@Test
public void test5(){
String querySql = "select * from account where username = ?";
Map<String, Object> zhangsan = jdbcTemplate.queryForMap(querySql, "zhangsan");
System.out.println(zhangsan);
}
@Test
public void test6(){
List<Emp> query = jdbcTemplate.query(querySql, new RowMapper<Emp>() {
@Override
public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
Emp emp = new Emp();
int id = resultSet.getInt("id");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
double balance = resultSet.getDouble("balance");
emp.setBalance(balance);
emp.setId(id);
emp.setUsername(username);
emp.setPassword(password);
return emp;
}
});
for (Emp emp : query) {
System.out.println(emp.getId()+"---->"+emp.getUsername()+"--->"+emp.getPassword()+"--->"
+emp.getBalance());
}
}
@Test
public void test7(){
List<Emp> query = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<Emp>(Emp.class));
for (Emp emp : query) {
System.out.println(emp.getId()+"---->"+emp.getUsername()+"--->"+emp.getPassword()+"--->"
+emp.getBalance());
}
}
@Test
public void test8(){
String sql = "select count(id) from account;";
Long aLong = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(aLong);
}
public void queryListResultPrint(List<Map<String, Object>> maps){
for (Map<?, ?> map :
maps) {
Set<Object> strings = (Set<Object>) map.keySet();
for (Object val :
strings) {
System.out.println(val+"---------->"+map.get(val));
}
/*Set<String> strings = maps.keySet();
System.out.println(i+"---------->"+maps.get(i));*/
}
}
}
次の図に示すように、jarパッケージをインポートする必要があります。
実行結果が多すぎるため、ここではあまり多くのデモンストレーションを行いません。興味のある子供は自分でテストできます。