zoukankan      html  css  js  c++  java
  • 工具类

    package Jdbc_Utils;

    import com.alibaba.druid.pool.DruidDataSourceFactory;

    import javax.sql.DataSource;


    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;

    public class JDBCUtils {
    //1.定义成员变量
    private static DataSource ds;
    static {

    try { //2.加载配置文件 获得连接池
    Properties pro = new Properties();
    pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
    ds = DruidDataSourceFactory.createDataSource(pro);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    //获得链接
    public static Connection getConnection() throws SQLException {
    return ds.getConnection();
    }
    //释放资源
    public static void close(Connection conn, Statement state){
    if (conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (state!=null){
    try {
    state.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    //释放全部资源
    public static void close(ResultSet result, Connection conn, Statement state){
    if (result!=null){
    try {
    result.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (state!=null){
    try {
    state.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    //获得连接池
    public static DataSource getDataSource(){
    return ds;
    }
    }
  • 相关阅读:
    <LinkedList> 61
    <LinkedList> (hard + 高)25
    <DP> (高频)322
    <BackTracking> (dfs hard) 291
    <Tree> (高频)236
    <Math> 29 365
    <String> 161 358
    <Array> 309 (高)334
    <Array> 54 (高频+hard )45
    <Design> 359 346
  • 原文地址:https://www.cnblogs.com/xuaima/p/10650743.html
Copyright © 2011-2022 走看看