質問編集履歴

16

DAOクラスの94行目から96行目でutil.Dateをsql.Dateに変換しました。

2021/12/15 07:19

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
  import java.sql.Time;
56
56
 
57
- import java.sql.Date;
57
+ import java.util.Date;
58
58
 
59
59
  import java.util.ArrayList;
60
60
 
@@ -216,7 +216,13 @@
216
216
 
217
217
  pstmt.setInt(1, btd.getId());
218
218
 
219
+ Date date = btd.getDate();
220
+
221
+ long timeInMilliSeconds = date.getTime();
222
+
223
+ java.sql.Date date1 = new java.sql.Date(timeInMilliSeconds);
224
+
219
- pstmt.setDate(2, btd.getDate());
225
+ pstmt.setDate(2, date1);
220
226
 
221
227
  pstmt.setString(3, btd.getStoreName());
222
228
 
@@ -230,6 +236,8 @@
230
236
 
231
237
  pstmt.executeUpdate();
232
238
 
239
+ System.out.println("Suucessfully added");
240
+
233
241
  pstmt.close();
234
242
 
235
243
 

15

BudgetTrackerDao更新

2021/12/15 07:19

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -54,524 +54,524 @@
54
54
 
55
55
  import java.sql.Time;
56
56
 
57
+ import java.sql.Date;
58
+
59
+ import java.util.ArrayList;
60
+
61
+ import java.util.List;
62
+
63
+ import java.util.Properties;
64
+
65
+
66
+
67
+ import com.jdbc.budgettracker.core.BudgetTrackerDto;
68
+
69
+
70
+
71
+ public class BudgetTrackerDao {
72
+
73
+ BudgetTrackerDto btd;
74
+
75
+ private Connection myConn;
76
+
77
+ private PreparedStatement mySmt;
78
+
79
+
80
+
81
+ private static final String SELECTALL = "select * from budget_table;";
82
+
83
+ // private static final String SELECTBYSTORENAME = "select * from budget_table where store name = ?;";
84
+
85
+
86
+
87
+ private static Connection getConnection() {
88
+
89
+
90
+
91
+ try {
92
+
93
+ // get db properties
94
+
95
+ Properties props = new Properties();
96
+
97
+ props.load(new FileInputStream(
98
+
99
+ "/home/yosuke/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties"));
100
+
101
+
102
+
103
+ String user = props.getProperty("user");
104
+
105
+ String password = props.getProperty("password");
106
+
107
+ String dburl = props.getProperty("dburl");
108
+
109
+ // Class.forName(RDB_DRIVE);
110
+
111
+ Connection con = DriverManager.getConnection(dburl, user, password);
112
+
113
+ return con;
114
+
115
+ } catch (Exception e) {
116
+
117
+ throw new IllegalStateException(e);
118
+
119
+ }
120
+
121
+ }
122
+
123
+
124
+
125
+ // select all
126
+
127
+ public List<BudgetTrackerDto> selectAll() throws FileNotFoundException, IOException {
128
+
129
+ List<BudgetTrackerDto> budgetList = new ArrayList<>();
130
+
131
+
132
+
133
+ try (Connection conn = BudgetTrackerDao.getConnection();
134
+
135
+ PreparedStatement ps = conn.prepareStatement(SELECTALL)) {
136
+
137
+
138
+
139
+ try (ResultSet rs = ps.executeQuery()) {
140
+
141
+ while (rs.next()) {
142
+
143
+ btd = new BudgetTrackerDto();
144
+
145
+ btd.setId(rs.getInt("ID"));
146
+
147
+ btd.setDate(rs.getDate("Date"));
148
+
149
+ btd.setStoreName(rs.getString("StoreName"));
150
+
151
+ btd.setProductName(rs.getString("ProductName"));
152
+
153
+ btd.setProductType(rs.getString("ProductType"));
154
+
155
+ btd.setPrice(rs.getInt("Price"));
156
+
157
+
158
+
159
+ budgetList.add(btd);
160
+
161
+ }
162
+
163
+ }
164
+
165
+ } catch (SQLException e) {
166
+
167
+ e.printStackTrace();
168
+
169
+ } catch (Exception e) {
170
+
171
+ e.printStackTrace();
172
+
173
+ } finally {
174
+
175
+ }
176
+
177
+ return budgetList;
178
+
179
+ }
180
+
181
+
182
+
183
+
184
+
185
+ public int insertIntoTable(BudgetTrackerDto btd) throws SQLException {
186
+
187
+ int rowsCount = 0;
188
+
189
+
190
+
191
+ PreparedStatement pstmt;
192
+
193
+ try {
194
+
195
+ // DBに接続
196
+
197
+ myConn = BudgetTrackerDao.getConnection();
198
+
199
+ // pstmt = (PreparedStatement) myConn.createStatement();
200
+
201
+
202
+
203
+ // String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE('"
204
+
205
+ // + btd.getId() + "','" + btd.getDate() + "','" + btd.getStoreName() + "','" + btd.getProductName() + "','" + btd.getProductType()
206
+
207
+ // + "','" + btd.getPrice() + "')";
208
+
209
+
210
+
211
+ String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)";
212
+
213
+
214
+
215
+ pstmt = myConn.prepareStatement(sql);
216
+
217
+ pstmt.setInt(1, btd.getId());
218
+
219
+ pstmt.setDate(2, btd.getDate());
220
+
221
+ pstmt.setString(3, btd.getStoreName());
222
+
223
+ pstmt.setString(4, btd.getProductName());
224
+
225
+ pstmt.setString(5, btd.getProductType());
226
+
227
+ pstmt.setInt(6, btd.getPrice());
228
+
229
+ // SQL文発行
230
+
231
+ pstmt.executeUpdate();
232
+
233
+ pstmt.close();
234
+
235
+
236
+
237
+ //rowsCount = pstmt.executeUpdate(sql);
238
+
239
+ } catch (SQLException e) {
240
+
241
+ System.out.println("Errorが発生しました!\n" + e + "\n");
242
+
243
+ } finally {
244
+
245
+ // リソースの開放
246
+
247
+ if (mySmt != null) {
248
+
249
+ try {
250
+
251
+ mySmt.close();
252
+
253
+ } catch (SQLException ignore) {
254
+
255
+ }
256
+
257
+ }
258
+
259
+ if (myConn != null) {
260
+
261
+ try {
262
+
263
+ myConn.close();
264
+
265
+ } catch (SQLException ignore) {
266
+
267
+ }
268
+
269
+ }
270
+
271
+ }
272
+
273
+
274
+
275
+ return 0;
276
+
277
+
278
+
279
+ }
280
+
281
+
282
+
283
+ }
284
+
285
+ ```
286
+
287
+ BudgetTrackerDto.java
288
+
289
+ ```ここに言語を入力
290
+
291
+
292
+
293
+ package com.jdbc.budgettracker.core;
294
+
295
+
296
+
57
297
  import java.util.Date;
