DBUtils_control.java 4.94 KB
package com.bsth.util.db;

import com.mchange.v2.c3p0.DataSources;
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 站点行业编码库库连接池
 * @author YouRuiFeng
 *
 */
//@Component
public class DBUtils_control {

	private static String url = null;

	private static String username = null;

	private static String pwd = null;

	private static DataSource ds_pooled;

	static Logger logger = Logger.getLogger(DBUtils_control.class);

	static {
		Properties env = new Properties();

		try {
			env.load(DBUtils_control.class.getClassLoader().getResourceAsStream("control-jdbc.properties"));
			// 1. 加载驱动类
			Class.forName(env.getProperty("station.mysql.driver"));

			url = env.getProperty("station.mysql.url");
			username = env.getProperty("station.mysql.username");
			pwd = env.getProperty("station.mysql.password");

			// 设置连接数据库的配置信息
			DataSource ds_unpooled = DataSources.unpooledDataSource(url,
					username, pwd);

			Map<String, Object> pool_conf = new HashMap<String, Object>();
			// 设置最大连接数
			pool_conf.put("maxPoolSize", 10);
			
			pool_conf.put("testConnectionOnCheckout", false);
			//异步检测连接的有效性
			pool_conf.put("testConnectionOnCheckin", true);
			//30秒检测一次
			pool_conf.put("idleConnectionTestPeriod", 30);
			ds_pooled = DataSources.pooledDataSource(ds_unpooled, pool_conf);
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
			e.printStackTrace();
		} catch (IOException e) {
			logger.error(e.toString());
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			logger.error(e.toString());
			e.printStackTrace();
		} catch (SQLException e) {
			logger.error(e.toString());
			e.printStackTrace();
		}
	}

	/**
	 * 获取连接对象
	 */
	public static Connection getConnection() throws SQLException {
		return ds_pooled.getConnection();
	}

	/**
	 * 释放连接池资源
	 */
	public static void clearup() {
		if (ds_pooled != null) {
			try {
				DataSources.destroy(ds_pooled);
			} catch (SQLException e) {
				logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	/**
	 * 资源关闭
	 * 
	 * @param rs
	 * @param stmt
	 * @param conn
	 */
	public static void close(ResultSet rs, Statement stmt, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error(e.toString());
				e.printStackTrace();
			}
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				logger.error(e.toString());
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	public static DataSource getDataSource(){
		return ds_pooled;
	}


	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		/*List<StationMatchData> listMD = new ArrayList<>();
		String sql = "select * from roadlinestop ORDER BY RoteLine,LineStandardCode,UpStream,LevelId";
		try{
			conn = DBUtils_station.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				StationMatchData arr = new StationMatchData();
				arr.setRoadLine(rs.getString("RoadLine"));
				arr.setLineStandardCode(rs.getString("LineStandardCode"));
				arr.setStationName(rs.getString("StationName"));
				arr.setStationStandardCode(rs.getString("StationStandardCode"));
				arr.setUpStream(Integer.parseInt(rs.getString("UpStream")));
				arr.setLevelId(rs.getString("LevelId"));
				arr.setStationType(rs.getString("StationType"));

				listMD.add(arr);
			}
			Map<String, Map<String, List<StationMatchData>>> mapsMD = new HashMap<>();
            for (StationMatchData s:listMD) {
                String key = s.getRoadLine()+"_"+s.getLineStandardCode();
                int dir = s.getUpStream();
                if(mapsMD.containsKey(key)){
                    Map<String, List<StationMatchData>> map = mapsMD.get(key);
                    if(mapsMD.containsKey(dir)){
                        List<StationMatchData> lists = map.get(dir);
                        lists.add(s);
                    } else {
                        List<StationMatchData> lists = new ArrayList<>();
                        lists.add(s);
                        map.put(dir+"",lists);
                    }
                } else {
                    Map<String, List<StationMatchData>> map = new HashMap<>();
                    List<StationMatchData> lists = new ArrayList<>();
                    lists.add(s);
                    map.put(dir+"",lists);
                    mapsMD.put(key,map);
                }
            }
            System.out.println(mapsMD);
		}catch(Exception e){
			logger.error("", e);
		}finally {
			DBUtils_station.close(rs, ps, conn);
		}*/
	}
}