-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathResultData.java
More file actions
88 lines (76 loc) · 1.99 KB
/
ResultData.java
File metadata and controls
88 lines (76 loc) · 1.99 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
package core.db;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
/**
* ResultSet Wrapper
* Created by johngrib on 2017. 4. 22..
*/
public class ResultData {
final private List<String> labels;
private ResultSet rs = null;
public ResultData(final ResultSet rs) throws SQLException {
this.rs = rs;
this.labels = getColumnLabels(rs);
}
/**
* column labels를 리턴한다.
* @return
*/
public List<String> getLabels() {
return labels;
}
/**
* column lable 을 수집한다.
* @param rs
* @return
*/
private List<String> getColumnLabels(final ResultSet rs) {
try {
final int size = rs.getMetaData().getColumnCount();
final List<String> list = new ArrayList<>(size);
final ResultSetMetaData meta = rs.getMetaData();
for (int i = 1; i <= size; i++) {
String name = meta.getColumnName(i);
list.add(name);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return Collections.EMPTY_LIST;
}
public ResultSet getResultSet() {
return rs;
}
public void close() {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
}
public boolean next() {
try {
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public Map<String, Object> getDataMap() {
try {
final Map<String, Object> data = new HashMap<>();
for (String label : labels) {
data.put(label, rs.getObject(label));
}
return data;
} catch (SQLException e) {
e.printStackTrace();
}
return Collections.EMPTY_MAP;
}
}