58
298
 
299
+
300
+
301
+ public class BudgetTrackerDto {
302
+
303
+ private int id;
304
+
305
+ private Date date;
306
+
307
+ private String storeName;
308
+
309
+ private String productName;
310
+
311
+ private String productType;
312
+
313
+ private int price;
314
+
315
+
316
+
317
+ public BudgetTrackerDto() {
318
+
319
+ }
320
+
321
+
322
+
323
+ public BudgetTrackerDto(int id, Date date, String storeName, String productName, String productType, int price) {
324
+
325
+ super();
326
+
327
+ this.id = id;
328
+
329
+ this.date = date;
330
+
331
+ this.storeName = storeName;
332
+
333
+ this.productName = productName;
334
+
335
+ this.productType = productType;
336
+
337
+ this.price = price;
338
+
339
+ }
340
+
341
+
342
+
343
+ public int getId() {
344
+
345
+ return id;
346
+
347
+ }
348
+
349
+
350
+
351
+ public void setId(int id) {
352
+
353
+ this.id = id;
354
+
355
+ }
356
+
357
+
358
+
359
+ public Date getDate() {
360
+
361
+ return date;
362
+
363
+ }
364
+
365
+
366
+
367
+ public void setDate(Date insertDate) {
368
+
369
+ this.date = insertDate;
370
+
371
+ }
372
+
373
+
374
+
375
+ public String getStoreName() {
376
+
377
+ return storeName;
378
+
379
+ }
380
+
381
+
382
+
383
+ public void setStoreName(String storeName) {
384
+
385
+ this.storeName = storeName;
386
+
387
+ }
388
+
389
+
390
+
391
+ public String getProductName() {
392
+
393
+ return productName;
394
+
395
+ }
396
+
397
+
398
+
399
+ public void setProductName(String productName) {
400
+
401
+ this.productName = productName;
402
+
403
+ }
404
+
405
+
406
+
407
+ public String getProductType() {
408
+
409
+ return productType;
410
+
411
+ }
412
+
413
+
414
+
415
+ public void setProductType(String productType) {
416
+
417
+ this.productType = productType;
418
+
419
+ }
420
+
421
+
422
+
423
+ public int getPrice() {
424
+
425
+ return price;
426
+
427
+ }
428
+
429
+
430
+
431
+ public void setPrice(int price) {
432
+
433
+ this.price = price;
434
+
435
+ }
436
+
437
+
438
+
439
+ }
440
+
441
+
442
+
443
+ ```
444
+
445
+ BudgetTrackerMain.java
446
+
447
+ ```ここに言語を入力
448
+
449
+ package com.jdbc.budgettracker.main;
450
+
451
+
452
+
453
+ import java.io.FileNotFoundException;
454
+
455
+ import java.io.IOException;
456
+
457
+ import java.sql.SQLException;
458
+
459
+ import java.util.Date;
460
+
461
+ import java.text.ParseException;
462
+
463
+ import java.text.SimpleDateFormat;
464
+
59
- import java.util.ArrayList;
465
+ import java.util.HashMap;
60
466
 
61
467
  import java.util.List;
62
468
 
469
+ import java.util.Map;
470
+
63
- import java.util.Properties;
471
+ import java.util.Scanner;
64
472
 
65
473
 
66
474
 
67
475
  import com.jdbc.budgettracker.core.BudgetTrackerDto;
68
476
 
