-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImportProvince.java
184 lines (169 loc) · 5.25 KB
/
ImportProvince.java
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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import dbc.DataBaseConnection;
public class ImportProvince {
public static void main(String[] args) {
// TODO Auto-generated method stub
XSSFWorkbook hw;
try {
hw = new XSSFWorkbook(new FileInputStream("province.xlsx"));
XSSFSheet hsheet = hw.getSheetAt(1);
importTotalData(hsheet);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void importTotalData(XSSFSheet hsheet) {
int rows = hsheet.getPhysicalNumberOfRows();
int flag = 0;
int province = 0;
for (int i = 3; i < 38; i++) {
province = i - 3;
XSSFRow hrow = hsheet.getRow(i);
for (int j = 2; j <= 85; j++) {
String date = "2017";
int month = j / 7 + 1;
if(month < 10) {
date = date + "0" + month;
} else {
date = date + month;
}
XSSFCell zyszbcell = hrow.getCell(j);
String zyszb = getCellValue(zyszbcell);
j++;
XSSFCell ARPUcell = hrow.getCell(j);
String ARPU = getCellValue(ARPUcell);
j++;
XSSFCell cxywzdwcell = hrow.getCell(j);
String cxywzdw = getCellValue(cxywzdwcell);
j++;
XSSFCell cxywzjkcell = hrow.getCell(j);
String cxywzjk = getCellValue(cxywzjkcell);
j++;
XSSFCell ydjczjkcell = hrow.getCell(j);
String ydjczjk = getCellValue(ydjczjkcell);
j++;
XSSFCell gwjczjkcell = hrow.getCell(j);
String gwjczjk = getCellValue(gwjczjkcell);
j++;
XSSFCell zlsrzdwcell = hrow.getCell(j);
String zlsrzdw = getCellValue(zlsrzdwcell);
System.out.println(zyszb+ " " +ARPU +" " +cxywzdw + " " + cxywzjk + " " + ydjczjk+ " " + gwjczjk+ " " + zlsrzdw + " " + date);
importdata(zyszb,ARPU ,cxywzdw,cxywzjk,ydjczjk,gwjczjk,zlsrzdw,date,province + "");
}
}
}
public static void importdata(String zysrzb,String ARPU,String cxywzdw,String cxywzjk,String ydjczjk,String gwjczjk,String zlsrzdw,String date,String province) {
try {
double zysrzbDouble = Double.parseDouble(zysrzb) * 100;
zysrzb = String.format("%.1f", zysrzbDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double cxywzdwDouble = Double.parseDouble(cxywzdw) * 100;
cxywzdw = String.format("%.1f", cxywzdwDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double cxywzjkDouble = Double.parseDouble(cxywzjk) * 100;
cxywzjk = String.format("%.1f", cxywzjkDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double ydjczjkDouble = Double.parseDouble(ydjczjk) * 100;
ydjczjk = String.format("%.1f", ydjczjkDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double gwjczjkDouble = Double.parseDouble(gwjczjk) * 100;
gwjczjk = String.format("%.1f", gwjczjkDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double zlsrzdwDouble = Double.parseDouble(zlsrzdw) * 100;
zlsrzdw = String.format("%.1f", zlsrzdwDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double ARPUDouble = Double.parseDouble(ARPU);
ARPU = String.format("%.1f", ARPUDouble);
} catch(Exception e) {
e.printStackTrace();
}
String sql = "update `provincesubjecttotal_copy` set zysrzb = ?,ARPU = ?,cxywzdw=?,cxywzjk=?,ydjczjk=?,gwjczjk=?,zlsrzdw=? where subject = 0 and province = ? and month = ?";
System.out.println(sql);
Connection conn = DataBaseConnection.getConnection();
PreparedStatement prep = null;
try {
prep = conn.prepareStatement(sql);
prep.setString(1,zysrzb);
prep.setString(2, ARPU);
prep.setString(3, cxywzdw);
prep.setString(4, cxywzjk);
prep.setString(5, ydjczjk);
prep.setString(6, gwjczjk);
prep.setString(7, zlsrzdw);
prep.setString(8, province);
prep.setString(9, date);
prep.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (prep != null)
prep.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
DataBaseConnection.returnConnection(conn);
}
}
public static String getCellValue(XSSFCell cell) {
String value = "0.0";
try {
if (cell != null) {
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_FORMULA:
try {
value = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
try {
value = String.valueOf(cell.getRichStringCellValue());
} catch (Exception ex) {
value = "error";
}
}
break;
case XSSFCell.CELL_TYPE_NUMERIC:
value = String.valueOf(cell.getNumericCellValue());
break;
case XSSFCell.CELL_TYPE_STRING:
value = String.valueOf(cell.getRichStringCellValue());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
value = "0.0";
}
if(value == null)
value = "0.0";
return value;
}
}