質問編集履歴

5

誤字

2018/09/26 09:21

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -558,8 +558,6 @@
558
558
 
559
559
  ### 試したこと
560
560
 
561
- ・コンパイルエラーとなるので、エラーにならないように記述したが正常に値を取得できない。
562
-
563
561
  ・DAOの時点でslListに値が格納されていない可能性を考慮してコンソール出力したが問題なかった。
564
562
 
565
563
  ### 補足情報

4

誤字

2018/09/26 09:20

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
 
60
60
 
61
- //INSERT文中の?に使用する値を設定しSQL完成
61
+ //SELECT文中の?に使用する値を設定しSQL完成
62
62
 
63
63
  pstmt.setInt(1, t_productBean.getproduct_id());
64
64
 

3

誤字

2018/09/26 08:56

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  <詳細>
8
8
 
9
- ソースコード内のslListをservletで取得したいが、コンパイルエラーになる
9
+ ソースコード内のslListをservletで取得したい。
10
10
 
11
11
  servlet内のコードの書き方についてご教示いただけないでしょうか。
12
12
 
@@ -20,8 +20,6 @@
20
20
 
21
21
  ・正常に値を取得できない。
22
22
 
23
- ・またはコンパイルエラーとなり、実行することができない。
24
-
25
23
  ```
26
24
 
27
25
 
@@ -560,7 +558,7 @@
560
558
 
561
559
  ### 試したこと
562
560
 
563
- ・コンパイルエラーとなるので、エラーにならないように記述した。
561
+ ・コンパイルエラーとなるので、エラーにならないように記述したが正常に値を取得できない
564
562
 
565
563
  ・DAOの時点でslListに値が格納されていない可能性を考慮してコンソール出力したが問題なかった。
566
564
 

2

文保の修正

2018/09/26 08:22

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -30,194 +30,540 @@
30
30
 
31
31
 
32
32
 
33
+ ```DAO
34
+
35
+ public List<t_productBean> select(t_productBean t_productBean) {
36
+
37
+
38
+
39
+ Connection conn = null;
40
+
41
+ List<t_productBean>slList = new ArrayList<t_productBean>();
42
+
43
+ try {
44
+
45
+ //データベースへ接続
46
+
47
+ conn = DriverManager.getConnection(url,user,password);
48
+
49
+
50
+
51
+ //select文の準備
52
+
53
+ String sql = "SELECT * FROM t_product WHERE product_id = ?";
54
+
55
+
56
+
57
+
58
+
59
+ PreparedStatement pstmt = conn.prepareStatement(sql);
60
+
61
+
62
+
63
+ //INSERT文中の?に使用する値を設定しSQL完成
64
+
65
+ pstmt.setInt(1, t_productBean.getproduct_id());
66
+
67
+
68
+
69
+ //SElECT文を実行し、結果を取得
70
+
71
+ ResultSet rs = pstmt.executeQuery();
72
+
73
+
74
+
75
+ while(rs.next()) { //select文の結果をArrayListに格納
76
+
77
+ int product_id = rs.getInt("product_id");
78
+
79
+ String product_genre_code = rs.getString("product_genre_code");
80
+
81
+ String product_name = rs.getString("product_name");
82
+
83
+ String product_maker = rs.getString("product_maker");
84
+
85
+ int product_price = rs.getInt("product_price");
86
+
87
+ int product_stock = rs.getInt("product_stock");
88
+
89
+ int product_sales = rs.getInt("product_sales");
90
+
91
+ String product_remarks = rs.getString("product_remarks");
92
+
93
+
94
+
95
+ if(product_genre_code.equals("1")) {
96
+
97
+ product_genre_code ="指定なし";
98
+
99
+ }else if(product_genre_code.equals("2")) {
100
+
101
+ product_genre_code = "時計";
102
+
103
+ }else if(product_genre_code.equals("3")) {
104
+
105
+ product_genre_code = "電子機器";
106
+
107
+ }else if(product_genre_code.equals("4")) {
108
+
109
+ product_genre_code = "携帯";
110
+
111
+ }else{product_genre_code ="error";}
112
+
113
+
114
+
115
+ t_productBean pb = new t_productBean(product_id,product_genre_code,product_name
116
+
117
+ ,product_maker,product_price,product_stock,product_sales,product_remarks);
118
+
119
+ pb.setproduct_id(product_id);
120
+
121
+ pb.setproduct_genre_code(product_genre_code);
122
+
123
+ pb.setproduct_name(product_name);
124
+
125
+ pb.setproduct_maker(product_maker);
126
+
127
+ pb.setproduct_price(product_price);
128
+
129
+ pb.setproduct_stock(product_stock);
130
+
131
+ pb.setproduct_sales(product_sales);
132
+
133
+ pb.setproduct_remarks(product_remarks);
134
+
135
+
136
+
137
+ System.out.println("sl"+product_id);
138
+
139
+ System.out.println("sl"+product_genre_code);
140
+
141
+ System.out.println("sl"+product_name);
142
+
143
+ System.out.println("sl"+product_maker);
144
+
145
+ System.out.println("sl"+product_price);
146
+
147
+ System.out.println("sl"+product_stock);
148
+
149
+ System.out.println("sl"+product_sales);
150
+
151
+ System.out.println("sl"+product_remarks);
152
+
153
+
154
+
155
+ t_productBean t_product = new t_productBean(product_id,product_genre_code,product_name
156
+
157
+ ,product_maker,product_price,product_stock,product_sales,product_remarks);
158
+
159
+
160
+
161
+ System.out.println(
162
+
163
+ t_product.getproduct_id());
164
+
165
+
166
+
167
+ slList.add(t_product);
168
+
169
+ }
170
+
171
+
172
+
173
+ }catch(SQLException e) { //例外SQL例外あり
174
+
175
+ e.printStackTrace();
176
+
177
+ return null;
178
+
179
+ }finally { //catchがなくても必ず実行
180
+
181
+ //データベース切断
182
+
183
+ if(conn != null) {
184
+
185
+ try {
186
+
187
+ conn.close();
188
+
189
+ }catch(SQLException e) {
190
+
191
+ e.printStackTrace();
192
+
193
+ return null;
194
+
195
+ }
196
+
197
+ }
198
+
199
+ }
200
+
201
+ return slList;
202
+
203
+ }
204
+
205
+ }
206
+
207
+ ```
208
+
209
+
210
+
33
211
  ```servlet