69
-
70
-
71
- public class BudgetTrackerDao {
72
-
73
- BudgetTrackerDto btd;
74
-
75
- private Connection myConn;
76
-
77
- private PreparedStatement mySmt;
78
-
79
-
80
-
81
- private static final String SELECTALL = "select * from budget_table;";
82
-
83
- // private static final String SELECTBYSTORENAME = "select * from budget_table where store name = ?;";
84
-
85
-
86
-
87
- private static Connection getConnection() {
88
-
89
-
90
-
91
- try {
92
-
93
- // get db properties
94
-
95
- Properties props = new Properties();
96
-
97
- props.load(new FileInputStream(
98
-
99
- "/home/user/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties"));
100
-
101
-
102
-
103
- String user = props.getProperty("user");
104
-
105
- String password = props.getProperty("password");
106
-
107
- String dburl = props.getProperty("dburl");
108
-
109
- // Class.forName(RDB_DRIVE);
110
-
111
- Connection con = DriverManager.getConnection(dburl, user, password);
112
-
113
- return con;
114
-
115
- } catch (Exception e) {
116
-
117
- throw new IllegalStateException(e);
477
+ import com.jdbc.budgettracker.dao.BudgetTrackerDao;
478
+
479
+
480
+
481
+ public class BudgetTrackerMain {
482
+
483
+
484
+
485
+ 一部省略
486
+
487
+
488
+
489
+ switch (initialNumInt) {
490
+
491
+
492
+
493
+ case 2:
494
+
495
+ // Insert
496
+
497
+ int insertcannerInt = 0;
498
+
499
+ System.out.println("You chose " + initialSwitchMap.get(2));
500
+
501
+ budgetTrackerDto = new BudgetTrackerDto();
502
+
503
+
504
+
505
+ Scanner insertScanner = new Scanner(System.in);
506
+
507
+ System.out.print("Input an ID: ");
508
+
509
+ int insertScannerInt = insertScanner.nextInt();
510
+
511
+ budgetTrackerDto.setId(insertScannerInt);
512
+
513
+
514
+
515
+ System.out.print("Input Date (yyyy-MM-dd): ");
516
+
517
+ String insertScannerStr = insertScanner.next();
518
+
519
+ Date insertDate=(Date) new SimpleDateFormat("yyyy-MM-dd").parse(insertScannerStr);
520
+
521
+ budgetTrackerDto.setDate(insertDate);
522
+
523
+
524
+
525
+ System.out.print("Input a store name: ");
526
+
527
+ insertScannerStr = insertScanner.next();
528
+
529
+ budgetTrackerDto.setStoreName(insertScannerStr);
530
+
531
+
532
+
533
+ System.out.print("Input a product name: ");
534
+
535
+ insertScannerStr = insertScanner.next();
536
+
537
+ budgetTrackerDto.setProductName(insertScannerStr);
538
+
539
+
540
+
541
+ System.out.print("Input a product type: ");
542
+
543
+ insertScannerStr = insertScanner.next();
544
+
545
+ budgetTrackerDto.setProductType(insertScannerStr);
546
+
547
+
548
+
549
+ System.out.print("Input price: ");
550
+
551
+ insertScannerStr = insertScanner.next();
552
+
553
+ insertcannerInt = Integer.parseInt(insertScannerStr);
554
+
555
+ budgetTrackerDto.setPrice(insertcannerInt);
556
+
557
+
558
+
559
+ budgetTrackerDao = new BudgetTrackerDao();
560
+
561
+ budgetTrackerDao.insertIntoTable(budgetTrackerDto);
562
+
563
+
118
564
 
119
565
  }
120
566
 
567
+
568
+
121
569
  }
122
570
 
123
-
124
-
125
- // select all
126
-
127
- public List<BudgetTrackerDto> selectAll() throws FileNotFoundException, IOException {
128
-
129
- List<BudgetTrackerDto> budgetList = new ArrayList<>();
130
-
131
-
132
-
133
- try (Connection conn = BudgetTrackerDao.getConnection();
134
-
135
- PreparedStatement ps = conn.prepareStatement(SELECTALL)) {
136
-
137
-
138
-
139
- try (ResultSet rs = ps.executeQuery()) {
140
-
141
- while (rs.next()) {
142
-
143
- btd = new BudgetTrackerDto();
144
-
145
- btd.setId(rs.getInt("ID"));
146
-
147
- btd.setDate(rs.getDate("Date"));
148
-
149
- btd.setStoreName(rs.getString("StoreName"));
150
-
151
- btd.setProductName(rs.getString("ProductName"));
152
-
153
- btd.setProductType(rs.getString("ProductType"));
154
-
155
- btd.setPrice(rs.getInt("Price"));
156
-
157
-
158
-
159
- budgetList.add(btd);
160
-
161
- }
162
-
163
- }
164
-
165
- } catch (SQLException e) {
166
-
167
- e.printStackTrace();
168
-
169
- } catch (Exception e) {
170
-
171
- e.printStackTrace();
172
-
173
- } finally {
174
-
175
- }
176
-
177
- return budgetList;
178
-
179
- }
180
-
181
-
182
-
183
-
184
-
185
- public int insertIntoTable(BudgetTrackerDto btd) throws SQLException {
186
-
187
- int rowsCount = 0;
188
-
189
-
190
-
191
- PreparedStatement pstmt;
192
-
193
- try {
194
-
195
- // DBに接続
196
-
197
- myConn = BudgetTrackerDao.getConnection();
198
-
199
- // pstmt = (PreparedStatement) myConn.createStatement();
200
-
201
-
202
-
203
- // String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE('"
204
-
205
- // + btd.getId() + "','" + btd.getDate() + "','" + btd.getStoreName() + "','" + btd.getProductName() + "','" + btd.getProductType()
206
-
207
- // + "','" + btd.getPrice() + "')";
208
-
209
-
210
-
211
- String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)";
212
-
213
-
214
-
215
- pstmt = myConn.prepareStatement(sql);
216
-
217
- pstmt.setInt(1, btd.getId());
218
-
219
- pstmt.setDate(2, (java.sql.Date) btd.getDate());
220
-
221
- pstmt.setString(3, btd.getStoreName());
222
-
223
- pstmt.setString(4, btd.getProductName());
224
-
225
- pstmt.setString(5, btd.getProductType());
226
-
227
- pstmt.setInt(6, btd.getPrice());
228
-
229
- // SQL文発行
230
-
231
- pstmt.executeUpdate();
232
-
233
- pstmt.close();
234
-
235
-
236
-
237
- //rowsCount = pstmt.executeUpdate(sql);
238
-
239
- } catch (SQLException e) {
240
-
241
- System.out.println("Errorが発生しました!\n" + e + "\n");
242
-
243
- } finally {
244
-
245
- // リソースの開放
246
-
247
- if (mySmt != null) {
248
-
249
- try {
250
-
251
- mySmt.close();
252
-
253
- } catch (SQLException ignore) {
254
-
255
- }
256
-
257
- }
258
-
259
- if (myConn != null) {
260
-
261
- try {
262
-
263
- myConn.close();
264
-
265
- } catch (SQLException ignore) {
266
-
267
- }
268
-
269
- }
270
-
271
- }
272
-
273
-
274
-
275
- return 0;
276
-
277
-
278
-
279
- }
280
-
281
-
282
-
283
571
  }
284
572
 
285
573
  ```
286
574
 
287
- BudgetTrackerDto.java
288
-
289
- ```ここに言語を入力
290
-
291
-
292
-
293
- package com.jdbc.budgettracker.core;
294
-
295
-
296
-
297
- import java.util.Date;
298
-
299
-
300
-
301
- public class BudgetTrackerDto {
302
-
303
- private int id;
304
-
305
- private Date date;
306
-
307
- private String storeName;
308
-
309
- private String productName;
310
-
311
- private String productType;
312
-
313
- private int price;
314
-
315
-
316
-
317
- public BudgetTrackerDto() {
318
-
319
- }
320
-
321
-
322
-
323
- public BudgetTrackerDto(int id, Date date, String storeName, String productName, String productType, int price) {
324
-
325
- super();
326
-
327
- this.id = id;
328
-
329
- this.date = date;
330
-
331
- this.storeName = storeName;
332
-
333
- this.productName = productName;
334
-
335
- this.productType = productType;
336
-
337
- this.price = price;
338
-
339
- }
340
-
341
-
342
-
343
- public int getId() {
344
-
345
- return id;
346
-
347
- }
348
-
349
-
350
-
351
- public void setId(int id) {
352
-
353
- this.id = id;
354
-
355
- }
356
-
357
-
358
-
359
- public Date getDate() {
360
-
361
- return date;
362
-
363
- }
364
-
365
-
366
-
367
- public void setDate(Date insertDate) {
368
-
369
- this.date = insertDate;
370
-
371
- }
372
-
373
-
374
-
375
- public String getStoreName() {
376
-
377
- return storeName;
378
-
379
- }
380
-
381
-
382
-
383
- public void setStoreName(String storeName) {
384
-
385
- this.storeName = storeName;
386
-
387
- }
388
-
389
-
390
-
391
- public String getProductName() {
392
-
393
- return productName;
394
-
395
- }
396
-
397
-
398
-
399
- public void setProductName(String productName) {
400
-
401
- this.productName = productName;
402
-
403
- }
404
-
405
-
406
-
407
- public String getProductType() {
408
-
409
- return productType;
410
-
411
- }
412
-
413
-
414
-
415
- public void setProductType(String productType) {
416
-
417
- this.productType = productType;
418
-
419
- }
420
-
421
-
422
-
423
- public int getPrice() {
424
-
425
- return price;
426
-
427
- }
428
-
429
-
430
-
431
- public void setPrice(int price) {
432
-
433
- this.price = price;
434
-
435
- }
436
-
437
-
438
-
439
- }
440
-
441
-
442
-
443
- ```
444
-
445
- BudgetTrackerMain.java
446
-
447
- ```ここに言語を入力
448
-
449
- package com.jdbc.budgettracker.main;
450
-
451
-
452
-
453
- import java.io.FileNotFoundException;
454
-
455
- import java.io.IOException;
456
-
457
- import java.sql.SQLException;
458
-
459
- import java.util.Date;
460
-
461
- import java.text.ParseException;
462
-
463
- import java.text.SimpleDateFormat;
464
-
465
- import java.util.HashMap;
466
-
467
- import java.util.List;
468
-
469
- import java.util.Map;
470
-
471
- import java.util.Scanner;
472
-
473
-
474
-
475
- import com.jdbc.budgettracker.core.BudgetTrackerDto;
476
-
477
- import com.jdbc.budgettracker.dao.BudgetTrackerDao;
478
-
479
-
480
-
481
- public class BudgetTrackerMain {
482
-
483
-
484
-
485
- 一部省略
486
-
487
-
488
-
489
- switch (initialNumInt) {
490
-
491
-
492
-
493
- case 2:
494
-
495
- // Insert
496
-
497
- int insertcannerInt = 0;
498
-
499
- System.out.println("You chose " + initialSwitchMap.get(2));
500
-
501
- budgetTrackerDto = new BudgetTrackerDto();
502
-
503
-
504
-
505
- Scanner insertScanner = new Scanner(System.in);
506
-
507
- System.out.print("Input an ID: ");
508
-
509
- int insertScannerInt = insertScanner.nextInt();
510
-
511
- budgetTrackerDto.setId(insertScannerInt);
512
-
513
-
514
-
515
- System.out.print("Input Date (yyyy-MM-dd): ");
516
-
517
- String insertScannerStr = insertScanner.next();
518
-
519
- Date insertDate=(Date) new SimpleDateFormat("yyyy-MM-dd").parse(insertScannerStr);
520
-
521
- budgetTrackerDto.setDate(insertDate);
522
-
523
-
524
-
525
- System.out.print("Input a store name: ");
526
-
527
- insertScannerStr = insertScanner.next();
528
-
529
- budgetTrackerDto.setStoreName(insertScannerStr);
530
-
531
-
532
-
533
- System.out.print("Input a product name: ");
534
-
535
- insertScannerStr = insertScanner.next();
536
-
537
- budgetTrackerDto.setProductName(insertScannerStr);
538
-
539
-
540
-
541
- System.out.print("Input a product type: ");
542
-
543
- insertScannerStr = insertScanner.next();
544
-
545
- budgetTrackerDto.setProductType(insertScannerStr);
546
-
547
-
548
-
549
- System.out.print("Input price: ");
550
-
551
- insertScannerStr = insertScanner.next();
552
-
553
- insertcannerInt = Integer.parseInt(insertScannerStr);
554
-
555
- budgetTrackerDto.setPrice(insertcannerInt);
556
-
557
-
558
-
559
- budgetTrackerDao = new BudgetTrackerDao();
560
-
561
- budgetTrackerDao.insertIntoTable(budgetTrackerDto);
562
-
563
-
564
-
565
- }
566
-
567
-
568
-
569
- }
570
-
571
- }
572
-
573
- ```
574
-
575
575
 
576
576
 
577
577
  画像 2021/12/15/11:49

14

エラー内容 2021/12/15 15:49更新

2021/12/15 06:53

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -8,11 +8,15 @@
8
8
 
9
9
 
10
10
 
11
- エラー内容 2021/12/15 15:31
11
+ エラー内容 2021/12/15 15:49
12
12
 
13
13
  ```ここに言語を入力
