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——数据库连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语语法---不定式短语详解
- 下一篇: LeetCode--Add Two Nu