Java JDBC篇4——数据库连接池
1、DBCP
1.1、依赖jar包
官网:/artifact/mons/commons-dbcp2
mysql-connector-java-5.1.49.jar 百度云:/s/17J2VfkGS2h44j69eB8TuFA提取码:nhnt
mysql-connector-java-8.0.25.jar 百度云:/s/1b8n7650uMKJtwidoptOjNQ提取码:wtvn
commons-dbcp2-2.8.0 百度云:/s/10bcq3Fzo36MnFFMppICi3A提取码:1tkz
commons-pool2-2.8.0 百度云:/s/1sZLSi0nRZqeYbhDuO5Mkxw提取码:hk3w
commons-logging-1.2 百度云:/s/1ZFZZcmCjZtRwumh_qisKIw提取码:2uaz
1.2、快速入门
url=jdbc:mysql://localhost:3306/testuser=rootpassword=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、配置项
2、Druid
2.1、依赖jar包
官网:/artifact/com.alibaba/druid
mysql-connector-java-5.1.49.jar 百度云:/s/17J2VfkGS2h44j69eB8TuFA提取码:nhnt
mysql-connector-java-8.0.25.jar 百度云:/s/1b8n7650uMKJtwidoptOjNQ提取码:wtvn
druid1.2.6 百度云:/s/1f90LwWhAtPaAOASKf1UfPA提取码:iq2n
2.2、快速入门
driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/testusername=rootpassword=blingbling123.initialSize=5maxActive=10maxWait=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——数据库连接池》对你有帮助,请点赞、收藏,并留下你的观点哦!