質問編集履歴

1

コード修正

2019/04/08 09:58

投稿

boobooboo
boobooboo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -10,334 +10,162 @@
10
10
 
11
11
  コード載せます。
12
12
 
13
+
14
+
15
+ 追記 だいぶすっきりしました。
16
+
17
+ whileのところができません。
18
+
13
19
  よろしくお願いします。
14
20
 
15
21
 
16
22
 
17
- ```ここimport java.io.BufferedReader;
18
-
19
- import java.io.File;
20
-
21
- import java.io.FileReader;
22
-
23
- import java.io.FileWriter;
24
-
25
- import java.io.IOException;
26
-
27
- import java.sql.Connection;
28
-
29
- import java.sql.PreparedStatement;
30
-
31
- import java.sql.ResultSet;
32
-
33
- import java.sql.SQLException;
34
-
35
- import java.text.DateFormat;
36
-
37
- import java.text.ParseException;
38
-
39
- import java.util.ArrayList;
40
-
41
- import java.util.Calendar;
42
-
43
- import java.util.Date;
44
-
45
- import java.util.List;
46
-
47
-
48
-
49
- public class Main {
50
-
51
-
52
-
53
- /* 事前準備(日付計算メソッド) */
54
-
55
- public static int differenceDays(String strDate1, String strDate2)
56
-
57
- throws ParseException {
58
-
59
- Date date1 = DateFormat.getDateInstance().parse(strDate1);
60
-
61
- Date date2 = DateFormat.getDateInstance().parse(strDate2);
62
-
63
- return differenceDays(date1, date2);
64
-
65
- }
66
-
67
-
68
-
69
- public static int differenceDays(Date date1, Date date2) {
70
-
71
- long datetime1 = date1.getTime();
72
-
73
- long datetime2 = date2.getTime();
74
-
75
- long one_date_time = 1000 * 60 * 60 * 24;
76
-
77
- long diffDays = (datetime1 - datetime2) / one_date_time;
78
-
79
- return (int) diffDays;
80
-
81
- }
82
-
83
-
84
-
85
- public static int differenceMonth(String strDate3, String strDate4) throws ParseException {
86
-
87
- Date date3 = DateFormat.getDateInstance().parse(strDate3);
88
-
89
- Date date4 = DateFormat.getDateInstance().parse(strDate4);
90
-
91
- return differenceMonth(date3, date4);
92
-
93
- }
94
-
95
-
96
-
97
- public static int differenceMonth(Date date3, Date date4) {
98
-
99
- Calendar cal1 = Calendar.getInstance();
100
-
101
- cal1.setTime(date3);
102
-
103
- cal1.set(Calendar.DATE, 1);
104
-
105
- Calendar cal2 = Calendar.getInstance();
106
-
107
- cal2.setTime(date4);
108
-
109
- cal2.set(Calendar.DATE, 1);
110
-
111
- int count = 0;
112
-
113
- if (cal1.before(cal2)) {
114
-
115
- while (cal1.before(cal2)) {
116
-
117
- cal1.add(Calendar.MONTH, 1);
118
-
119
- count--;
120
-
121
- }
122
-
123
- } else {
124
-
125
- count--;
126
-
127
- while (!cal1.before(cal2)) {
128
-
129
- cal1.add(Calendar.MONTH, -1);
130
-
131
- count++;
132
-
133
- }
23
+ ```ここpublic static void main(String[] args) throws IOException, SQLException, ParseException {
24
+
25
+
26
+
27
+ File file1 = new File("c:\Output");
28
+
29
+
30
+
31
+ if (file1.mkdir()) {
32
+
33
+ System.out.println("フォルダの作成に成功しました");
34
+
35
+ } else {
36
+
37
+ System.out.println("フォルダの作成に失敗しました");
38
+
39
+ }
40
+
41
+
42
+
43
+
44
+
45
+ Connection con = Dao.getConnection();
46
+
47
+
48
+
49
+ PreparedStatement pstmt = con.prepareStatement("SELECT* FROM employee_info WHERE employee_id >= ?");
50
+
51
+
52
+
53
+ pstmt.setInt(1, 0);
54
+
55
+
56
+
57
+ ResultSet rs;
58
+
59
+
60
+
61
+ rs = pstmt.executeQuery();
62
+
63
+ /* インスタンス化、ファイルの書き出し */
64
+
65
+ List<Entity> list = new ArrayList<Entity>();
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+ FileWriter f = new FileWriter("C:\Output\before.csv", true);
74
+
75
+ Entity entity = new Entity();
76
+
77
+
78
+
79
+ while (rs.next()) {
80
+
81
+
82
+
83
+ entity.setEmployee_id(rs.getInt("employee_id"));
84
+
85
+ entity.setName(rs.getString("name"));
86
+
87
+ entity.setName_hiragana(rs.getString("name_hiragana"));
88
+
89
+ entity.setBirthday(rs.getString("birthday"));
90
+
91
+ entity.setSex(rs.getInt("sex"));
92
+
93
+ entity.setMail_address(rs.getString("mail_address"));
94
+
95
+ entity.setTelephone_number(rs.getString("telephone_number"));
96
+
97
+ entity.setCompany_info_id(rs.getInt("company_info_id"));
98
+
99
+ entity.setHire_date(rs.getString("hire_date"));
100
+
101
+ entity.setEnrollment_year(rs.getString("enrollment_year"));
102
+
103
+ entity.setEnrollment_month(rs.getString("enrollment_month"));
104
+
105
+ entity.setEnrollment_day(rs.getString("enrollment_day"));
106
+
107
+
108
+
109
+ list.add(entity);
110
+
111
+
112
+
113
+ f.write(String.valueOf(entity.getEmployee_id()));
114
+
115
+ f.write(",");
116
+
117
+ f.write(entity.getName());
118
+
119
+ f.write(",");
120
+
121
+ f.write(entity.getName_hiragana());
122
+
123
+ f.write(",");
124
+
125
+ f.write(entity.getBirthday());
126
+
127
+ f.write(",");
128
+
129
+ f.write(String.valueOf(entity.getSex()));
130
+
131
+ f.write(",");
132
+
133
+ f.write(entity.getMail_address());
134
+
135
+ f.write(",");
136
+
137
+ f.write(entity.getTelephone_number());
138
+
139
+ f.write(",");
140
+
141
+ f.write(String.valueOf(entity.getCompany_info_id()));
142
+
143
+ f.write(",");
144
+
145
+ f.write(entity.getHire_date());
146
+
147
+ f.write(",");
148
+
149
+ f.write(entity.getEnrollment_year());
150
+
151
+ f.write(",");
152
+
153
+ f.write(entity.getEnrollment_month());
154
+
155
+ f.write(",");
156
+
157
+ f.write(entity.getEnrollment_day());
158
+
159
+ f.write(",");
160
+
161
+ f.flush();
162
+
163
+ f.close();
164
+
165
+
134
166
 
135
167
  }
136
168
 
137
- return count;
138
-
139
-
140
-
141
- }
142
-
143
- /* dao呼び出し、データベース読み込み */
144
-
145
- public static void main(String[] args) throws IOException, SQLException, ParseException {
146
-
147
-
148
-
149
- File file1 = new File("c:\Output");
150
-
151
-
152
-
153
- if (file1.mkdir()) {
154
-
155
- System.out.println("フォルダの作成に成功しました");
156
-
157
- } else {
158
-
159
- System.out.println("フォルダの作成に失敗しました");
160
-
161
- }
162
-
163
-
164
-
165
-
166
-
167
- Connection con = Dao.getConnection();
168
-
169
-
170
-
171
- PreparedStatement pstmt = con.prepareStatement("SELECT* FROM employee_info WHERE employee_id >= ?");
172
-
173
-
174
-
175
- pstmt.setInt(1, 0);
176
-
177
-
178
-
179
- ResultSet rs;
180
-
181
-
182
-
183
- rs = pstmt.executeQuery();
184
-
185
- /* インスタンス化、ファイルの書き出し */
186
-
187
- List<Entity> list = new ArrayList<Entity>();
188
-
189
-
190
-
191
- while (rs.next()) {
192
-
193
-
194
-
195
- FileWriter f = new FileWriter("C:\Output\before.csv", true);
196
-
197
- Entity entity = new Entity();
198
-
199
-
200
-
201
-
202
-
203
- f.write("employee_id");
204
-
205
- f.write(",");
206
-
207
- f.write("name");
208
-
209
-
210
-
211
-
212
-
213
- entity.setEmployee_id(rs.getInt("employee_id"));
214
-
215
- list.add(entity);
216
-
217
- f.write(String.valueOf(entity.getEmployee_id()));
218
-
219
- f.write(",");
220
-
221
-
222
-
223
- entity.setName(rs.getString("name"));
224
-
225
- list.add(entity);
226
-
227
- f.write(entity.getName());
228
-
229
- f.write(",");
230
-
231
-
232
-
233
- entity.setName_hiragana(rs.getString("name_hiragana"));
234
-
235
- list.add(entity);
236
-
237
- f.write(entity.getName_hiragana());
238
-
239
- f.write(",");
240
-
241
-
242
-
243
- entity.setBirthday(rs.getString("birthday"));
244
-
245
- list.add(entity);
246
-
247
- f.write(entity.getBirthday());
248
-
249
- f.write(",");
250
-
251
-
252
-
253
- entity.setSex(rs.getInt("sex"));
254
-
255
- list.add(entity);
256
-
257
- f.write(String.valueOf(entity.getSex()));
258
-
259
- f.write(",");
260
-
261
-
262
-
263
- entity.setMail_address(rs.getString("mail_address"));
264
-
265
- list.add(entity);
266
-
267
- f.write(entity.getMail_address());
268
-
269
- f.write(",");
270
-
271
-
272
-
273
- entity.setTelephone_number(rs.getString("telephone_number"));
274
-
275
- list.add(entity);
276
-
277
- f.write(entity.getTelephone_number());
278
-
279
- f.write(",");
280
-
281
-
282
-
283
- entity.setCompany_info_id(rs.getInt("company_info_id"));
284
-
285
- list.add(entity);
286
-
287
- f.write(String.valueOf(entity.getCompany_info_id()));
288
-
289
- f.write(",");
290
-
291
-
292
-
293
- entity.setHire_date(rs.getString("hire_date"));
294
-
295
- list.add(entity);
296
-
297
- f.write(entity.getHire_date());
298
-
299
- f.write(",");
300
-
301
-
302
-
303
- entity.setEnrollment_year(rs.getString("enrollment_year"));
304
-
305
- list.add(entity);
306
-
307
- f.write(entity.getEnrollment_year());
308
-
309
- f.write(",");
310
-
311
-
312
-
313
- entity.setEnrollment_month(rs.getString("enrollment_month"));
314
-
315
- list.add(entity);
316
-
317
- f.write(entity.getEnrollment_month());
318
-
319
- f.write(",");
320
-
321
-
322
-
323
- entity.setEnrollment_day(rs.getString("enrollment_day"));
324
-
325
- list.add(entity);
326
-
327
- f.write(entity.getEnrollment_day());
328
-
329
- f.write(",");
330
-
331
-
332
-
333
- f.flush();
334
-
335
- f.close();
336
-
337
-
338
-
339
- }
340
-
341
169
 
342
170
 
343
171
  rs.close();
@@ -386,107 +214,77 @@
386
214
 
387
215
  // 読み込んでリスト化したもので入社日を取り出す
388
216
 
217
+ String[] HireDate = new String[5];
218
+
389
- String HireDate1 = data[8];
219
+ HireDate[0] = data[8];
390
-
220
+
391
- String HireDate2 = data[20];
221
+ HireDate[1] = data[20];
392
-
222
+
393
- String HireDate3 = data[32];
223
+ HireDate[2] = data[32];
394
-
224
+
395
- String HireDate4 = data[44];
225
+ HireDate[3] = data[44];
396
-
226
+
397
- String HireDate5 = data[56];
227
+ HireDate[4] = data[56];
228
+
229
+ String[] ReHireDate = new String[5];
230
+
231
+ for(int i = 0; i < HireDate.length; i++) {
232
+
233
+ ReHireDate[i] = HireDate[i].replace("-", "/");
234
+
235
+ System.out.println(ReHireDate[i]);
236
+
237
+ }
398
238
 
399
239
 
400
240
 
401
241
  // 置換
402
242
 
243
+ int[] Day = new int[5];
244
+
245
+ for(int i = 0; i < Day.length; i++) {
246
+
403
- String ReHireDate1 = HireDate1.replace("-", "/");
247
+ Day[i] = differenceDays(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate[i]);
248
+
249
+ System.out.println(Day[i]);
250
+
251
+ }
404
252
 
405
253
 
406
254
 
255
+ int[] Mon = new int[5];
256
+
257
+ for(int i = 0; i < Mon.length; i++) {
258
+
259
+ Mon[i] = differenceMonth(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate[i]);
260
+
261
+ System.out.println(Mon[i]);
262
+
263
+ }
264
+
407
265
 
408
266
 
267
+ int[] Year = new int[5];
268
+
409
- String ReHireDate2 = HireDate2.replace("-", "/");
269
+ for(int i = 0; i <Year.length; i++) {
410
-
411
- String ReHireDate3 = HireDate3.replace("-", "/");
270
+
412
-
413
- String ReHireDate4 = HireDate4.replace("-", "/");
414
-
415
- String ReHireDate5 = HireDate5.replace("-", "/");
416
-
417
- // 計算
418
-
419
- int day1 = differenceDays(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate1);
420
-
421
-
422
-
423
- int day2 = differenceDays(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate2);
424
-
425
-
426
-
427
- int day3 = differenceDays(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate3);
428
-
429
-
430
-
431
- int day4 = differenceDays(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate4);
432
-
433
-
434
-
435
- int day5 = differenceDays(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate5);
436
-
437
-
438
-
439
- int mon1 = differenceMonth(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate1);
440
-
441
-
442
-
443
- int mon2 = differenceMonth(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate2);
444
-
445
-
446
-
447
- int mon3 = differenceMonth(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate3);
448
-
449
-
450
-
451
- int mon4 = differenceMonth(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate4);
452
-
453
-
454
-
455
- int mon5 = differenceMonth(StrYear + "/" + StrMonth + "/" + StrDay, ReHireDate5);
456
-
457
-
458
-
459
- int year1 = mon1 / 12;
271
+ Year[i] = Mon[i]/ 12;
272
+
460
-
273
+ System.out.println(Year[i]);
274
+
275
+ }
276
+
277
+ int[] YearMonth = new int[5];
278
+
279
+ for(int i = 0; i <YearMonth.length; i++) {
280
+
461
- int YearMonth1 = mon1 % 12;
281
+ YearMonth[i] = Mon[i]% 12;
462
-
463
-
464
-
465
-
466
-
467
- int year2 = mon2 / 12;
282
+
468
-
469
- int YearMonth2 = mon2 % 12;
283
+ System.out.println(YearMonth[i]);
470
-
471
-
472
-
284
+
473
- int year3 = mon3 / 12;
285
+ }
474
-
475
- int YearMonth3 = mon3 % 12;
286
+
476
-
477
-
478
-
479
- int year4 = mon4 / 12;
287
+
480
-
481
- int YearMonth4 = mon4 % 12;
482
-
483
-
484
-
485
- int year5 = mon5 / 12;
486
-
487
- int YearMonth5 = mon5 % 12;
488
-
489
-
490
288
 
491
289
  con.setAutoCommit(false);
492
290
 
@@ -498,92 +296,140 @@
498
296
 
499
297
 
500
298
 
299
+ int[] r = new int[5];
300
+
301
+ for(int i = 0; i <r.length; i++) {
302
+
501
- pstmt.setString(1, year1 + "年" + YearMonth1 + "か月");
303
+ pstmt.setString(1, Year[i] + "年" + YearMonth[i] + "か月");
502
-
304
+
503
- if (YearMonth1 == 0){
305
+ if (YearMonth[i] == 0){
504
-
306
+
505
- pstmt.setString(1, year1+ "年");
307
+ pstmt.setString(1, Year[i]+ "年");
506
-
507
- }
508
-
509
- pstmt.setInt(2, mon1);
510
-
511
- pstmt.setInt(3, day1);
512
-
513
- pstmt.setInt(4, 1);
514
-
515
- pstmt.executeUpdate();
516
-
517
-
518
-
519
- pstmt.setString(1, year2 + "年" + YearMonth2 + "か月");
520
-
521
- if (YearMonth2 == 0){
522
-
523
- pstmt.setString(1, year2+ "年");
524
308
 
525
309
  }
526
310
 
527
- pstmt.setInt(2, mon2);
311
+ pstmt.setInt(2, Mon[i]);
528
-
312
+
529
- pstmt.setInt(3, day2);
313
+ pstmt.setInt(3, Day[i]);
530
-
314
+
531
- pstmt.setInt(4, 2);
315
+ pstmt.setInt(4, i);
532
-
316
+
533
- pstmt.executeUpdate();
317
+ pstmt.executeUpdate();
534
-
535
-
536
-
537
- pstmt.setString(1, year3 + "年" + YearMonth3 + "か月");
318
+
538
-
539
- if (YearMonth3 == 0){
319
+ System.out.println(r[i]);
540
-
541
- pstmt.setString(1, year3+ "年");
542
320
 
543
321
  }
544
322
 
545
- pstmt.setInt(2, mon3);
546
-
547
- pstmt.setInt(3, day3);
548
-
549
- pstmt.setInt(4, 3);
550
-
551
- pstmt.executeUpdate();
552
-
553
-
554
-
555
- pstmt.setString(1, year4 + "年" + YearMonth4 + "か月");
556
-
557
- if (YearMonth4 == 0){
558
-
559
- pstmt.setString(1, year4+ "年");
560
-
561
- }
562
-
563
- pstmt.setInt(2, mon4);
564
-
565
- pstmt.setInt(3, day4);
566
-
567
- pstmt.setInt(4, 4);
568
-
569
- pstmt.executeUpdate();
570
-
571
-
572
-
573
- pstmt.setString(1, year5 + "年" + YearMonth5 + "か月");
574
-
575
- if (YearMonth5 == 0){
576
-
577
- pstmt.setString(1, year5+ "年");
578
-
579
- }
580
-
581
- pstmt.setInt(2, mon5);
582
-
583
- pstmt.setInt(3, day5);
584
-
585
- pstmt.setInt(4, 5);
586
-
587
- pstmt.executeUpdate();ード
323
+
324
+
325
+ /* 更新したデータベースを再度読み込み */
326
+
327
+ pstmt = con.prepareStatement("SELECT* FROM employee_info WHERE employee_id >= ?");
328
+
329
+
330
+
331
+ pstmt.setInt(1, 0);
332
+
333
+
334
+
335
+ rs = pstmt.executeQuery();
336
+
337
+ /* 読み込んだものをファイルに書き込み */
338
+
339
+ while (rs.next()) {
340
+
341
+
342
+
343
+ new FileWriter(
344
+
345
+ "C:\\Output\after.csv", true);
346
+
347
+
348
+
349
+ entity.setEmployee_id(rs.getInt("employee_id"));
350
+
351
+ entity.setName(rs.getString("name"));
352
+
353
+ entity.setName_hiragana(rs.getString("name_hiragana"));
354
+
355
+ entity.setBirthday(rs.getString("birthday"));
356
+
357
+ entity.setSex(rs.getInt("sex"));
358
+
359
+ entity.setMail_address(rs.getString("mail_address"));
360
+
361
+ entity.setTelephone_number(rs.getString("telephone_number"));
362
+
363
+ entity.setCompany_info_id(rs.getInt("company_info_id"));
364
+
365
+ entity.setHire_date(rs.getString("hire_date"));
366
+
367
+ entity.setEnrollment_year(rs.getString("enrollment_year"));
368
+
369
+ entity.setEnrollment_month(rs.getString("enrollment_month"));
370
+
371
+ entity.setEnrollment_day(rs.getString("enrollment_day"));
372
+
373
+
374
+
375
+ list.add(entity);
376
+
377
+
378
+
379
+ f.write(String.valueOf(entity.getEmployee_id()));
380
+
381
+ f.write(",");
382
+
383
+ f.write(entity.getName());
384
+
385
+ f.write(",");
386
+
387
+ f.write(entity.getName_hiragana());
388
+
389
+ f.write(",");
390
+
391
+ f.write(entity.getBirthday());
392
+
393
+ f.write(",");
394
+
395
+ f.write(String.valueOf(entity.getSex()));
396
+
397
+ f.write(",");
398
+
399
+ f.write(entity.getMail_address());
400
+
401
+ f.write(",");
402
+
403
+ f.write(entity.getTelephone_number());
404
+
405
+ f.write(",");
406
+
407
+ f.write(String.valueOf(entity.getCompany_info_id()));
408
+
409
+ f.write(",");
410
+
411
+ f.write(entity.getHire_date());
412
+
413
+ f.write(",");
414
+
415
+ f.write(entity.getEnrollment_year());
416
+
417
+ f.write(",");
418
+
419
+ f.write(entity.getEnrollment_month());
420
+
421
+ f.write(",");
422
+
423
+ f.write(entity.getEnrollment_day());
424
+
425
+ f.write(",");
426
+
427
+
428
+
429
+ f.flush();
430
+
431
+ f.close();
432
+
433
+ ード
588
434
 
589
435
  ```