加载基础数据到static

加载基础数据到static,减少查询数据库次数

package com.?.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class Utility {
	private static final Logger log = Logger.getLogger(Utility.class);
	private static Map mpCityName; // 城市中文名称
	private static Map mpCityCode;// 三字码


	/** 获取中文城市名称 */
	public static String getCityName(String code) {
		if (code == null)
			return code;
		String cityName = "";
		try {
			cityName = (String) mpCityName.get(code.toUpperCase().trim());
		} catch (Exception e) {

		}
		if (cityName == null)
			return code;
		else
			return cityName;
	}

	
	public static void loadStaticData() {
		Connection con = getConnection();
		try {
			Statement stmt = con.createStatement();
			String sqlStr = "select code_f, name from city";
			ResultSet rs = stmt.executeQuery(sqlStr);
			mpCityName = new HashMap();
			mpCityCode = new HashMap();
			String cityName = "";
			String cityCode = "";
			while (rs.next()) {
				cityCode = rs.getString(1).toUpperCase();
				cityName = rs.getString(2);
				mpCityName.put(cityCode, cityName);
				mpCityCode.put(cityName, cityCode);
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (SQLException exp) {
			Utility.logDBError(exp);
		} catch (Exception ex) {
			Utility.logError(ex);
		}
	}

	/**
	 * 获取数据库连接
	 */
	public static Connection getConnection() {
		try {
			InitialContext initial = new InitialContext();
			// 使连接池初始化
			DataSource dataSource = (DataSource) initial
					.lookup(Constants.JDBC_DATASOURCE_URL);
			Connection con = dataSource.getConnection();
			return con;
		} catch (SQLException exp) {
			logDBError(exp);
			return null;
		} catch (Exception ex) {
			logError(ex);
			return null;
		}
	}

	/**
	 * 记录异常
	 */
	public static synchronized void logError(Exception exp) {
		StackTraceElement ste[] = exp.getStackTrace();
		StringBuffer content = new StringBuffer();
		content.append("[" + exp.toString() + "]\n");
		for (int i = 0; i < ste.length; i++) {
			content.append("  " + ste[i].toString() + "\n");
		}
		log.error(content);
	}

	/**
	 * 记录数据库异常
	 */
	public static synchronized void logDBError(Exception exp) {
		StackTraceElement ste[] = exp.getStackTrace();
		StringBuffer content = new StringBuffer();
		content.append(exp.toString());
		for (int i = 0; i < ste.length; i++) {
			content.append("  " + ste[i].toString() + "\n");
		}
		log.error(content);
	}

	/**
	 * 加载静态数据
	 */
	static {
		try {
			loadStaticData();
		} catch (Exception ex) {
			logError(ex);
		}
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值