配置数据源

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" />
posted @   1906zz  阅读(138)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示