配置数据源
2022/7/5 23:25:06
本文主要是介绍配置数据源,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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" />
这篇关于配置数据源的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行