14
14
 
15
- Exception in thread "main" java.lang.ClassCastException: class java.util.Date cannot be cast to class java.sql.Date (java.util.Date is in module java.base of loader 'bootstrap'; java.sql.Date is in module java.sql of loader 'platform')
15
+ Exception in thread "main" java.lang.Error: Unresolved compilation problem:
16
+
17
+ The method setDate(int, java.sql.Date) in the type PreparedStatement is not applicable for the arguments (int, java.util.Date)
18
+
19
+
16
20
 
17
21
  at BudgetTrackerCui/com.jdbc.budgettracker.dao.BudgetTrackerDao.insertIntoTable(BudgetTrackerDao.java:93)
18
22
 

13

BudgetTrackerMain.javaも一部記載

2021/12/15 06:49

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -438,6 +438,136 @@
438
438
 
439
439
  ```
440
440
 
441
+ BudgetTrackerMain.java
442
+
443
+ ```ここに言語を入力
444
+
445
+ package com.jdbc.budgettracker.main;
446
+
447
+
448
+
449
+ import java.io.FileNotFoundException;
450
+
451
+ import java.io.IOException;
452
+
453
+ import java.sql.SQLException;
454
+
455
+ import java.util.Date;
456
+
457
+ import java.text.ParseException;
458
+
459
+ import java.text.SimpleDateFormat;
460
+
461
+ import java.util.HashMap;
462
+
463
+ import java.util.List;
464
+
465
+ import java.util.Map;
466
+
467
+ import java.util.Scanner;
468
+
469
+
470
+
471
+ import com.jdbc.budgettracker.core.BudgetTrackerDto;
472
+
473
+ import com.jdbc.budgettracker.dao.BudgetTrackerDao;
474
+
475
+
476
+
477
+ public class BudgetTrackerMain {
478
+
479
+
480
+
481
+ 一部省略
482
+
483
+
484
+
485
+ switch (initialNumInt) {
486
+
487
+
488
+
489
+ case 2:
490
+
491
+ // Insert
492
+
493
+ int insertcannerInt = 0;
494
+
495
+ System.out.println("You chose " + initialSwitchMap.get(2));
496
+
497
+ budgetTrackerDto = new BudgetTrackerDto();
498
+
499
+
500
+
501
+ Scanner insertScanner = new Scanner(System.in);
502
+
503
+ System.out.print("Input an ID: ");
504
+
505
+ int insertScannerInt = insertScanner.nextInt();
506
+
507
+ budgetTrackerDto.setId(insertScannerInt);
508
+
509
+
510
+
511
+ System.out.print("Input Date (yyyy-MM-dd): ");
512
+
513
+ String insertScannerStr = insertScanner.next();
514
+
515
+ Date insertDate=(Date) new SimpleDateFormat("yyyy-MM-dd").parse(insertScannerStr);
516
+
517
+ budgetTrackerDto.setDate(insertDate);
518
+
519
+
520
+
521
+ System.out.print("Input a store name: ");
522
+
523
+ insertScannerStr = insertScanner.next();
524
+
525
+ budgetTrackerDto.setStoreName(insertScannerStr);
526
+
527
+
528
+
529
+ System.out.print("Input a product name: ");
530
+
531
+ insertScannerStr = insertScanner.next();
532
+
533
+ budgetTrackerDto.setProductName(insertScannerStr);
534
+
535
+
536
+
537
+ System.out.print("Input a product type: ");
538
+
539
+ insertScannerStr = insertScanner.next();
540
+
541
+ budgetTrackerDto.setProductType(insertScannerStr);
542
+
543
+
544
+
545
+ System.out.print("Input price: ");
546
+
547
+ insertScannerStr = insertScanner.next();
548
+
549
+ insertcannerInt = Integer.parseInt(insertScannerStr);
550
+
551
+ budgetTrackerDto.setPrice(insertcannerInt);
552
+
553
+
554
+
555
+ budgetTrackerDao = new BudgetTrackerDao();
556
+
557
+ budgetTrackerDao.insertIntoTable(budgetTrackerDto);
558
+
559
+
560
+
561
+ }
562
+
563
+
564
+
565
+ }
566
+
567
+ }
568
+
569
+ ```
570
+
441
571
 
442
572
 
443
573
  画像 2021/12/15/11:49

12

BudgetTrackerDto.javaを修正

2021/12/15 06:46

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -284,6 +284,8 @@
284
284
 
285
285
  ```ここに言語を入力
