⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconfig.java.svn-base

📁 自用的一个简单的数据库连接池
💻 SVN-BASE
字号:
package dev.trade.common.db;

import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;

/**
 *
 * <p>Title: 福建客服支撑系统</p>
 *
 * <p>Description: 系统的配置信息管理类</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Newland</p>
 *
 * @author ZhengYanNan
 * @version 1.0
 */
public class DBConfig{
  private static Logger log = Logger.getLogger(DBConfig.class);

  //配置文件名,置于类路径下
  public final static String DB_CONFIG_FILE = "dbConfig.properties";
  public final static String DB_CONFIG_FILE_ZH_CN = "dbConfig_zh_CN.properties";
  public final static String INIT_POOLS = "db.initPools";
  public final static String POOL_PREFIX = "db.pool.";
  public final static String POOL_DRIVER = ".driver";
  public final static String POOL_URL = ".url";
  public final static String POOL_USER = ".user";
  public final static String POOL_PWD = ".password";
  public final static String POOL_LOGIN_TIMEOUT = ".loginTimeout";
  public final static String POOL_INIT_CONNS = ".initConns";
  public final static String POOL_INC_CONNS = ".incConns";
  public final static String POOL_MAX_CONNS = ".maxConns";
  public final static String POOL_TEST_TABLE = ".testTable";

  private static Properties coreProp = new Properties();

  public DBConfig(){
  }

  /**
   * 初始化配置
   */
  public static void init() throws Exception{
    InputStream fis = null;
    try{
      log.info("正在载入连接池配置信息!");
      coreProp.clear();
      try{
        fis = getResourceAsStream(DB_CONFIG_FILE);
      } catch(Exception ex){
        fis = getResourceAsStream(DB_CONFIG_FILE);
      }
      coreProp.load(fis);
    } catch(Exception e){
      log.error("无法从文件:" + DB_CONFIG_FILE + " 载入配置信息", e);
      throw e;
    } finally{
      if(fis != null)
        try{
          fis.close();
        } catch(Exception e){
        }
    }
  }

  /**
   * 得到配置属性的值
   * @param key 配置属性的名称
   * @return 配置属性的值,如果配置属性没有设置值,则返回null
   */
  public static String get(String key){
    return coreProp.getProperty(key, null);
  }

  /**
   * 得到配置属性的值
   * @param key 配置属性的名称
   * @param defaultvalue 默认值
   * @return 配置属性的值
   */
  public static String get(String key, String defaultvalue){
    return coreProp.getProperty(key, defaultvalue);
  }

  /**
   * 获取配置属性的int型值
   * @param key String
   * @param defValue int
   * @return int
   */
  public static int getInt(String key, int defValue){
    try{
      String val = get(key);
      return Integer.parseInt(val);
    } catch(Exception ex){
      return defValue;
    }
  }

  /**
   * 获取配置属性的long型值
   * @param key String
   * @param defValue long
   * @return long
   */
  public static long getLong(String key, long defValue){
    try{
      String val = get(key);
      return Long.parseLong(val);
    } catch(Exception ex){
      return defValue;
    }
  }

  /**
   * 获取配置属性的float型值
   * @param key String
   * @param defValue float
   * @return float
   */
  public static float getFloat(String key, float defValue){
    try{
      String val = get(key);
      return Float.parseFloat(val);
    } catch(Exception ex){
      return defValue;
    }
  }

  /**
   * 获取配置属性的double型值
   * @param key String
   * @param defValue double
   * @return double
   */
  public static double getDouble(String key, double defValue){
    try{
      String val = get(key);
      return Double.parseDouble(val);
    } catch(Exception ex){
      return defValue;
    }
  }

  /**
   * 获取配置属性的boolean型值
   * @param key String
   * @param defValue boolean
   * @return boolean
   */
  public static boolean getBoolean(String key, boolean defValue){
    try{
      String text = get(key);
      return (("true".equalsIgnoreCase(text))
            || ("yes".equalsIgnoreCase(text))
            || ("enable".equalsIgnoreCase(text))
            || ("on".equalsIgnoreCase(text))
            || ("1".equalsIgnoreCase(text)));
    } catch(Exception ex){
      return defValue;
    }
  }

  /**
   * 得到全部的属性配置信息
   * @return 返回通过配置文件生成的properties。
   */
  public static Properties getAll(){
    return coreProp;
  }

  /**
   * 从资源文件中得到文件的输入流
   * @param resource 资源文件的名称,这个文件应该存在classpath下
   * @return 文件的输入流
   * @throws IOException 如果读取文件错误,则抛出该异常。
   */
  public static InputStream getResourceAsStream(String resource) throws Exception{
    InputStream in = DBConfig.class.getClassLoader().getResourceAsStream(resource);
    if(in == null){
//      log.debug("load from system");
      in = ClassLoader.getSystemResourceAsStream(resource);
    }
    if(in == null){
//      log.debug("load from file");
      in = new FileInputStream(resource);
    }
    if(in == null)
      throw new IOException("无法读取资源文件: " + resource);
    return in;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -