Commit 67754b96e6faa40b2b8175ea69c0786b38b40ce2

Authored by zlz
1 parent 541bd234

添加车载上报停靠站数据库配置文件

src/main/java/com/bsth/service/traffic/impl/VehicleInoutStopServiceImpl.java
... ... @@ -5,10 +5,9 @@ import com.bsth.service.impl.BaseServiceImpl;
5 5 import com.bsth.service.impl.TrafficManageServiceImpl;
6 6 import com.bsth.service.traffic.VehicleInoutStopService;
7 7 import com.bsth.util.DateUtils;
8   -import com.bsth.util.db.DBUtils_MS;
  8 +import com.bsth.util.db.DBUtils_traffic;
9 9 import org.slf4j.Logger;
10 10 import org.slf4j.LoggerFactory;
11   -import org.springframework.beans.factory.annotation.Autowired;
12 11 import org.springframework.jdbc.core.JdbcTemplate;
13 12 import org.springframework.stereotype.Service;
14 13  
... ... @@ -76,7 +75,7 @@ public class VehicleInoutStopServiceImpl extends BaseServiceImpl<VehicleInoutSto
76 75 * @return
77 76 */
78 77 public List<Map<String, Object>> getVehicleInoutStopByParam(Map<String,Object> map){
79   - JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_MS.getDataSource());
  78 + JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_traffic.getDataSource());
80 79 String sql = "SELECT branche_company,company,name,shanghai_linecode,inside_code,equipment_code,car_plate," +
81 80 "if( service_state= 0,'营运','停运') as service_state ," +
82 81 "if( up_down= 0,'上行','下行') as up_down ," +
... ... @@ -93,7 +92,7 @@ public class VehicleInoutStopServiceImpl extends BaseServiceImpl&lt;VehicleInoutSto
93 92 * @return
94 93 */
95 94 public long getVehicleInoutStopCountByParam(Map<String,Object> map){
96   - JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_MS.getDataSource());
  95 + JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_traffic.getDataSource());
97 96 String sql = "SELECT count(1) COUNT FROM bsth_c_shreal" + packageParam(map,"count");
98 97 long result = Long.valueOf(jdbcTemp.queryForMap(sql).get("COUNT")+"");
99 98 logger.info("车载上报停靠站查询count-sql:"+sql);
... ...
src/main/java/com/bsth/util/db/DBUtils_traffic.java 0 → 100644
  1 +package com.bsth.util.db;
  2 +
  3 +import com.mchange.v2.c3p0.DataSources;
  4 +import org.apache.log4j.Logger;
  5 +
  6 +import javax.sql.DataSource;
  7 +import java.io.FileNotFoundException;
  8 +import java.io.IOException;
  9 +import java.sql.Connection;
  10 +import java.sql.ResultSet;
  11 +import java.sql.SQLException;
  12 +import java.sql.Statement;
  13 +import java.util.HashMap;
  14 +import java.util.Map;
  15 +import java.util.Properties;
  16 +
  17 +/**
  18 + * 网关ms库连接池
  19 + * @author PanZhao
  20 + *
  21 + */
  22 +//@Component
  23 +public class DBUtils_traffic {
  24 +
  25 + private static String url = null;
  26 +
  27 + private static String username = null;
  28 +
  29 + private static String pwd = null;
  30 +
  31 + private static DataSource ds_pooled;
  32 +
  33 + static Logger logger = Logger.getLogger(DBUtils_traffic.class);
  34 +
  35 + static {
  36 + Properties env = new Properties();
  37 +
  38 + try {
  39 + env.load(DBUtils_traffic.class.getClassLoader().getResourceAsStream("traffic-jdbc.properties"));
  40 + // 1. 加载驱动类
  41 + Class.forName(env.getProperty("ms.mysql.driver"));
  42 +
  43 + url = env.getProperty("ms.mysql.url");
  44 + username = env.getProperty("ms.mysql.username");
  45 + pwd = env.getProperty("ms.mysql.password");
  46 +
  47 + // 设置连接数据库的配置信息
  48 + DataSource ds_unpooled = DataSources.unpooledDataSource(url,
  49 + username, pwd);
  50 +
  51 + Map<String, Object> pool_conf = new HashMap<String, Object>();
  52 + // 设置最大连接数
  53 + pool_conf.put("maxPoolSize", 10);
  54 +
  55 + pool_conf.put("testConnectionOnCheckout", false);
  56 + //异步检测连接的有效性
  57 + pool_conf.put("testConnectionOnCheckin", true);
  58 + //30秒检测一次
  59 + pool_conf.put("idleConnectionTestPeriod", 30);
  60 + ds_pooled = DataSources.pooledDataSource(ds_unpooled, pool_conf);
  61 + } catch (FileNotFoundException e) {
  62 + logger.error(e.toString());
  63 + e.printStackTrace();
  64 + } catch (IOException e) {
  65 + logger.error(e.toString());
  66 + e.printStackTrace();
  67 + } catch (ClassNotFoundException e) {
  68 + logger.error(e.toString());
  69 + e.printStackTrace();
  70 + } catch (SQLException e) {
  71 + logger.error(e.toString());
  72 + e.printStackTrace();
  73 + }
  74 + }
  75 +
  76 + /**
  77 + * 获取连接对象
  78 + */
  79 + public static Connection getConnection() throws SQLException {
  80 + return ds_pooled.getConnection();
  81 + }
  82 +
  83 + /**
  84 + * 释放连接池资源
  85 + */
  86 + public static void clearup() {
  87 + if (ds_pooled != null) {
  88 + try {
  89 + DataSources.destroy(ds_pooled);
  90 + } catch (SQLException e) {
  91 + logger.error(e.toString());
  92 + e.printStackTrace();
  93 + }
  94 + }
  95 + }
  96 +
  97 + /**
  98 + * 资源关闭
  99 + *
  100 + * @param rs
  101 + * @param stmt
  102 + * @param conn
  103 + */
  104 + public static void close(ResultSet rs, Statement stmt, Connection conn) {
  105 + if (rs != null) {
  106 + try {
  107 + rs.close();
  108 + } catch (SQLException e) {
  109 + logger.error(e.toString());
  110 + e.printStackTrace();
  111 + }
  112 + }
  113 +
  114 + if (stmt != null) {
  115 + try {
  116 + stmt.close();
  117 + } catch (SQLException e) {
  118 + logger.error(e.toString());
  119 + e.printStackTrace();
  120 + }
  121 + }
  122 +
  123 + if (conn != null) {
  124 + try {
  125 + conn.close();
  126 + } catch (SQLException e) {
  127 + logger.error(e.toString());
  128 + e.printStackTrace();
  129 + }
  130 + }
  131 + }
  132 +
  133 + public static DataSource getDataSource(){
  134 + return ds_pooled;
  135 + }
  136 +}
... ...
src/main/resources/traffic-jdbc.properties 0 → 100644
  1 +#ms.mysql.driver= com.mysql.jdbc.Driver
  2 +#ms.mysql.url= jdbc:mysql://127.0.0.1/pd_control?useUnicode=true&characterEncoding=utf-8&useSSL=false
  3 +#ms.mysql.username= root
  4 +#ms.mysql.password= 123456
  5 +
  6 +ms.mysql.driver= com.mysql.jdbc.Driver
  7 +ms.mysql.url= jdbc:mysql://10.10.150.21:3306/ms?useUnicode=true&characterEncoding=utf-8
  8 +ms.mysql.username= root
  9 +ms.mysql.password= root2jsp@JSP
  10 +
... ...