Java教程

配置数据源

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

1.Spring配置数据源

1.1获得数据源的步骤

①导入数据源相关的jar包
②创建数据源对象
③设置数据源的基本连接数据(配置信息)
④使用数据源获取连接资源和归还资源

1.2手动创建数据源demo

1.c3p0

@Test
public void test1() throws PropertyVetoException, SQLException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
    comboPooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true");
    comboPooledDataSource.setUser("root");
    comboPooledDataSource.setPassword("admin");
    Connection connection = comboPooledDataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

2.druid

@Test
public void test2() throws PropertyVetoException, SQLException {
    DruidDataSource comboPooledDataSource = new DruidDataSource();
    comboPooledDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    comboPooledDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true");
    comboPooledDataSource.setUsername("root");
    comboPooledDataSource.setPassword("admin");
    Connection connection = comboPooledDataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

3.读取配置文件

    @Test
public void test3() throws PropertyVetoException, SQLException {
    ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
    String driver = jdbc.getString("jdbc.driver");
    String url = jdbc.getString("jdbc.url");
    String username = jdbc.getString("jdbc.username");
    String password = jdbc.getString("jdbc.password");
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setDriverClass(driver);
    comboPooledDataSource.setJdbcUrl(url);
    comboPooledDataSource.setUser(username);
    comboPooledDataSource.setPassword(password);
    Connection connection = comboPooledDataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

jdbc.properties文件

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

1.3 Spring产生数据源

demo

    public void test4() throws PropertyVetoException, SQLException {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSources = applicationContext.getBean(DataSource.class);//DataSources为sql.DataSource
    Connection connection = dataSources.getConnection();
    System.out.println(connection);
    connection.close();
    }

xml配置

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

1.3 Spring 配置文件加载jdbc.properties文件

1.引入context命名空间

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd

2.配置xml文件

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="classpath:jdbc.properties" />
这篇关于配置数据源的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!