Java配置数据源(连接池)

2021/7/9 11:07:38

本文主要是介绍Java配置数据源(连接池),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

数据源(连接池)的作用

数据源(连接池)时提高程序性能而出现的
事先实例化数据源,初始化部分连接资源
使用连接资源时从数据源中获取
使用完毕后将连接资源归还给数据源

数据源的开发步骤

导入数据源的坐标和数据库驱动坐标
创建数据源对象
设置数据源的基本连接数据
使用数据源获取连接资源和归还连接资源

本文主要讲述以下几种配置数据源方式

  1. 手动创建C3P0数据源
  2. 手动创建Druid数据源
  3. 抽取jdbc.properties文件
  4. 自动创建C3P0数据源
  5. Spring加载properties文件

为了方便,我就直接在测试文件里面编写代码

package com.ssm.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.sql.Connection;
import java.util.ResourceBundle;

public class DateSourceTest {
    @Test
    // 测试手动创建C3P0数据源
    public void test1() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        // 括号里面的内容需要对应改成自己数据库的地址
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db1");
        // 注意更改数据库用户名
        dataSource.setUser("root");
        // 注意改成对应用户名的密码
        dataSource.setPassword("root");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    // 测试手动创建Druid数据源
    public void test2() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        // 括号里面的内容需要对应改成自己数据库的地址
        dataSource.setUrl("jdbc:mysql://localhost:3306/db1");
        // 注意更改数据库用户名
        dataSource.setUsername("root");
        // 注意改成对应用户名的密码
        dataSource.setPassword("root");
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    // 测试手动创建C3P0数据源(加载配置文件形式)
    public void test3() throws Exception {
        // 读取配置文件
        // 注意与properties文件名相对应
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");
        // 以下注意与文件名中的变量相对应
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String username = rb.getString("jdbc.username");
        String password = rb.getString("jdbc.password");

        // 创建数据源对象,设置连接参数
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    
    @Test
    // 测试自动创建C3P0数据源
    public void test4() throws Exception{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    
	@Test
    // 测试Spring容器产生数据源对象
    public void test5() throws Exception{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }      
}

test3需要有一个名为jdbc.properties的文件配合

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=root

test4需要有一个名为applicationContext.xml的文件配合使用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	// 设置参数
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db1"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

</beans>

test5与test4类似,只是test4的参数是直接放在了applicationContext.xml中,而test5的参数放在了test3所在的jdbc.properties文件中,并且applicationContext.xml做了一些细微的改动。注意:${}里面的参数名必须与jdbc.properties中的一一对应

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--  加载外部的properties文件  -->
    <context:property-placeholder location="classpath:jdbc.properties" />


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

</beans>


这篇关于Java配置数据源(连接池)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程