连接池dbcp

连接池dbcp

DBCP:apache组织


使用步骤:
  1.导入jar包(commons-dbcp-1.4.jar和commons-pool-1.5.6.jar、commons-logging-1.2.jar)
  2.使用api


a.硬编码
  //创建连接池
  BasicDataSource ds = new BasicDataSource();

  //配置信息
  ds.setDriverClassName("com.mysql.jdbc.Driver");
  ds.setUrl("jdbc:mysql:///day07");
  ds.setUsername("root");
  ds.setPassword("1234");

代码展示:

  1.创建一个项目DataSourse,创建一个包com.dbcp.hjh

  2.在包下创建一个类DBCPTest1.java

  3.新建一个lib用来存放jar包(commons-dbcp-1.4.jar    commons-pool-1.5.6.jar   commons-logging-1.2.jar),导入后buildpat

 

 

代码运行之前数据库数据如下:

 

java代码如下:

 JDBCUtil.java代码:

package com.util.hjh;

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

public class JDBCUtil {
    final static String driver = "com.mysql.jdbc.Driver";
    final static String url = "jdbc:mysql://localhost/hejh?useUnicode=true&characterEncoding=UTF-8";
    final static String user  = "root";
    final static String password = "root";
    
    Connection conn = null;
    PreparedStatement ps = null;
    Statement st = null;
    ResultSet rs = null;
        
    /**获取连接*/
    public static Connection getConnection() throws SQLException  {
        Connection    conn = null;
        try {
            //注册驱动
            Class.forName(driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }                    
        return conn;    
    }
    
    /**关闭资源closeResourse(conn,st)*/
    public static void closeResourse(Connection conn,Statement st) {
        try {
            if(st!=null) {
                st.close();
            }else {
                st = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null) {
                conn.close();
            }else {
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**关闭资源closeResourse(conn,ps)*/
    public static void closeResourse(Connection conn,PreparedStatement ps) {
        try {
            if(ps!=null) {
                ps.close();
            }else {
                ps = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null) {
                conn.close();
            }else {
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**关闭资源closeResourse(rs)*/
    public static void closeResourse(ResultSet rs) {
        try {
            if(rs!=null) {
                rs.close();
            }else {
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            
        }
    }
}

 

DBCPTest1.java代码如下:

package com.dbcp.hjh;

import java.sql.Connection;
import java.sql.PreparedStatement;import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.Test;
import com.util.hjh.JDBCUtil;

public class DBCPTest1 {

    @Test
    public void test() throws Exception  {
        //创建连接池
        BasicDataSource ds = new BasicDataSource();
        
        //配置信息
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost/jdbc");
        ds.setUsername("root");
        ds.setPassword("root");
        
        //连接数据库操作
        Connection conn =     ds.getConnection();
        String sql = "insert into student values(?,?,?)";
        PreparedStatement ps= conn.prepareStatement(sql);
        //设置参数
        ps.setInt(1, 10);
        ps.setString(2, "yz");
        ps.setInt(3, 95);
        
        int i = ps.executeUpdate();
        System.out.println("插入了"+i+"行数据");
        
        //关闭资源
        JDBCUtil.closeResourse(conn, ps);    
    }
}

 

代码运行后数据库数据显示为:在jdbc数据库中student表,新增了一条id为10的数据

 

 


b.配置文件
  实现编写一个properties文件
  //存放配置文件
  Properties prop = new Properties();
  prop.load(new FileInputStream("src/dbcp.properties"));
  //设置
  //prop.setProperty("driverClassName", "com.mysql.jdbc.Driver");

  //创建连接池
  DataSource ds = new BasicDataSourceFactory().createDataSource(prop);

 

代码介绍:

  1.创建一个项目DataSourse,创建一个包com.dbcp.hjh

  2.在包下创建一个类DBCPTest2.java

  3.新建一个lib用来存放jar包(commons-dbcp-1.4.jar    commons-pool-1.5.6.jar   commons-logging-1.2.jar),导入后buildpat

  4.新建一个配置文件dbcp.properties

 

 dbcp.properties内容:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/jdbc
username=root
password=root

 

 工具类JDBCUtil.java源码如下:

package com.util.hjh;

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

public class JDBCUtil {
    final static String driver = "com.mysql.jdbc.Driver";
    final static String url = "jdbc:mysql://localhost/hejh?useUnicode=true&characterEncoding=UTF-8";
    final static String user  = "root";
    final static String password = "root";
    
    Connection conn = null;
    PreparedStatement ps = null;
    Statement st = null;
    ResultSet rs = null;
        
    /**获取连接*/
    public static Connection getConnection() throws SQLException  {
        Connection    conn = null;
        try {
            //注册驱动
            Class.forName(driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }                    
        return conn;    
    }
    
    /**关闭资源closeResourse(conn,st)*/
    public static void closeResourse(Connection conn,Statement st) {
        try {
            if(st!=null) {
                st.close();
            }else {
                st = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null) {
                conn.close();
            }else {
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**关闭资源closeResourse(conn,ps)*/
    public static void closeResourse(Connection conn,PreparedStatement ps) {
        try {
            if(ps!=null) {
                ps.close();
            }else {
                ps = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null) {
                conn.close();
            }else {
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**关闭资源closeResourse(rs)*/
    public static void closeResourse(ResultSet rs) {
        try {
            if(rs!=null) {
                rs.close();
            }else {
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            
        }
    }
}

 

DBCPTest2.java源码如下:

package com.dbcp.hjh;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.junit.Test;
import com.util.hjh.JDBCUtil;

public class DBCPTest2 {
    
    @Test
    public void test() throws Exception {
        
        //存放配置文件
        Properties pp = new Properties();
        pp.load(new FileInputStream("src/dbcp.properties"));
        
        //创建连接池
        DataSource ds =   new BasicDataSourceFactory().createDataSource(pp);
        
        Connection conn = ds.getConnection();
        String sql  = "insert into student values(?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, 5);
        ps.setString(2, "zys");
        ps.setInt(3, 88);
        int i = ps.executeUpdate();
        System.out.println("插入了"+i+"条数据");
        
        //释放资源
        JDBCUtil.closeResourse(conn, ps);
    }
}

 

 数据库数据显示为:

posted @ 2019-05-15 14:01  雪顶儿  阅读(274)  评论(0编辑  收藏  举报