286
286
 
287
+
288
+
287
289
  package com.jdbc.budgettracker.core;
288
290
 
289
291
 
@@ -430,7 +432,11 @@
430
432
 
431
433
 
432
434
 
435
+ }
436
+
437
+
438
+
433
- }```
439
+ ```
434
440
 
435
441
 
436
442
 

11

BudgetTrackerDto.javaを追加

2021/12/15 06:37

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -280,6 +280,160 @@
280
280
 
281
281
  ```
282
282
 
283
+ BudgetTrackerDto.java
284
+
285
+ ```ここに言語を入力
286
+
287
+ package com.jdbc.budgettracker.core;
288
+
289
+
290
+
291
+ import java.util.Date;
292
+
293
+
294
+
295
+ public class BudgetTrackerDto {
296
+
297
+ private int id;
298
+
299
+ private Date date;
300
+
301
+ private String storeName;
302
+
303
+ private String productName;
304
+
305
+ private String productType;
306
+
307
+ private int price;
308
+
309
+
310
+
311
+ public BudgetTrackerDto() {
312
+
313
+ }
314
+
315
+
316
+
317
+ public BudgetTrackerDto(int id, Date date, String storeName, String productName, String productType, int price) {
318
+
319
+ super();
320
+
321
+ this.id = id;
322
+
323
+ this.date = date;
324
+
325
+ this.storeName = storeName;
326
+
327
+ this.productName = productName;
328
+
329
+ this.productType = productType;
330
+
331
+ this.price = price;
332
+
333
+ }
334
+
335
+
336
+
337
+ public int getId() {
338
+
339
+ return id;
340
+
341
+ }
342
+
343
+
344
+
345
+ public void setId(int id) {
346
+
347
+ this.id = id;
348
+
349
+ }
350
+
351
+
352
+
353
+ public Date getDate() {
354
+
355
+ return date;
356
+
357
+ }
358
+
359
+
360
+
361
+ public void setDate(Date insertDate) {
362
+
363
+ this.date = insertDate;
364
+
365
+ }
366
+
367
+
368
+
369
+ public String getStoreName() {
370
+
371
+ return storeName;
372
+
373
+ }
374
+
375
+
376
+
377
+ public void setStoreName(String storeName) {
378
+
379
+ this.storeName = storeName;
380
+
381
+ }
382
+
383
+
384
+
385
+ public String getProductName() {
386
+
387
+ return productName;
388
+
389
+ }
390
+
391
+
392
+
393
+ public void setProductName(String productName) {
394
+
395
+ this.productName = productName;
396
+
397
+ }
398
+
399
+
400
+
401
+ public String getProductType() {
402
+
403
+ return productType;
404
+
405
+ }
406
+
407
+
408
+
409
+ public void setProductType(String productType) {
410
+
411
+ this.productType = productType;
412
+
413
+ }
414
+
415
+
416
+
417
+ public int getPrice() {
418
+
419
+ return price;
420
+
421
+ }
422
+
423
+
424
+
425
+ public void setPrice(int price) {
426
+
427
+ this.price = price;
428
+
429
+ }
430
+
431
+
432
+
433
+ }```
434
+
435
+
436
+
283
437
  画像 2021/12/15/11:49
284
438
 
285
439
  ![![イメージ説明](023e9f1da0bd0eb74ad4625964ededcf.png)](7963446809efa5fd7c31cdd56cb9dd98.png)

10

エラー内容 2021/12/15 15:31追記

2021/12/15 06:36

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -8,13 +8,15 @@
8
8
 
9
9
 
10
10
 
11
- エラー内容 2021/12/15
11
+ エラー内容 2021/12/15 15:31
12
12
 
13
13
  ```ここに言語を入力
