c3p0 c3p0 0.9.1.2
junit junit 4.12 test
org.springframework spring-context 5.0.5.RELEASE

标签解析
//创建数据源ComboPooledDataSource dataSource = new ComboPooledDataSource();//设置连接参数dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");dataSource.setUser("root");dataSource.setPassword("root");
解释:ComboPooledDataSource类的四个变量分别为driverClass,jdbcUrl,user,password,这四个名字的是根据四个set方法setDriverClass,setJdbcUrl,setUser,setPassword,去掉set得到的,且将首字母大写改为小写。即setDriverClass,则name=“driverClass”。
public void test4() throws Exception{//创建Spring容器对象ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//获取ComboPooledDataSource类的对象(连接池数据源对象)DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");//获取连接Connection connection = dataSource.getConnection();//打印连接System.out.println(connection);//连接放回连接池connection.close();
}
结果
com.mchange.v2.c3p0.impl.NewProxyConnection@4c40b76e
com.alibaba druid 1.1.10
junit junit 4.12 test
org.springframework spring-context 5.0.5.RELEASE

标签解析
//创建数据源DruidDataSource dataSource = new DruidDataSource();//设置连接参数dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test");dataSource.setUsername("root");dataSource.setPassword("root");
解释:DruidDataSource 类的四个变量分别为driverClassName,url,username,password,这四个名字的是根据四个set方法setDriverClassName,setUrl,setUsername,setPassword,去掉set得到的,且将首字母大写改为小写。即setDriverClassName,则name=“driverClassName”。(与c3p0的name有所不同)
public void test5() throws Exception{//创建Spring容器对象ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//获取DruidDataSource类的对象(连接池数据源对象)DataSource dataSource = (DataSource) applicationContext.getBean("dataSource1");//获取连接Connection connection = dataSource.getConnection();//打印连接System.out.println(connection);//连接放回连接池connection.close();
}
结果
com.mysql.jdbc.JDBC4Connection@402e37bc
下一篇:Word加载项/插件管理