34
212
 
35
- public List<t_productBean> select(t_productBean t_productBean) {
36
-
37
-
38
-
39
- Connection conn = null;
40
-
41
- List<t_productBean>slList = new ArrayList<t_productBean>();
42
-
43
- try {
44
-
45
- //データベースへ接続
46
-
47
- conn = DriverManager.getConnection(url,user,password);
48
-
49
-
50
-
51
- //select文の準備
52
-
53
- String sql = "SELECT * FROM t_product WHERE product_id = ?";
54
-
55
-
56
-
57
-
58
-
59
- PreparedStatement pstmt = conn.prepareStatement(sql);
60
-
61
-
62
-
63
- //INSERT文中の?に使用する値を設定しSQL完成
64
-
65
- pstmt.setInt(1, t_productBean.getproduct_id());
66
-
67
-
68
-
69
- //SElECT文を実行し、結果を取得
70
-
71
- ResultSet rs = pstmt.executeQuery();
72
-
73
-
74
-
75
- while(rs.next()) { //select文の結果をArrayListに格納
76
-
77
- int product_id = rs.getInt("product_id");
78
-
79
- String product_genre_code = rs.getString("product_genre_code");
80
-
81
- String product_name = rs.getString("product_name");
82
-
83
- String product_maker = rs.getString("product_maker");
84
-
85
- int product_price = rs.getInt("product_price");
86
-
87
- int product_stock = rs.getInt("product_stock");
88
-
89
- int product_sales = rs.getInt("product_sales");
90
-
91
- String product_remarks = rs.getString("product_remarks");
92
-
93
-
94
-
95
- if(product_genre_code.equals("1")) {
96
-
97
- product_genre_code ="指定なし";
98
-
99
- }else if(product_genre_code.equals("2")) {
100
-
101
- product_genre_code = "時計";
102
-
103
- }else if(product_genre_code.equals("3")) {
104
-
105
- product_genre_code = "電子機器";
106
-
107
- }else if(product_genre_code.equals("4")) {
108
-
109
- product_genre_code = "携帯";
110
-
111
- }else{product_genre_code ="error";}
112
-
113
-
114
-
115
- t_productBean pb = new t_productBean(product_id,product_genre_code,product_name
116
-
117
- ,product_maker,product_price,product_stock,product_sales,product_remarks);
118
-
119
- pb.setproduct_id(product_id);
120
-
121
- pb.setproduct_genre_code(product_genre_code);
122
-
123
- pb.setproduct_name(product_name);
124
-
125
- pb.setproduct_maker(product_maker);
126
-
127
- pb.setproduct_price(product_price);
128
-
129
- pb.setproduct_stock(product_stock);
130
-
131
- pb.setproduct_sales(product_sales);
132
-
133
- pb.setproduct_remarks(product_remarks);
134
-
135
-
136
-
137
- System.out.println("sl"+product_id);
138
-
139
- System.out.println("sl"+product_genre_code);
140
-
141
- System.out.println("sl"+product_name);
142
-
143
- System.out.println("sl"+product_maker);
144
-
145
- System.out.println("sl"+product_price);
146
-
147
- System.out.println("sl"+product_stock);
148
-
149
- System.out.println("sl"+product_sales);
150
-
151
- System.out.println("sl"+product_remarks);
152
-
153
-
154
-
155
- t_productBean t_product = new t_productBean(product_id,product_genre_code,product_name
156
-
157
- ,product_maker,product_price,product_stock,product_sales,product_remarks);
158
-
159
-
160
-
161
- System.out.println("korekore"+t_product.getproduct_id());
162
-
163
-
164
-
165
- slList.add(t_product);
166
-
167
- }
168
-
169
-
170
-
171
- }catch(SQLException e) { //例外SQL例外あり
172
-
173
- e.printStackTrace();
174
-
175
- return null;
176
-
177
- }finally { //catchがなくても必ず実行
178
-
179
- //データベース切断
180
-
181
- if(conn != null) {
182
-
183
- try {
184
-
185
- conn.close();
186
-
187
- }catch(SQLException e) {
188
-
189
- e.printStackTrace();
190
-
191
- return null;
192
-
193
- }
194
-
195
- }
213
+ package servlet;
214
+
215
+
216
+
217
+ import java.io.IOException;
218
+
219
+ import java.util.ArrayList;
220
+
221
+ import java.util.List;
222
+
223
+
224
+
225
+ import javax.servlet.RequestDispatcher;
226
+
227
+ import javax.servlet.ServletException;
228
+
229
+ import javax.servlet.annotation.WebServlet;
230
+
231
+ import javax.servlet.http.HttpServlet;
232
+
233
+ import javax.servlet.http.HttpServletRequest;
234
+
235
+ import javax.servlet.http.HttpServletResponse;
236
+
237
+
238
+
239
+ import model.Selectlogic;
240
+
241
+ import model.Updatelogic;
242
+
243
+ import model.t_productBean;
244
+
245
+
246
+
247
+ @WebServlet("/ProductUpdate")
248
+
249
+ public class ProductUpdate extends HttpServlet {
250
+
251
+ private static final long serialVersionUID = 1L;
252
+
253
+
254
+
255
+ public ProductUpdate() {
256
+
257
+ super();
258
+
259
+ }
260
+
261
+
262
+
263
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
264
+
265
+ response.setCharacterEncoding("UTF-8");
266
+
267
+
268
+
269
+ String product_id_parameter = request.getParameter("product_id");
270
+
271
+ String product_name = "";
272
+
273
+ String product_genre_code = "";
274
+
275
+ String product_maker = "";
276
+
277
+ int product_price = 0;
278
+
279
+ int product_stock= 0;
280
+
281
+ int product_sales= 0;
282
+
283
+ String product_remarks = "";
284
+
285
+
286
+
287
+ Integer product_id = Integer.parseInt(product_id_parameter);
288
+
289
+
290
+
291
+
292
+
293
+ t_productBean T_product = new t_productBean(product_id,product_genre_code,product_name
294
+
295
+ ,product_maker,product_price,product_stock,product_sales,product_remarks);
296
+
297
+
298
+
299
+ Selectlogic.execute(T_product);
300
+
301
+
302
+
303
+ List<t_productBean>productList = new ArrayList<t_productBean>();
304
+
305
+ productList.add(new t_productBean(product_id,product_genre_code,product_name,product_maker,product_price,product_stock,product_sales,product_remarks));
306
+
307
+
308
+
309
+ request.setAttribute("productList", productList);
310
+
311
+ RequestDispatcher rd = request.getRequestDispatcher("/ProductUpdate.jsp" );
312
+
313
+ rd.forward(request,response);
314
+
315
+ }
316
+
317
+
318
+
319
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
320
+
321
+ request.setCharacterEncoding("UTF-8");
322
+
323
+
324
+
325
+ //formの値を取得
326
+
327
+ String product_id_parameter = request.getParameter("product_id");
328
+
329
+ String product_genre_code = request.getParameter("product_genre_code");
330
+
331
+ String product_name = request.getParameter("product_name");
332
+
333
+ String product_maker = request.getParameter("product_maker");
334
+
335
+ String product_price_parameter = request.getParameter("product_price");
336
+
337
+ String product_stock_parameter = request.getParameter("product_stock");
338
+
339
+ String product_sales_parameter = request.getParameter("product_sales");
340
+
341
+ String product_remarks = request.getParameter("product_remarks");
342
+
343
+
344
+
345
+ switch (product_genre_code) {
346
+
347
+ case "指定なし":
348
+
349
+ product_genre_code ="1";
350
+
351
+ break;
352
+
353
+
354
+
355
+ case "時計":
356
+
357
+ product_genre_code ="2";
358
+
359
+ break;
360
+
361
+
362
+
363
+ case "電子機器":
364
+
365
+ product_genre_code ="3";
366
+
367
+ break;
368
+
369
+
370
+
371
+ case "携帯":
372
+
373
+ product_genre_code ="4";
374
+
375
+ break;
196
376
 
197
377
  }
198
378
 
379
+
380
+
199
- return slList;
381
+ //String型をintに変換
382
+
383
+ Integer product_id = Integer.parseInt(product_id_parameter);
384
+
385
+ Integer product_price = Integer.parseInt(product_price_parameter);
386
+
387
+ Integer product_stock = Integer.parseInt(product_stock_parameter);
388
+
389
+ Integer product_sales = Integer.parseInt(product_sales_parameter);
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+ //インスタンス化
398
+
399
+ t_productBean T_product = new t_productBean(product_id,product_genre_code,product_name
400
+
401
+ ,product_maker,product_price,product_stock,product_sales,product_remarks);
402
+
403
+
404
+
405
+ Updatelogic.execute(T_product);
406
+
407
+
408
+
409
+ RequestDispatcher dispatcher = request.getRequestDispatcher("/ProductList" );
410
+
411
+ dispatcher.forward(request,response);
412
+
413
+
414
+
415
+ }
200
416
 
201
417
  }
