日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java文档阅读笔记-C3P0连接池的使用

發(fā)布時間:2025/3/15 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java文档阅读笔记-C3P0连接池的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這篇博文如何在應用程序中使用和配置C3P0

prom.xml如下:

<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.5</version> </dependency>

數(shù)據(jù)庫使用MySQL,這個例子連接knpcode庫,并且檢索EMPLOYEE表,這個EMPOLYEE表包含的域有ID,FIREST_NAME,LAST_NAME以及DEPARTMENT。

通過實例化ComboPooledDataSource類來創(chuàng)建C3P0連接池。

下面是一些數(shù)據(jù)庫相關(guān)的配置:

acquireIncrement:如果連接池中的連接被使用殆盡,將會以多少的數(shù)量進行增加,默認值為3。

initialPoolSize:程序啟動時連接池的數(shù)量,默認為3。

maxPoolSize:最大連接數(shù),默認為15.

maxIdleTime:幾秒未使用的連接就會被丟棄,默認為0,不丟棄。

minPoolSize:連接池中最小的數(shù)量。

數(shù)據(jù)庫和鏈接池相關(guān)的配置都在db.properties文件中:

DB.DRIVER_CLASS=com.mysql.cj.jdbc.Driver DB.DB_URL=jdbc:mysql://localhost:3306/knpcode DB.DB_USER=root DB.DB_PASSWORD=admin DB.INITIAL_POOL_SIZE=5 DB.MAX_POOL_SIZE=5

下面是創(chuàng)建ComboPooledDataSource相關(guān)的代碼:

import java.beans.PropertyVetoException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource;public class DSCreator {private static ComboPooledDataSource pooledDS;static {try {pooledDS = new ComboPooledDataSource();Properties properties = new Properties();// Loading properties file from classpathInputStream inputStream = DSCreator.class.getClassLoader().getResourceAsStream("db.properties");if(inputStream == null){throw new IOException("File not found");}properties.load(inputStream); pooledDS.setDriverClass(properties.getProperty("DB.DRIVER_CLASS"));pooledDS.setJdbcUrl(properties.getProperty("DB.DB_URL"));pooledDS.setUser(properties.getProperty("DB.DB_USER"));pooledDS.setPassword(properties.getProperty("DB.DB_PASSWORD"));pooledDS.setInitialPoolSize(Integer.parseInt(properties.getProperty("DB.INITIAL_POOL_SIZE")));// Default anywaypooledDS.setAcquireIncrement(3);pooledDS.setMaxPoolSize(Integer.parseInt(properties.getProperty("DB.MAX_POOL_SIZE")));}catch(IOException | PropertyVetoException e) {e.printStackTrace();}}public static DataSource getDataSource() {return pooledDS;} }

Test類用于創(chuàng)建連接以及獲取DataSource對象以及查詢數(shù)據(jù)庫:

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource;public class DSDemo {public static void main(String[] args) {DSDemo dsDemo = new DSDemo();dsDemo.displayEmployeeById(16);}private void displayEmployeeById(int id){Connection connection = null; String selectSQL = "SELECT * FROM EMPLOYEE WHERE id = ?";PreparedStatement prepStmt = null;try {DataSource ds = DSCreator.getDataSource();connection = ds.getConnection();prepStmt = connection.prepareStatement(selectSQL);prepStmt.setInt(1, id);ResultSet rs = prepStmt.executeQuery();while(rs.next()){System.out.println("id: " + rs.getInt("id"));System.out.println("First Name: " + rs.getString("FIRST_NAME"));System.out.println("Last Name: " + rs.getString("LAST_NAME"));System.out.println("Department: " + rs.getString("DEPARTMENT"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(connection != null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }

?

總結(jié)

以上是生活随笔為你收集整理的Java文档阅读笔记-C3P0连接池的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。