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
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.引入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" />