2021-08-07 DBCP的一些配置文件和测试代码工具库

DBCP配置文件.peoperties

#连接设置,这里面的名字,是DBCP数据源中定义好的
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root

#<!-- 初始化连接-->
initialSize=10


#最大连接数量
maxActive=50


#<!-- 最大空闲连接-->
maxIdle=20


#<!-- 最小空闲连接-->
minIdle=5

#<!-- 超时等待时间以毫秒为单位 就是60秒-->
maxxWait=60000

#JDBC驱动建立连接时附带的连接属性的格式必须 为这样:[属性名=property;]
#注意:"user"与”password“两个属性会被明确传递,因此这里不需要包含他们
connectionProperties=useUnicode=true;characterEncoding=UTF8

#指定由连接池所创建的连接的自动提交(auto-commit)状态
 defaultAutoCommit=true

#drive default 指定由连接池所创建的连接的只读状态
#如果没有设置只读,则setReadOnly 方法不会被调用。(某些驱动并不支持只读模式,如Informix)
defaultReadOnly=

#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)
# 可用值为下列之一:(详情可见javadoc)NOME,READ_UNCOMMITTED,REPEATABLE,REPEATABLE_READ,SERIALIZABLF
defaultTransactionIsolation=READ_UNCOMMITTED

DBCP工具库以及测试代码:

package com.kuang.lesson05.utils;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbcUtils_DBCP {
   private  static DataSource dataSource1=null;
    static {
        try {
            InputStream in = jdbcUtils_DBCP.class.getClassLoader ().getResourceAsStream ("dbcpconfig.properties");
            Properties properties = new Properties ();
            properties.load (in);
             //创建数据源  工厂模式 --》创建
       dataSource1= BasicDataSourceFactory.createDataSource (properties);
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }


        //  获取连接
       public static Connection getConnection() throws SQLException {
            return dataSource1.getConnection ();
        }



        //  释放资源
        public static void release(Connection con, Statement st, ResultSet rs){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace ();
            }
        }
            if(st!=null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace ();
                }
                if(con!=null) {
                    try {
                        con.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace ();
                    }
                }

            }
        }
        }



package com.kuang.lesson05;

import com.kuang.lesson02.utils.jdbcUtils;
import com.kuang.lesson05.utils.jdbcUtils_DBCP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

public class TestDBCP {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement st = null;
        try {
            con =jdbcUtils_DBCP.getConnection ();
            String sql = "INSERT INTO users (id,name,password,email,birthday)" +
                    "VALUES(?,?,?,?,?)";
            st = con.prepareStatement (sql);
            st.setInt (1, 4);
            st.setString (2, "zhangsan");
            st.setString (3, "123456");
            st.setString (4, "23434234@qq.com");
            st.setDate (5, new java.sql.Date (new Date ().getTime ()));
            int n = st.executeUpdate ();
            if (n > 0) {
                System.out.println ("插入成功");
            }
        } catch (SQLException e) {
            e.printStackTrace ();
        } finally {
            jdbcUtils_DBCP.release (con, st, null);
        }
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值