202
418
 
203
- }
204
-
205
419
  ```
206
420
 
207
421
 
208
422
 
423
+ ```JSP
424
+
425
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
426
+
427
+ pageEncoding="UTF-8"%>
428
+
429
+ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
430
+
431
+ <!DOCTYPE html>
432
+
433
+ <html>
434
+
435
+ <head>
436
+
437
+ <meta charset = "utf-8">
438
+
439
+ <title>更新画面</title>
440
+
441
+ <link rel="stylesheet" type="text/css" href="css/ProductInfo.css">
442
+
443
+ </head>
444
+
445
+ <body>
446
+
447
+ <form action="" method="post" name="product">
448
+
449
+ <table><c:forEach var="productList" items="${productList}">
450
+
451
+ <!--商品IDジャンルコード-->
452
+
453
+ <tr>
454
+
455
+ <th align = "left">商品ID</th><th align = "left">ジャンル</th>
456
+
457
+ </tr>
458
+
459
+ <tr>
460
+
461
+ <td><input name ="product_id" type="text" value="" placeholder="${productList.product_id }"></td><td>
462
+
463
+ <select name = "product_genre_code" >
464
+
465
+ <option selected>${productList.product_genre_code }</option>
466
+
467
+ <option>指定なし</option><option>時計</option><option>電子機器</option><option>携帯</option>
468
+
469
+ </select>
470
+
471
+ </td>
472
+
473
+ </tr>
474
+
475
+ <!--商品名メーカー品-->
476
+
477
+ <tr>
478
+
479
+ <th align = "left">商品名</th><th align = "left">メーカー品</th>
480
+
481
+ </tr>
482
+
483
+ <tr>
484
+
485
+ <td><input name = "product_name" type="text" placeholder=${productList.product_name }></td>
486
+
487
+ <td><select name = "product_maker">
488
+
489
+ <option selected>${productList.product_maker }</option>
490
+
491
+ <option>指定なし</option><option>パナソニック</option><option>ソニー</option><option>シャープ</option>
492
+
493
+ </select>
494
+
495
+ </td>
496
+
497
+ </tr>
498
+
499
+ <!--金額と在庫-->
500
+
501
+ <tr>
502
+
503
+ <th align = "left">金額</th><th align = "left">在庫数</th>
504
+
505
+ </tr>
506
+
507
+ <tr>
508
+
509
+ <td><input name = "product_price" type="text" placeholder=${productList.product_price }></td>
510
+
511
+ <td><input name = "product_stock" type="text" placeholder=${productList.product_stock }></td>
512
+
513
+ </tr>
514
+
515
+ <!--販売個数-->
516
+
517
+ <tr>
518
+
519
+ <th align="left">販売個数</th>
520
+
521
+ </tr>
522
+
523
+ <tr>
524
+
525
+ <td><input name="product_sales" type="text" placeholder=${productList.product_sales }></td>
526
+
527
+ </tr>
528
+
529
+ <!--備考テキストエリア-->
530
+
531
+ <tr><!--備考-->
532
+
533
+ <th align="left">備考</th>
534
+
535
+ </tr>
536
+
537
+ </c:forEach>
538
+
539
+ </table>
540
+
541
+ <table>
542
+
543
+ <tr><!--テキストエリア-->
544
+
545
+ <td><textarea name="product_remarks"></textarea></td>
546
+
547
+ </tr>
548
+
549
+ </table>
550
+
551
+ <input class="sctn" type="image" alt="登録" width="50" height="30" src="img/sc.png" />
552
+
553
+ </form>
554
+
555
+ </body>
556
+
557
+ </html>
558
+
559
+ ```
560
+
209
561
  ### 試したこと
210
562
 
211
563
  ・コンパイルエラーとなるので、エラーにならないように記述した。
212
564
 
213
565
  ・DAOの時点でslListに値が格納されていない可能性を考慮してコンソール出力したが問題なかった。
214
566
 
215
-
216
-
217
-
218
-
219
567
  ### 補足情報
220
568
 
221
-
222
-
223
569
  このソースコードはクラスの一部であり、ほかにもメソッドがあります。

1

文法の修正

2018/09/26 08:07

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
 
32
32
 
33
- ```JSP
33
+ ```servlet
34
34
 
35
35
  public List<t_productBean> select(t_productBean t_productBean) {
36
36