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

歡迎訪問 生活随笔!

生活随笔

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

java

Java JDBC篇4——数据库连接池

發布時間:2025/3/12 java 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java JDBC篇4——数据库连接池 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java JDBC篇4——數據庫連接池

1、DBCP

1.1、依賴jar包

官網:https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2

mysql-connector-java-5.1.49.jar 百度云:https://pan.baidu.com/s/17J2VfkGS2h44j69eB8TuFA提取碼:nhnt

mysql-connector-java-8.0.25.jar 百度云:https://pan.baidu.com/s/1b8n7650uMKJtwidoptOjNQ提取碼:wtvn

commons-dbcp2-2.8.0 百度云:https://pan.baidu.com/s/10bcq3Fzo36MnFFMppICi3A提取碼:1tkz

commons-pool2-2.8.0 百度云:https://pan.baidu.com/s/1sZLSi0nRZqeYbhDuO5Mkxw提取碼:hk3w

commons-logging-1.2 百度云:https://pan.baidu.com/s/1ZFZZcmCjZtRwumh_qisKIw提取碼:2uaz

1.2、快速入門

url=jdbc:mysql://localhost:3306/test user=root password=blingbling123. driver=com.mysql.jdbc.Driver public class DBCPPoolUtils {private static String urls;private static String user;private static String password;private static String driver;private static BasicDataSource basicDataSource=null;static {Properties properties=new Properties();ClassLoader classLoader=JDBCtool.class.getClassLoader();URL url=classLoader.getResource("connection.properties");String path=url.getPath();try {properties.load(new FileReader(path));} catch (IOException e) {e.printStackTrace();}System.out.println(properties);basicDataSource=new BasicDataSource();basicDataSource.setDriverClassName(properties.getProperty("driver"));basicDataSource.setUrl(properties.getProperty("url"));basicDataSource.setUsername(properties.getProperty("user"));basicDataSource.setPassword(properties.getProperty("password"));}public static Connection getConnection() throws SQLException {System.out.println(basicDataSource);//從連接池中獲取連接Connection connection = basicDataSource.getConnection();return connection;}public static void close(Connection connection, Statement statement, ResultSet resultSet){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();}}} } public class Test {public static void main(String[] args) throws SQLException {Connection connection = DBCPPoolUtils.getConnection();String sql="select * from user";PreparedStatement preparedStatement = connection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("username"));}DBCPPoolUtils.close(connection,preparedStatement,resultSet);} }

1.3、配置項

屬性描述
driverClassName數據庫驅動名稱
url數據庫地址
username用戶名
password密碼
maxActive最大連接數量
maxIdle最大空閑連接
minIdle最小空閑連接
initialSize初始化連接

2、Druid

2.1、依賴jar包

官網:https://mvnrepository.com/artifact/com.alibaba/druid

mysql-connector-java-5.1.49.jar 百度云:https://pan.baidu.com/s/17J2VfkGS2h44j69eB8TuFA提取碼:nhnt

mysql-connector-java-8.0.25.jar 百度云:https://pan.baidu.com/s/1b8n7650uMKJtwidoptOjNQ提取碼:wtvn

druid1.2.6 百度云:https://pan.baidu.com/s/1f90LwWhAtPaAOASKf1UfPA提取碼:iq2n

2.2、快速入門

driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=blingbling123. initialSize=5 maxActive=10 maxWait=3000 public class DruidPoolUtils {private static DataSource dataSource=null;static {Properties properties=new Properties();ClassLoader classLoader=JDBCtool.class.getClassLoader();URL url=classLoader.getResource("connection.properties");String path=url.getPath();try {properties.load(new FileReader(path));System.out.println(properties);dataSource= DruidDataSourceFactory.createDataSource(properties);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}public static Connection getConnection(){try {return dataSource.getConnection();} catch (SQLException e) {e.printStackTrace();return null;}}public static void close(Connection connection, Statement statement, ResultSet resultSet){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();}}} } public class Test {public static void main(String[] args) throws SQLException {Connection connection = DruidPoolUtils.getConnection();String sql="select * from user";PreparedStatement preparedStatement = connection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("username"));}DruidPoolUtils.close(connection,preparedStatement,resultSet);} }

總結

以上是生活随笔為你收集整理的Java JDBC篇4——数据库连接池的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。