14
14
 
15
- Exception in thread "main" java.lang.ClassCastException: class com.mysql.cj.jdbc.StatementImpl cannot be cast to class java.sql.PreparedStatement (com.mysql.cj.jdbc.StatementImpl is in unnamed module of loader 'app'; java.sql.PreparedStatement is in module java.sql of loader 'platform')
16
-
17
- at BudgetTrackerCui/com.jdbc.budgettracker.dao.BudgetTrackerDao.insertIntoTable(BudgetTrackerDao.java:82)
15
+ Exception in thread "main" java.lang.ClassCastException: class java.util.Date cannot be cast to class java.sql.Date (java.util.Date is in module java.base of loader 'bootstrap'; java.sql.Date is in module java.sql of loader 'platform')
16
+
17
+ at BudgetTrackerCui/com.jdbc.budgettracker.dao.BudgetTrackerDao.insertIntoTable(BudgetTrackerDao.java:93)
18
+
19
+ at BudgetTrackerCui/com.jdbc.budgettracker.main.BudgetTrackerMain.main(BudgetTrackerMain.java:130)
18
20
 
19
21
  ```
20
22
 

9

93行目をpstmt.setDate(2, (java.sql.Date) btd.getDate());に変更

2021/12/15 06:31

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -48,6 +48,8 @@
48
48
 
49
49
  import java.sql.Time;
50
50
 
51
+ import java.util.Date;
52
+
51
53
  import java.util.ArrayList;
52
54
 
53
55
  import java.util.List;
@@ -88,7 +90,7 @@
88
90
 
89
91
  props.load(new FileInputStream(
90
92
 
91
- "/home/yosuke/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties"));
93
+ "/home/user/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties"));
92
94
 
93
95
 
94
96
 
@@ -174,11 +176,11 @@
174
176
 
175
177
 
176
178
 
177
- public int insertIntoTable(BudgetTrackerDto budgetTrackerDto) throws SQLException {
179
+ public int insertIntoTable(BudgetTrackerDto btd) throws SQLException {
178
180
 
179
181
  int rowsCount = 0;
180
182
 
181
- btd = new BudgetTrackerDto();
183
+
182
184
 
183
185
  PreparedStatement pstmt;
184
186
 
@@ -208,7 +210,7 @@
208
210
 
209
211
  pstmt.setInt(1, btd.getId());
210
212
 
211
- pstmt.setTime(2, (Time) btd.getDate());
213
+ pstmt.setDate(2, (java.sql.Date) btd.getDate());
212
214
 
213
215
  pstmt.setString(3, btd.getStoreName());
214
216
 

8

画像「2021/12/15/12:21」を添付

2021/12/15 06:29

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -279,3 +279,9 @@
279
279
  画像 2021/12/15/11:49
280
280
 
281
281
  ![![イメージ説明](023e9f1da0bd0eb74ad4625964ededcf.png)](7963446809efa5fd7c31cdd56cb9dd98.png)
282
+
283
+
284
+
285
+ 画像 2021/12/15/12:21
286
+
287
+ ![イメージ説明](9e2c388c8c09173d78b5d794aac14219.png)

7

画像を挿入しました。

2021/12/15 03:22

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -275,3 +275,7 @@
275
275
  }
276
276
 
277
277
  ```
