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

📄 crbtdbcommon.java

📁 中国联通炫铃业务接口开发
💻 JAVA
字号:
package com.wireless.crbt.gwif.function;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Map;

import org.apache.commons.configuration.Configuration;

import com.wireless.sms.pub.db.ConnectionPool;
import com.wireless.sms.pub.util.XMLConfig;
/**
 * 该类实现初始化数据库
 * @author Administrator
 *
 */
public class CrbtDBCommon {
	private static CrbtDBCommon _instance=null;
	public ConnectionPool POOL = null;
	public static String localIp = null;
	public static int [] ports = null;

	private static Map mttypeMap = null;
	private static long lastTime1 = 0;
	private static long HOURS = 60*60*1000l;
	  
	public static synchronized CrbtDBCommon getInstance(){
		if(_instance==null)
			_instance=new CrbtDBCommon();
		return _instance;
	}
	
	/**
	 * 得到数据库连接
	 * @return
	 */
	public boolean initCommon() {
		boolean initResultFlag=false;
	      XMLConfig.setConfiguration(null);
	      Configuration config = XMLConfig.getConfiguration();

	      //数据库参数
	      initDB();
	      localIp = config.getString("socket.datains.localIp");

	      String [] sports = config.getStringArray("socket.datains.localIp[@ports]");
	      ports = new int[sports.length];
	      for(int i=0; i<sports.length; i++){
	        ports[i] = Integer.parseInt(sports[i]);
	      }

	      //初始化数据库参数.....
	      if( mttypeMap == null || ((System.currentTimeMillis()-lastTime1)>HOURS)){
	          Connection conn = null;
	          Statement stmt = null;
	          ResultSet rst = null;

	          Hashtable tempMap = new Hashtable();
	          String sql = "select * from sms_mtlogparameter t";

	          try {
	              conn = POOL.getConnection();
	              stmt = conn.createStatement();
	              rst  = stmt.executeQuery(sql);

	              while (rst.next()) {
	                  String mttype = rst.getString("nummtype");
	                  String issend = rst.getString("vc2isend");
	                  String savetable = rst.getString("vc2savetable");
	                  tempMap.put(mttype, issend+":"+savetable);
	              }
	              mttypeMap = new Hashtable(tempMap);
	              lastTime1 = System.currentTimeMillis();
	          } catch (Exception e) {
	              e.printStackTrace();
	              e.printStackTrace();
//	              SmsGWIFDBGlobal.sendMonitor("D000001");
	          } finally {
	              if( rst != null ){
	                try {
	                  rst.close();
	                  rst = null;
	                }
	                catch (SQLException ex2) {
	                }
	              }
	              if (stmt != null) {
	                  try {
	                      stmt.close();
	                      stmt = null;
	                  } catch (SQLException ex) {
	                  }
	              }
	              if (conn != null) {//断开数据库连接
	                  try {
	                      conn.close();
	                      conn = null;
	                  } catch (SQLException ex1) {
	                  }
	              }
	          }
	          if( mttypeMap.size() == 0 ){
	        	  initResultFlag=false;
	        	  throw new java.lang.IllegalStateException("数据库初始化失败 ...");
	          }else{
	        	  System.out.println("数据库初始化成功.........");
	        	  initResultFlag=true;
	            }
	      }
	      return initResultFlag;
	  }
	
	/**
     * 断开数据库连接
     * @param conn Connection
     */
    public void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception ex) {
        }
    }
	
	private void initDB(){
	      try {
	          //从系统属性中得到配置文件路径配置连接池,如果没有该文件则用默认方法
	          String dbconfigfile = System.getProperty("dbconfig");
	          File temFile = null;
	          if (dbconfigfile != null && !dbconfigfile.equals("")){
	              temFile = new File(dbconfigfile);
	              if (temFile != null && temFile.exists()) {
	                  POOL = ConnectionPool.getInstance(dbconfigfile);
	              }else{
	                  POOL = ConnectionPool.getInstance();
	              }
	          }else{
	              POOL = ConnectionPool.getInstance();
	          }
	              //end add
	      }catch (Exception ex1){
	          ex1.printStackTrace();
	      }
	  }

}

⌨️ 快捷键说明

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