1、配置Spring的坐标
点击链接:Spring的开发步骤_爱李多一点的博客-CSDN博客
2、Spring配置文件注入数据源
<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="123456"></property>
</bean>
3、加载数据源资源
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
connection.close();
}