JDBC连接与连接池

一、JDBC介绍

  • JDBC为访问不同的数据库,提供了统一接口
  • 基本原理(好处

image-20220709160810100

  • Java驱动数据库
    • 程序可以通过数据库驱动 相当于所有的可视化界面的步骤
//1.加载驱动  
    Class.forName
//2.用户信息和url
//3.连接成功  
    Connection db=DriverManager.getConnection(url, username, password);
//4.创建执行SQL的对象  
    Statement statement =db.createStatement();
//5.执行SQL的对象去执行SQL,可能存在结果,查看返回结果
    ResultSet resultSet = statement.executeQuery(sql);
//6.释放连接
    .close();

二、不同方式连接数据库

  1. 反射机制
Class<?> class=Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
Driver driver=(Driver)class.newInstance();//通过反射创建驱动对象
driver.connect(url,properties);//连接
  1. DriverManager替换Driver
Class<?> class=Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
Driver driver=(Driver)class.newInstance();//通过反射创建驱动对象
DriverManager.registerDriver(driver);//注册driver驱动
DriverManager.getConnection(url, username, password);//连接

3.省略注册driver驱动

//使用Class.forName会自动完成注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
DriverManager.getConnection(url, username, password);//连接

原理:image-20220709175131767

4.可以省略加载驱动(但还是建议写上)

DriverManager.getConnection(url, username, password);//连接

5.网页端写法(省略路径问题

Properties properties = new Properties();
//通过反射来获取配置文件流(jdbc.properties必须在Resource目录底下)
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//并且加载
properties.load(inputStream);
//创建数据库连接池
dataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);

三、数据库连接池

  • 传统方式连接的问题
    • 连接较多时耗时太久,占用很多系统资源
    • 如果程序出现关闭异常,将可能出现内存泄漏
    • 不能控制连接数量
  • 连接池原理(使用DataSource接口实现

image-20220711102024584

image-20220711102632493

  • 连接池种类

    image-20220711102938111

    • C3P0连接池

    连接方法1:读取properties配置文件,手动进行填写

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w0LFvufd-1658969598968)(…/…/myimage/image-20220712113609946.png)]

    /**
         * 方式1:相关参数方法
         * 在程序中指定user,url,password
         * @throws Exception
         */
        public static void testC3P0_1() throws Exception {
            //1.创建一个数据源对象
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
            //2.通过配置文件获取相关信息
            Properties properties = new Properties();
            properties.load(new FileReader("resources/jdbc.properties"));
            String driver=properties.getProperty("driverClassName");
            String user = properties.getProperty("username");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
    
            //给comboPooledDataSource设置相关参数
            comboPooledDataSource.setDriverClass(driver);
            comboPooledDataSource.setJdbcUrl(url);
            comboPooledDataSource.setUser(user);
            comboPooledDataSource.setPassword(password);
            comboPooledDataSource.setInitialPoolSize(5);
            comboPooledDataSource.setMaxPoolSize(10);
    
            Connection con = comboPooledDataSource.getConnection();
            System.out.println("连接成功");
            con.close();
        }
    

    连接方法2:使用配置模板c3p0-service.xml,需要放到根目录下

        /**
         * 方式2:使用配置模板,需要放到根目录下
         * 将使用的模板c3p0-service.xml拷贝进来
         * 使用named-config 进行配置
         * @throws SQLException
         */
        public static void testC3P0_02() throws SQLException {
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("mySource");
            Connection connection = comboPooledDataSource.getConnection();
            System.out.println("连接成功");
            connection.close();
        }
    

    运行结果

    image-20220712113735540

    • Druid连接池

    读取properties配置文件

    image-20220712113609946

    //1.加入jdbc.properties配置文件
    Properties properties=new Properties();
    properties.load(new FileReader("resources/jdbc.properties"));
    
    //2.创建数据库连接池
    DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
    Connection connection = dataSource.getConnection();
    System.out.println("连接成功");
    connection.close();
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值