DBUtils_station.java
5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.bsth.util.db;
import com.bsth.data.BasicData;
import com.bsth.data.forecast.entity.ArrivalEntity;
import com.bsth.entity.StationMatchData;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* 站点行业编码库库连接池
* @author YouRuiFeng
*
*/
//@Component
public class DBUtils_station {
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_station.class);
static {
Properties env = new Properties();
try {
env.load(DBUtils_station.class.getClassLoader().getResourceAsStream("station-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);
}*/
}
}