//dataSource.java
package spring_ioc_anno; import java.beans.PropertyVetoException; import java.sql.Connection; import java.util.ResourceBundle; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceTest { @Test //测试手动创建c3p0数据源 public void test1() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db3"); dataSource.setUser("root"); dataSource.setPassword("001224"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } @Test //测试手动创建druid数据源 public void test2() throws Exception { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/db3"); dataSource.setUsername("root"); dataSource.setPassword("001224"); DruidPooledConnection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } @Test //测试手动创建c3p0数据源(加载properties配置文件形式) public void test3() throws Exception { //读取配置文件 ResourceBundle rb = ResourceBundle.getBundle("jdbc"); 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(); } @Test //测试Spring容器产生c3p0数据源对象 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(); } }
jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/db3 jdbc.username = root jdbc.password = 001224
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <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> </beans>
标签:jdbc,mysql,数据源,配置,connection,dataSource,Spring,import From: https://www.cnblogs.com/zlyyds/p/16919715.html