278
+
279
+ 画像 2021/12/15/11:49
280
+
281
+ ![![イメージ説明](023e9f1da0bd0eb74ad4625964ededcf.png)](7963446809efa5fd7c31cdd56cb9dd98.png)

6

90行目から96行目を更新

2021/12/15 02:49

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -188,7 +188,7 @@
188
188
 
189
189
  myConn = BudgetTrackerDao.getConnection();
190
190
 
191
-  //pstmt = (PreparedStatement) myConn.createStatement();
191
+ // pstmt = (PreparedStatement) myConn.createStatement();
192
192
 
193
193
 
194
194
 
@@ -206,21 +206,21 @@
206
206
 
207
207
  pstmt = myConn.prepareStatement(sql);
208
208
 
209
- (pstmt).setInt(1, btd.getId());
209
+ pstmt.setInt(1, btd.getId());
210
-
210
+
211
- (pstmt).setTime(2, (Time) btd.getDate());
211
+ pstmt.setTime(2, (Time) btd.getDate());
212
-
212
+
213
- (pstmt).setString(3, btd.getStoreName());
213
+ pstmt.setString(3, btd.getStoreName());
214
-
214
+
215
- (pstmt).setString(4, btd.getProductName());
215
+ pstmt.setString(4, btd.getProductName());
216
-
216
+
217
- (pstmt).setString(5, btd.getProductType());
217
+ pstmt.setString(5, btd.getProductType());
218
-
218
+
219
- (pstmt).setInt(6, btd.getPrice());
219
+ pstmt.setInt(6, btd.getPrice());
220
220
 
221
221
  // SQL文発行
222
222
 
223
- pstmt.executeUpdate(sql);
223
+ pstmt.executeUpdate();
224
224
 
225
225
  pstmt.close();
226
226
 

5

82行目をコメントアウト

2021/12/15 02:42

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -188,7 +188,7 @@
188
188
 
189
189
  myConn = BudgetTrackerDao.getConnection();
190
190
 
191
- pstmt = (PreparedStatement) myConn.createStatement();
191
+  //pstmt = (PreparedStatement) myConn.createStatement();
192
192
 
193
193
 
194
194
 

4

エラー内容更新

2021/12/15 02:37

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -8,16 +8,20 @@
8
8
 
9
9
 
10
10
 
11
- エラー内容
11
+ エラー内容 2021/12/15
12
12
 
13
13
  ```ここに言語を入力
14
14
 
15
- java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
15
+ Exception in thread "main" java.lang.ClassCastException: class com.mysql.cj.jdbc.StatementImpl cannot be cast to class java.sql.PreparedStatement (com.mysql.cj.jdbc.StatementImpl is in unnamed module of loader 'app'; java.sql.PreparedStatement is in module java.sql of loader 'platform')
16
+
17
+ at BudgetTrackerCui/com.jdbc.budgettracker.dao.BudgetTrackerDao.insertIntoTable(BudgetTrackerDao.java:82)
16
18
 
17
19
  ```
