cyy3900

博客园 首页 联系 订阅 管理

下面是dbcp连接池的一个小例子,它包含了连接池管理类,连接池配置文件和一个测试类讲解如何获取链接和如何关闭链接。现在详述之。

1.相关的依赖包
使用jar包:commons-dbcp-1.4.jar、commons-pool-1.6.jar、commons-logging-1.1.3.jar,另外还有数据库的jdbc驱动,适用java6及以上平台。
2.连接池管理类

 1 package com.cyy.utils;
 2 
 3 import java.io.IOException;
 4 import java.sql.Connection;
 5 import java.sql.SQLException;
 6 import java.util.Properties;
 7 
 8 import javax.sql.DataSource;
 9 
10 import org.apache.commons.dbcp.BasicDataSourceFactory;
11 
12 
13 
14 
15 public final class JdbcUtils_Dbcp {
16 
17     private static DataSource ds;
18     
19     static{
20         Properties dbProp = new Properties();
21         try {
22             dbProp.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("dbcp.properties"));
23             ds = BasicDataSourceFactory.createDataSource(dbProp);
24         } catch (IOException e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         } catch (Exception e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31     }
32     
33     public static final Connection getConnection() throws SQLException{
34         return ds.getConnection();
35     }
36 }

3.连接池配置文件
在src根目录下面新建一个配置文件dbcp.properties

 1 #数据库驱动  
 2 driverClassName=com.mysql.jdbc.Driver
 3 #数据库连接地址  
 4 url=jdbc:mysql://localhost/mybatis
 5 #用户名  
 6 username=root
 7 #密码  
 8 password=390032540
 9 #连接池的最大数据库连接数。设为0表示无限制  
10 maxActive=30
11 #最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连  
12 #接将被标记为不可用,然后被释放。设为0表示无限制  
13 maxIdle=10
14 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制  
15 maxWait=1000
16 #超过removeAbandonedTimeout时间后,是否进 行没用连接(废弃)的回收(默认为false,调整为true)   
17 removeAbandoned=true
18 #超过时间限制,回收没有用(废弃)的连接(默认为 300秒,调整为180)  
19 removeAbandonedTimeout=180

4.测试类

package com.cyy.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cyy.domain.User;
import com.cyy.utils.JdbcUtils;
import com.cyy.utils.JdbcUtils_Dbcp;

public class JdbcTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 通过jdbc与数据交互的一般步骤
       Connection conn = null;
        PreparedStatement statement = null; //预编译sql语句,防止SQL注入
        ResultSet rs = null;
        String sql = "select * from user where name=?";
        try {
            // 1.加载驱动
//            Class.forName("com.mysql.jdbc.Driver");

            // 2.获取连接
            conn = JdbcUtils_Dbcp.getConnection();

            // 3.创建查询语句
            statement = conn.prepareStatement(sql);

            // 4.向PrepareStatement占位符设置参数
            statement.setString(1, "cyy");

            // 5.执行查询语句,得到结果集
            rs = statement.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt(1));
                user.setBirthday(rs.getDate(3));
                user.setName(rs.getString(2));
                System.out.println(user);
            }
        }  catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                rs = null;
            }
            
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                statement = null;
            }
            
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            
            
    }
}

 

posted on 2017-03-11 14:19  cyy3900  阅读(198)  评论(0编辑  收藏  举报