zoukankan      html  css  js  c++  java
  • DBCPTool

    dbcp读取配置文件的方式:

    1. 导入3个包:commons-dbcp-...  .jar(数据源) commons-collections-.....jar(集合) commons-pool....jar(连接池)   dbcp依赖 collections和pool

    2.新建 *.properties属性文件,内容如下:

    driver=com.mysql.jdbc.Driver

    url=jdbc:mysql://localhost:3306/dbName

    username=root

    password=root

    #<!-- 初始化连接-->

    initialSize=10

    maxActive=50

    minIdle=5

    maxWait=5000

      .

        .

        .     属性

    3.创建通用连接:

      

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

    import javax.sql.DataSource;

    import org.apache.commons.dbcp.BasicDataSourceFactory;

    public class DbcpTool {
    public static Connection dbcpWithDataSourceFactory() throws Exception{
    Connection conn=null;
    InputStream inputStream=DbcpTool.class.getClassLoader().getResourceAsStream("dbcp.properties");
    Properties properties=new Properties();
    properties.load(inputStream);
    DataSource dataSource=BasicDataSourceFactory.createDataSource(properties);
    conn=dataSource.getConnection();
    return conn;
    }
    public static void close(Connection conn){
    try {
    if(conn!=null){
    conn.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public static void close(PreparedStatement prst){
    try {
    if(prst!=null){
    prst.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public static void close(ResultSet rs){
    try {
    if(rs!=null){
    rs.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public static void close(PreparedStatement prst,Connection conn){
    close(prst);
    close(conn);
    }
    public static void close(ResultSet rs,PreparedStatement prst,Connection conn){
    close(rs);
    close(prst);
    close(conn);
    }

    }

  • 相关阅读:
    Insus Meta Utility
    The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
    Insus Binary Utility
    asp.net实现文件下载功能
    Column 'Column Name' does not belong to table Table
    程序已被编译为DLL,怎样去修改程序功能
    如何在Web网站实现搜索功能
    如何把数据流转换为二进制字符串
    Asp.net更新文件夹的文件
    如何显示中文月份
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/7469156.html
Copyright © 2011-2022 走看看