18
20
 
19
21
 
20
22
 
23
+
24
+
21
25
  BudgetTrackerDao.java
22
26
 
23
27
  ```ここに言語を入力

3

PreparedStatementに統一

2021/12/15 02:25

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -42,8 +42,6 @@
42
42
 
43
43
  import java.sql.SQLException;
44
44
 
45
- import java.sql.Statement;
46
-
47
45
  import java.sql.Time;
48
46
 
49
47
  import java.util.ArrayList;
@@ -64,11 +62,13 @@
64
62
 
65
63
  private Connection myConn;
66
64
 
67
- private Statement mySmt;
65
+ private PreparedStatement mySmt;
68
-
69
-
70
-
66
+
67
+
68
+
71
- private static final String SQL = "select * from budget_table;";
69
+ private static final String SELECTALL = "select * from budget_table;";
70
+
71
+ // private static final String SELECTBYSTORENAME = "select * from budget_table where store name = ?;";
72
72
 
73
73
 
74
74
 
@@ -84,7 +84,7 @@
84
84
 
85
85
  props.load(new FileInputStream(
86
86
 
87
- "/home/user/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties"));
87
+ "/home/yosuke/git/BudgetTrackerCui/BudgetTrackerCui/sql/config_budgettracker.properties"));
88
88
 
89
89
 
90
90
 
@@ -110,13 +110,17 @@
110
110
 
111
111
 
112
112
 
113
+ // select all
114
+
113
115
  public List<BudgetTrackerDto> selectAll() throws FileNotFoundException, IOException {
114
116
 
115
117
  List<BudgetTrackerDto> budgetList = new ArrayList<>();
116
118
 
117
119
 
118
120
 
119
- try (Connection conn = BudgetTrackerDao.getConnection(); PreparedStatement ps = conn.prepareStatement(SQL)) {
121
+ try (Connection conn = BudgetTrackerDao.getConnection();
122
+
123
+ PreparedStatement ps = conn.prepareStatement(SELECTALL)) {
120
124
 
121
125
 
122
126
 
@@ -164,7 +168,7 @@
164
168
 
165
169
 
166
170
 
167
- // Insert
171
+
168
172
 
169
173
  public int insertIntoTable(BudgetTrackerDto budgetTrackerDto) throws SQLException {
170
174
 
@@ -172,7 +176,7 @@
172
176
 
173
177
  btd = new BudgetTrackerDto();
174
178
 
175
- Statement pstmt;
179
+ PreparedStatement pstmt;
176
180
 
177
181
  try {
178
182
 
@@ -180,7 +184,7 @@
180
184
 
181
185
  myConn = BudgetTrackerDao.getConnection();
182
186
 
183
- pstmt = myConn.createStatement();
187
+ pstmt = (PreparedStatement) myConn.createStatement();
184
188
 
185
189
 
186
190
 
@@ -192,21 +196,25 @@
192
196
 
193
197
 
194
198
 
195
- String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)";
199
+ String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)";
200
+
201
+
196
202
 
197
203
  pstmt = myConn.prepareStatement(sql);
198
204
 
199
- ((PreparedStatement) pstmt).setInt(1, btd.getId());
205
+ (pstmt).setInt(1, btd.getId());
200
-
206
+
201
- ((PreparedStatement) pstmt).setTime(2, (Time) btd.getDate());
207
+ (pstmt).setTime(2, (Time) btd.getDate());
202
-
208
+
203
- ((PreparedStatement) pstmt).setString(3, btd.getStoreName());
209
+ (pstmt).setString(3, btd.getStoreName());
204
-
210
+
205
- ((PreparedStatement) pstmt).setString(4, btd.getProductName());
211
+ (pstmt).setString(4, btd.getProductName());
206
-
212
+
207
- ((PreparedStatement) pstmt).setString(5, btd.getProductType());
213
+ (pstmt).setString(5, btd.getProductType());
208
-
214
+
209
- ((PreparedStatement) pstmt).setInt(6, btd.getPrice());
215
+ (pstmt).setInt(6, btd.getPrice());
216
+
217
+ // SQL文発行
210
218
 
211
219
  pstmt.executeUpdate(sql);
212
220
 
@@ -214,6 +222,8 @@
214
222
 
215
223
 
216
224
 
225
+ //rowsCount = pstmt.executeUpdate(sql);
226
+
217
227
  } catch (SQLException e) {
218
228
 
219
229
  System.out.println("Errorが発生しました!\n" + e + "\n");

2

SQL文内の全角スペースを削除しました。

2021/12/15 02:20

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -192,9 +192,7 @@
192
192
 
193
193
 
194
194
 
195
- String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) "
195
+ String sql = "INSERT INTO budget_table(id,Date,StoreName, ProductName, ProductType, Price) " + "VALUE (?,?,?,?,?,?)";
196
-
197
- + "VALUE (?,?,?,?,?,?)";
198
196
 
199
197
  pstmt = myConn.prepareStatement(sql);
200
198
 

1

不要だった2回目のexecuteUpdateを削除

2021/12/15 01:25

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -216,10 +216,6 @@
216
216
 
217
217
 
218
218
 
219
- // SQL文発行
220
-
221
- rowsCount = pstmt.executeUpdate(sql);
222
-
223
219
  } catch (SQLException e) {
224
220
 
225
221
  System.out.println("Errorが発生しました!\n" + e + "\n");