YuFeng

jdbc dbcp连接池
工具包下载:commons-dbcp-1.4-bin.zipcommons-pool-1.5.6-bin.zipd...
扫描右侧二维码阅读全文
15
2018/11

jdbc dbcp连接池

工具包下载:
commons-dbcp-1.4-bin.zip
commons-pool-1.5.6-bin.zip
dbcpconfig.properties

DBCP 开源连接池 tomcat内置连接池
使用DBCP连接池需要导入两个jar包: commons-dbcp-1.4.jar,commons-pool-1.5.6.jar.
相应的配置文件名称为:*.properties 建议src路径下
代码::::

package cn.yufeng.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBCPUtils {
    
    @SuppressWarnings("unused")
    private static DataSource dataSource;
    
    static {
        try {
            ClassLoader classLoader = DBCPUtils.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream("dbcp.properties");
            Properties p = new Properties();
            p.load(is);
            dataSource = BasicDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static DataSource getDataSource() {
        return dataSource;
    }
    
    public static void replace(Connection conn,Statement stat, ResultSet set) {
        if(set != null) {
            try {
                set.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        
        if(stat != null) {
            try {
                set.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        
        if(conn != null) {
            try {
                set.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

配置文件::::
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/fromName
username=root
password=password

连接数据库代码::::

package cn.yufeng.connection;

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

import cn.yufeng.utils.*;

public class DBCPConnection {
    public static void main(String[] args) {
        try {
            Connection conn = DBCPUtils.getConnection();
            String sql = "select * from user";
            PreparedStatement prepareStatement = conn.prepareStatement(sql);
            ResultSet set = prepareStatement.executeQuery();
            while(set.next()) {
                System.out.print(set.getString("uid"));
                System.out.println(set.getString("name"));
            }
            DBCPUtils.replace(conn, prepareStatement, set);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Last modification:June 11th, 2019 at 12:34 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment