DBCP和C3P0

DBCP

  • 要写一个dbcp.properties配置文件
  • 导包:
    这里写图片描述
package bull03.DBCP;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;

import bull01.JDBCbasis.JDBCUtils;

/**
 * DBCP连接池
 * @author 45度炸
 *
 */
public class DBCPdemo1 {
    @Test
    public void demo1() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        Properties properties = new Properties();
        try {
            //获得数据源
            properties.load(new FileInputStream("src/dbcp.properties"));
            DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
            //获得连接
            conn = dataSource.getConnection();
            String sql = "select * from category";
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            while(rs.next()) {
                System.out.println(rs.getInt("cid")+"---"+rs.getString("cname"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(rs, psmt, conn);
        }
    }
}


C3P0

  • 要写一个c3p0-config.xml配置文件
  • 导包:
    这里写图片描述
C3P0工具类
package bull04.C3P0;

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

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0JDBCUtils {
    private static final ComboPooledDataSource dataSource = new ComboPooledDataSource("bull");

    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public static void closeResource(ResultSet rs,PreparedStatement psmt,Connection conn) {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            rs = null;
        }
        if(psmt != null) {
            try {
                psmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            psmt = null;
        }
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn = null;
        }
    }
}
C3P0测试类
package bull04.C3P0;

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

import org.junit.Test;

import bull01.JDBCbasis.JDBCUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * C3P0
 * @author 45度炸
 *
 */
public class C3P0demo1 {
    @Test
    public void demo1() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;

        try {
            //ComboPooledDataSource dataSource = new ComboPooledDataSource("bull");
            //conn = dataSource.getConnection();
            conn = C3P0JDBCUtils.getConnection();
            String sql = "select * from category";
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            while(rs.next()) {
                System.out.println(rs.getInt("cid")+"---"+rs.getString("cname"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(rs, psmt, conn);
        }
    }



}
阅读更多
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_40662281/article/details/80322724
文章标签: java
个人分类: JavaWeb
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