1、数据源(连接池)可以提高程序性能
2、事先实例化数据源,初始化部分连接资源
3、使用连接资源时从数据源中获取
4、使用完毕后将连接资源归还给数据源
常见的数据源(连接池): DBCP、C3P0、BoneCP、Druid
1、导入数据源的坐标和数据库驱动坐标
2、创建数据源的对象
3、设置数据源的4个(驱动、数据库地址、用户名、密码)基本参数信息
4、使用数据源获取连接资源和归还连接资源
public void test1() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//dataSource.setDriverClass("com.mysql.jdbc.Driver"); //5.0版本以上的数据库写法
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); //8.0版本以上的数据库写法
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/bilibili");
dataSource.setUser("root");
dataSource.setPassword("228675");
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
public void test2() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/bilibili");
dataSource.setUsername("root");
dataSource.setPassword("228675");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
xxxxxxxxxx
public void test3() throws Exception {
//读取配置文件
ResourceBundle rb = ResourceBundle.getBundle("jdbc");//表示读取jdbc.properties文件
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();
}
优点: 解耦,当我们数据库信息变化时,不需要改代码,直接去改jdbc.properties文件
可以让Spring容器帮我们创建DataSource
xxxxxxxxxx
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bilibili"></property>
<property name="user" value="root"></property>
<property name="password" value="228675"></property>
</bean>
public void test4() throws Exception {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = app.getBean(DataSource.class);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
'applicationContext.xml'加载'jdbc.properties配置文件'获得连接信息
首先,需要引入context命名空间和约束路径:
1、命名空间: xmlns:context="http://www.springframework.org/schema/context"
2、约束路径: http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
通过context命名空间
<context:property-placeholder location="classpath:jdbc.properties"/>
<property name="" value="${key}"></property>
例如如下
<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>