JAVA--高级基础开发Druid

Druid阿里巴巴属性文件
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&character=utf8
user=root
password=root
#配置Druid链接池参数
initialSize=5
minIdle=3
maxActive=10
maxWait=60000
timeBetweenEvictionRunsMillis=2000

//阿里 Druid 链接池
import com.alibaba.druid.pool.DruidDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtils3 {
    //建立阿里巴巴链接池对象
    private  static DruidDataSource ds;
    private  static Properties P;
    static {
        ds=new DruidDataSource();
        P=new Properties();
        //读取属性文件
        InputStream input=Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties");
        //加载P对象
        try{
            P.load(input);
        }catch(IOException ce){
            ce.printStackTrace();
        }
        //根据键获取值
        ds.setDriverClassName(P.getProperty("driverClass"));
        ds.setUrl(P.getProperty("url"));
        ds.setUsername(P.getProperty("user"));
        ds.setPassword(P.getProperty("password"));
        //配链接池的参数
        ds.setInitialSize(Integer.parseInt(P.getProperty("initialSize")));
        ds.setMinIdle(Integer.parseInt(P.getProperty("minIdle")));
        ds.setMaxActive(Integer.parseInt(P.getProperty("maxActive")));
        ds.setMaxWait(Integer.parseInt(P.getProperty("maxWait")));
        ds.setTimeBetweenEvictionRunsMillis(Integer.parseInt(P.getProperty("timeBetweenEvictionRunsMillis")));
    }

    //获取数据库链接对象
    public  static Connection  getConnection()throws SQLException{
        return ds.getConnection();
    }


    //关闭数据库链接对象之insert  delete update的操做
    public static  void  close(Connection  con, Statement state)throws SQLException{
        con.close();;
        state.close();
    }
    //关闭数据库链接的对象之 select 查找查询的操做
    public static  void close(Connection  con, Statement  state, ResultSet  set)throws SQLException{
        set.close();
        state.close();
        con.close();
    }
    //关闭获取数据库链接对象
    public  static  void  close(Connection  con)throws SQLException{
        con.close();
    }
    // 关闭执行Statement执行SQL 语句的对象
    public static  void close(Statement  state)throws SQLException{
        state.close();
    }
    //关闭结果集对象ResultSet对象
    public static  void close(ResultSet  set)throws SQLException{
        set.close();
    }
}
相关文章
相关标签/搜索