其他分享
首页 > 其他分享> > 连接池

连接池

作者:互联网

使用连接池有两种方式
1.使用DBCP,在maven项目中导入包

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-dbcp2</artifactId>
		<version>2.7.0</version>
	</dependency>
            
            import org.apache.commons.dbcp2.BasicDataSource;
            import java.sql.Connection;
            import java.sql.PreparedStatement;
            import java.sql.ResultSet;
            
            // 连接池的包
            // 1.可以只创建一次 一直连接数据库
            // 2.连接池中有很多个连接,可以同时去处理
            public class DBCPDemo {
                public static void main(String[] args) throws Exception{
                    //创建连接池对象
                    BasicDataSource basicDataSource = new BasicDataSource();
                    //指定连接信息
                    basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
                    basicDataSource.setUrl("jdbc:mysql://master:3306/shujia");
                    basicDataSource.setUsername("root");
                    basicDataSource.setPassword("123456");
                    //初始连接池大小
                    basicDataSource.setInitialSize(2);
                    //最大连接数量
                    basicDataSource.setMaxTotal(2);
                    //从池中获取连接
                    Connection connection = basicDataSource.getConnection();
                    PreparedStatement statement = connection.prepareStatement("select * from  user");
                    ResultSet rs = statement.executeQuery();
                    while (rs.next()) {
                        System.out.println(rs.getString("username"));
                    }
                    rs.close();
                    statement.close();
                    connection.close();
                }
            }

2.使用C3P0,在maven项目中导入包

        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
	<dependency>
		<groupId>com.mchange</groupId>
		<artifactId>c3p0</artifactId>
		<version>0.9.5.2</version>
	</dependency>

            import com.mchange.v2.c3p0.ComboPooledDataSource;
            import java.sql.Connection;
            public class Demo02C3P0 {
                public static void main(String[] args) throws Exception{
                    // 创建连接池
                    ComboPooledDataSource dataSource = new ComboPooledDataSource();
                    //配置连接信息
                    dataSource.setDriverClass("com.mysql.jdbc.Driver");
                    dataSource.setJdbcUrl("jdbc:mysql://master:3306/shujia?useUnicode=true&characterEncoding=utf-8");
                    dataSource.setUser("root");
                    dataSource.setPassword("123456");

                    // 设置连接池的信息
                    dataSource.setInitialPoolSize(2);
                    dataSource.setMaxPoolSize(4);
                    Connection connection = dataSource.getConnection();
                    connection.close();
                }
            }

标签:basicDataSource,connection,dataSource,mysql,import,连接池
来源: https://www.cnblogs.com/yuzhongwen/p/15264684.html