赞
踩
一.数据源(连接池的作用)
1.数据源(连接池)是提高程序性能如
2.事先实例化数据源,初始部分连接资源
3.使用来连接资源时从数据源获取
4.使用完毕时将连接资源归还给数据源
二、数据源的开发步骤
步骤:
1.导入依赖坐标和数据驱动坐标
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.34</version>
- </dependency>
-
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.10</version>
- </dependency>
2.创建数据源的手动创建
2.1 创建C3p0连接池
- @Test
- //测试手动创建C3p0数据源
- public void test1() throws PropertyVetoException, SQLException {
- ComboPooledDataSource dataSource = new ComboPooledDataSource();
- dataSource.setDriverClass("com.mysql.jdbc.Driver");
- dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
- dataSource.setUser("root");
- dataSource.setPassword("123456");
- Connection connection = dataSource.getConnection();
- System.out.println(connection);
- connection.close();
- }
2.2创建Druid连接池
- public void Test2() throws SQLException {
- DruidDataSource dataSource=new DruidDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/test");
- dataSource.setUsername("root");
- dataSource.setPassword("123456");
- DruidPooledConnection connection = dataSource.getConnection();
- System.out.println(connection);
- connection.close();
-
- }
2.3 提取jdbc.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123456
2.4读取jdbc.properties配置文件文件创建连接池
- @Test
- //测试手动创建C3p0数据源(加载配置文件形式)
- public void test3() throws PropertyVetoException, SQLException {
- //读取配置文件
- ResourceBundle rb=ResourceBundle.getBundle("jdbc");
- String driver=rb.getString("jdbc.driver");
- String username=rb.getString("jdbc.username");
- String url=rb.getString("jdbc.url");
- 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();
- }
将DataSource的创建权交由Spring容器去完成
DataSource有无参构造的方法,而Spring默认就是通过无参构造的方法实例化对象
DataSource要想使用需要通过set方法设置数据库连接信息,而SPring可以通过set方法进行字符串注入
-
- <bean name="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>
- <bean name="druid" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
- <property name="username" value="root"></property>
- <property name="password" value="123456"></property>
- </bean>
测试从容器中获取数据源
- ApplicationContext applicationContext = new
- ClassPathXmlApplicationContext("applicationContext.xml");
- DataSource dataSource = (DataSource)
- applicationContext.getBean("dataSource");
- Connection connection = dataSource.getConnection();
- System.out.println(connection);
抽取jdbc配置文件
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <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>
Spring容器中加载properties文件
< context :property-placeholder location ="xx.properties" />< property name ="" value ="${key}" />
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。