質問編集履歴
4
書式の改善
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
JSP 簡易注文システム 合計金額
|
1
|
+
JSP 簡易注文システム 合計金額の算出
|
test
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
JSPとJavaBeansを利用して簡易注文システムを作成しています。
|
4
4
|
|
5
|
+
大枠の部分は出来上がったのでここに合計金額を算出するコードを書き加えたいのですが、
|
6
|
+
|
5
|
-
|
7
|
+
どのように書けばいいのか分かりません。どなたかご教示いただけますと幸いです。
|
6
8
|
|
7
9
|
|
8
10
|
|
3
書式の改善、自分で考えて分かった部分のコードを修正しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
JSP
|
1
|
+
JSP 簡易注文システム 合計金額を出したい
|
test
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
JSPとJavaBeansを利用して簡易注文システムを作成しています。
|
4
4
|
|
5
|
+
合計金額の算出の仕方が分かりません。どなたかご教示いただけますと幸いです。
|
6
|
+
|
7
|
+
|
8
|
+
|
5
9
|
条件、仕組みは以下のようなものです。
|
6
10
|
|
7
11
|
|
@@ -22,9 +26,7 @@
|
|
22
26
|
|
23
27
|
③「1ページ目から受取ったid」を実引数としてメソッドupdateDB()に渡す。
|
24
28
|
|
25
|
-
④メソッドupdateDB()の丸括弧内に仮引数を宣言し、idを受け取る。
|
29
|
+
④メソッドupdateDB()の丸括弧内に仮引数(tempid)を宣言し、idを受け取る。
|
26
|
-
|
27
|
-
↑自分はtempidと宣言しました。
|
28
30
|
|
29
31
|
⑤メソッドupdateDB()で、SQLのupdate文を実行し、注文数を「+1」する。
|
30
32
|
|
@@ -40,22 +42,6 @@
|
|
40
42
|
|
41
43
|
|
42
44
|
|
43
|
-
〈分からないこと〉
|
44
|
-
|
45
|
-
③「1ページ目から受取ったid」を実引数としてメソッドupdateDB()に渡す と
|
46
|
-
|
47
|
-
⑦最新の注文の支払い合計金額を表示する のやり方が分かりませんでした。
|
48
|
-
|
49
|
-
どなたかご教示いただけますとありがたいです。
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
45
|
### 該当のソースコード
|
60
46
|
|
61
47
|
|
@@ -84,7 +70,7 @@
|
|
84
70
|
|
85
71
|
<%
|
86
72
|
|
87
|
-
List<BeanAccessDB> list = obj.DBtoList();
|
73
|
+
List<BeanAccessDB> list = obj.DBtoList();
|
88
74
|
|
89
75
|
int sum = 0;
|
90
76
|
|
@@ -92,7 +78,9 @@
|
|
92
78
|
|
93
79
|
obj = list.get(i);
|
94
80
|
|
95
|
-
|
81
|
+
%>
|
82
|
+
|
83
|
+
|
96
84
|
|
97
85
|
<a href="driveBean2.jsp?linkdata=<%= obj.getId() %>"> <%= obj.getId() %> </a>
|
98
86
|
|
@@ -102,59 +90,313 @@
|
|
102
90
|
|
103
91
|
<img src="images/寿司の画像/<%= obj.getPhoto() %>" width="70" height="70" />
|
104
92
|
|
105
|
-
|
93
|
+
<%
|
94
|
+
|
95
|
+
out.print("合計:" + sum + "円");
|
96
|
+
|
97
|
+
%>
|
106
98
|
|
107
99
|
|
108
100
|
|
109
101
|
<%}%>
|
110
102
|
|
103
|
+
|
104
|
+
|
105
|
+
</body>
|
106
|
+
|
107
|
+
</html>
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
``````
|
116
|
+
|
117
|
+
```ここに言語を入力
|
118
|
+
|
119
|
+
【driveBean2.jsp】
|
120
|
+
|
121
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
122
|
+
|
123
|
+
pageEncoding="UTF-8"%>
|
124
|
+
|
125
|
+
<% request.setCharacterEncoding("UTF-8"); %>
|
126
|
+
|
127
|
+
<jsp:useBean id="obj" class="bean.BeanAccessDB" />
|
128
|
+
|
129
|
+
<!DOCTYPE html>
|
130
|
+
|
111
|
-
<
|
131
|
+
<html>
|
132
|
+
|
112
|
-
|
133
|
+
<head>
|
134
|
+
|
113
|
-
|
135
|
+
<meta charset="ISO-8859-1">
|
136
|
+
|
137
|
+
<title>Insert title here</title>
|
138
|
+
|
139
|
+
</head>
|
140
|
+
|
141
|
+
<body>
|
142
|
+
|
143
|
+
<% String linkdata = request.getParameter("linkdata");
|
144
|
+
|
145
|
+
obj.updateDB(linkdata);
|
114
146
|
|
115
147
|
%>
|
116
148
|
|
149
|
+
|
150
|
+
|
117
151
|
</body>
|
118
152
|
|
119
|
-
</html>
|
120
|
-
|
121
153
|
|
122
154
|
|
123
155
|
```
|
124
156
|
|
125
157
|
```ここに言語を入力
|
126
158
|
|
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
|
-
|
159
|
+
【BeanAccessDB.java】
|
160
|
+
|
161
|
+
package bean;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
import java.lang.reflect.InvocationTargetException;
|
166
|
+
|
167
|
+
import java.sql.Connection;
|
168
|
+
|
169
|
+
import java.sql.DriverManager;
|
170
|
+
|
171
|
+
import java.sql.PreparedStatement;
|
172
|
+
|
173
|
+
import java.sql.ResultSet;
|
174
|
+
|
175
|
+
import java.sql.SQLException;
|
176
|
+
|
177
|
+
import java.util.ArrayList;
|
178
|
+
|
179
|
+
import java.util.List;
|
180
|
+
|
181
|
+
import org.mariadb.jdbc.Driver;
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
public class BeanAccessDB {
|
186
|
+
|
187
|
+
private String id;
|
188
|
+
|
189
|
+
private String name;
|
190
|
+
|
191
|
+
private int price;
|
192
|
+
|
193
|
+
private String photo;
|
194
|
+
|
195
|
+
private int number;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public BeanAccessDB() {
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
public BeanAccessDB(String id, String name, int price, String photo, int number) {
|
206
|
+
|
207
|
+
this.id = id;
|
208
|
+
|
209
|
+
this.name = name;
|
210
|
+
|
211
|
+
this.price = price;
|
212
|
+
|
213
|
+
this.photo = photo;
|
214
|
+
|
215
|
+
this.number = number;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
public String getId() {
|
222
|
+
|
223
|
+
return id;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
public String getName() {
|
230
|
+
|
231
|
+
return name;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public int getPrice() {
|
238
|
+
|
239
|
+
return price;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
public String getPhoto() {
|
246
|
+
|
247
|
+
return photo;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
public int getNumber() {
|
254
|
+
|
255
|
+
return number;
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
public List<BeanAccessDB> DBtoList() {
|
262
|
+
|
263
|
+
List<BeanAccessDB> list = new ArrayList<BeanAccessDB>();
|
264
|
+
|
265
|
+
Connection con = null;
|
266
|
+
|
267
|
+
PreparedStatement ps = null;
|
268
|
+
|
269
|
+
try {
|
270
|
+
|
271
|
+
Driver.class.getDeclaredConstructor().newInstance();
|
272
|
+
|
273
|
+
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
String sql = "select * from sushi; ";
|
278
|
+
|
279
|
+
ps = con.prepareStatement(sql.toString());
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
ResultSet rs = ps.executeQuery();
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
while (rs.next()) {
|
288
|
+
|
289
|
+
String id = rs.getString("id");
|
290
|
+
|
291
|
+
System.out.println(id);
|
292
|
+
|
293
|
+
String name = rs.getString("name");
|
294
|
+
|
295
|
+
int price = rs.getInt("price");
|
296
|
+
|
297
|
+
String photo = rs.getString("photo");
|
298
|
+
|
299
|
+
int number = rs.getInt("number");
|
300
|
+
|
301
|
+
list.add(new BeanAccessDB(id, name, price, photo, number));
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
} catch (Exception e) {
|
308
|
+
|
309
|
+
e.printStackTrace();
|
310
|
+
|
311
|
+
} finally {
|
312
|
+
|
313
|
+
try {
|
314
|
+
|
315
|
+
if (ps != null) {
|
316
|
+
|
317
|
+
ps.close();
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
if (con != null) {
|
322
|
+
|
323
|
+
con.close();
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
} catch (Exception e) {
|
328
|
+
|
329
|
+
e.printStackTrace();
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
return list;
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
public void updateDB(String tempid) {
|
342
|
+
|
343
|
+
Connection con = null;
|
344
|
+
|
345
|
+
PreparedStatement ps = null;
|
346
|
+
|
347
|
+
try {
|
348
|
+
|
349
|
+
Driver.class.getDeclaredConstructor().newInstance();
|
350
|
+
|
351
|
+
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
String sql = "update sushi set number = number + 1 where id = ? ";
|
356
|
+
|
357
|
+
ps = con.prepareStatement(sql.toString());
|
358
|
+
|
359
|
+
ps.setString(1, tempid);
|
360
|
+
|
361
|
+
ps.executeUpdate();
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
} catch (Exception e) {
|
366
|
+
|
367
|
+
e.printStackTrace();
|
368
|
+
|
369
|
+
} finally {
|
370
|
+
|
371
|
+
try {
|
372
|
+
|
373
|
+
if (ps != null) {
|
374
|
+
|
375
|
+
ps.close();
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
if (con != null) {
|
380
|
+
|
381
|
+
con.close();
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
} catch (Exception e) {
|
386
|
+
|
387
|
+
e.printStackTrace();
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
|
158
400
|
|
159
401
|
|
160
402
|
|
@@ -162,292 +404,40 @@
|
|
162
404
|
|
163
405
|
```ここに言語を入力
|
164
406
|
|
165
|
-
【B
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
imp
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
i
|
176
|
-
|
177
|
-
i
|
178
|
-
|
179
|
-
i
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
im
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
i
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
private int price;
|
198
|
-
|
199
|
-
private String photo;
|
200
|
-
|
201
|
-
private int number;
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
public BeanAccessDB() {
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
public BeanAccessDB(String id, String name, int price, String photo, int number) {
|
212
|
-
|
213
|
-
this.id = id;
|
214
|
-
|
215
|
-
this.name = name;
|
216
|
-
|
217
|
-
this.price = price;
|
218
|
-
|
219
|
-
this.photo = photo;
|
220
|
-
|
221
|
-
this.number = number;
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
public String getId() {
|
228
|
-
|
229
|
-
return id;
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
public String getName() {
|
234
|
-
|
235
|
-
return name;
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
public int getPrice() {
|
242
|
-
|
243
|
-
return price;
|
244
|
-
|
245
|
-
}
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
public String getPhoto() {
|
250
|
-
|
251
|
-
return photo;
|
252
|
-
|
253
|
-
}
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
public int getNumber() {
|
258
|
-
|
259
|
-
return number;
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
public List<BeanAccessDB> DBtoList() {
|
266
|
-
|
267
|
-
List<BeanAccessDB> list = new ArrayList<BeanAccessDB>();
|
268
|
-
|
269
|
-
Connection con = null;
|
270
|
-
|
271
|
-
PreparedStatement ps = null;
|
272
|
-
|
273
|
-
try {
|
274
|
-
|
275
|
-
Driver.class.getDeclaredConstructor().newInstance();
|
276
|
-
|
277
|
-
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
String sql = "select * from sushi; ";
|
284
|
-
|
285
|
-
ps = con.prepareStatement(sql.toString());
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
ResultSet rs = ps.executeQuery();
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
while (rs.next()) {
|
294
|
-
|
295
|
-
String id = rs.getString("id");
|
296
|
-
|
297
|
-
System.out.println(id);
|
298
|
-
|
299
|
-
String name = rs.getString("name");
|
300
|
-
|
301
|
-
int price = rs.getInt("price");
|
302
|
-
|
303
|
-
String photo = rs.getString("photo");
|
304
|
-
|
305
|
-
int number = rs.getInt("number");
|
306
|
-
|
307
|
-
list.add(new BeanAccessDB(id, name, price, photo, number));
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
} catch (Exception e) {
|
314
|
-
|
315
|
-
e.printStackTrace();
|
316
|
-
|
317
|
-
} finally {
|
318
|
-
|
319
|
-
try {
|
320
|
-
|
321
|
-
if (ps != null) {
|
322
|
-
|
323
|
-
ps.close();
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
if (con != null) {
|
328
|
-
|
329
|
-
con.close();
|
330
|
-
|
331
|
-
}
|
332
|
-
|
333
|
-
} catch (Exception e) {
|
334
|
-
|
335
|
-
e.printStackTrace();
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
return list;
|
342
|
-
|
343
|
-
}
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
public List<BeanAccessDB> updateDB(String tempid){
|
348
|
-
|
349
|
-
Connection con = null;
|
350
|
-
|
351
|
-
PreparedStatement ps = null;
|
352
|
-
|
353
|
-
try {
|
354
|
-
|
355
|
-
Driver.class.getDeclaredConstructor().newInstance();
|
356
|
-
|
357
|
-
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
String sql = "update sushi set number = number + 1 where id = ? ";
|
362
|
-
|
363
|
-
ps = con.prepareStatement(sql.toString());
|
364
|
-
|
365
|
-
ps.setString(1, tempid);
|
366
|
-
|
367
|
-
ps.executeUpdate();
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
} catch (Exception e) {
|
372
|
-
|
373
|
-
e.printStackTrace();
|
374
|
-
|
375
|
-
} finally {
|
376
|
-
|
377
|
-
try {
|
378
|
-
|
379
|
-
if (ps != null) {
|
380
|
-
|
381
|
-
ps.close();
|
382
|
-
|
383
|
-
}
|
384
|
-
|
385
|
-
if (con != null) {
|
386
|
-
|
387
|
-
con.close();
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
} catch (Exception e) {
|
392
|
-
|
393
|
-
e.printStackTrace();
|
394
|
-
|
395
|
-
}
|
396
|
-
|
397
|
-
}
|
398
|
-
|
399
|
-
return null;
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
407
|
+
【SQL TABLE sushi】
|
408
|
+
|
409
|
+
select * from sushi;
|
410
|
+
|
411
|
+
+-----+------------+-------+--------------+--------+
|
412
|
+
|
413
|
+
| id | name | price | photo | number |
|
414
|
+
|
415
|
+
+-----+------------+-------+--------------+--------+
|
416
|
+
|
417
|
+
| 001 | えび | 240 | ebi.png | 0 |
|
418
|
+
|
419
|
+
| 002 | いか | 210 | ika.png | 0 |
|
420
|
+
|
421
|
+
| 003 | いくら | 300 | ikura.png | 0 |
|
422
|
+
|
423
|
+
| 004 | まぐろ | 150 | maguro.png | 0 |
|
424
|
+
|
425
|
+
| 005 | しめさば | 200 | simesaba.png | 0 |
|
426
|
+
|
427
|
+
| 006 | たまご | 140 | tamago.png | 0 |
|
428
|
+
|
429
|
+
| 007 | うに | 400 | uni.png | 0 |
|
430
|
+
|
431
|
+
| 008 | 太巻き | 500 | futomaki.png | 0 |
|
432
|
+
|
433
|
+
| 009 | 手巻き寿司 | 550 | temaki.png | 0 |
|
434
|
+
|
435
|
+
| 010 | ちらし寿司 | 1200 | tirasi.png | 0 |
|
436
|
+
|
437
|
+
+-----+------------+-------+--------------+--------+
|
412
438
|
|
413
439
|
```
|
414
440
|
|
415
|
-
```ここに言語を入力
|
416
|
-
|
417
|
-
【SQL TABLE sushi】
|
418
|
-
|
419
|
-
select * from sushi;
|
420
|
-
|
421
|
-
+-----+------------+-------+--------------+--------+
|
422
|
-
|
423
|
-
| id | name | price | photo | number |
|
424
|
-
|
425
|
-
+-----+------------+-------+--------------+--------+
|
426
|
-
|
427
|
-
| 001 | えび | 240 | ebi.png | 0 |
|
428
|
-
|
429
|
-
| 002 | いか | 210 | ika.png | 0 |
|
430
|
-
|
431
|
-
| 003 | いくら | 300 | ikura.png | 0 |
|
432
|
-
|
433
|
-
| 004 | まぐろ | 150 | maguro.png | 0 |
|
434
|
-
|
435
|
-
| 005 | しめさば | 200 | simesaba.png | 0 |
|
436
|
-
|
437
|
-
| 006 | たまご | 140 | tamago.png | 0 |
|
438
|
-
|
439
|
-
| 007 | うに | 400 | uni.png | 0 |
|
440
|
-
|
441
|
-
| 008 | 太巻き | 500 | futomaki.png | 0 |
|
442
|
-
|
443
|
-
| 009 | 手巻き寿司 | 550 | temaki.png | 0 |
|
444
|
-
|
445
|
-
| 010 | ちらし寿司 | 1200 | tirasi.png | 0 |
|
446
|
-
|
447
|
-
+-----+------------+-------+--------------+--------+
|
448
|
-
|
449
|
-
```
|
450
|
-
|
451
441
|
|
452
442
|
|
453
443
|
|
2
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -84,79 +84,329 @@
|
|
84
84
|
|
85
85
|
<%
|
86
86
|
|
87
|
-
List<BeanAccessDB> list = obj.DBtoList();
|
87
|
+
List<BeanAccessDB> list = obj.DBtoList();
|
88
88
|
|
89
89
|
int sum = 0;
|
90
90
|
|
91
91
|
for(int i = 0; i < list.size(); i++){
|
92
92
|
|
93
|
+
obj = list.get(i);
|
94
|
+
|
95
|
+
<% sum += obj.getPrice();%>
|
96
|
+
|
97
|
+
<a href="driveBean2.jsp?linkdata=<%= obj.getId() %>"> <%= obj.getId() %> </a>
|
98
|
+
|
99
|
+
<%= obj.getName() %>、
|
100
|
+
|
101
|
+
<%= obj.getPrice() %>
|
102
|
+
|
93
|
-
|
103
|
+
<img src="images/寿司の画像/<%= obj.getPhoto() %>" width="70" height="70" />
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
<%}%>
|
110
|
+
|
111
|
+
<%
|
112
|
+
|
113
|
+
out.print("合計:" + sum + "円");
|
94
114
|
|
95
115
|
%>
|
96
116
|
|
117
|
+
</body>
|
118
|
+
|
119
|
+
</html>
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
```ここに言語を入力
|
126
|
+
|
127
|
+
【driveBean2.jsp】
|
128
|
+
|
129
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
130
|
+
|
97
|
-
|
131
|
+
pageEncoding="UTF-8"%>
|
98
|
-
|
132
|
+
|
99
|
-
<
|
133
|
+
<% request.setCharacterEncoding("UTF-8"); %>
|
100
|
-
|
134
|
+
|
101
|
-
<
|
135
|
+
<!DOCTYPE html>
|
102
|
-
|
103
|
-
|
136
|
+
|
104
|
-
|
105
|
-
<img src="images/寿司の画像/<%= obj.getPhoto() %>" width="70" height="70" />
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
<
|
137
|
+
<html>
|
112
|
-
|
138
|
+
|
113
|
-
<
|
139
|
+
<head>
|
114
|
-
|
140
|
+
|
115
|
-
|
141
|
+
<meta charset="ISO-8859-1">
|
142
|
+
|
143
|
+
<title>Insert title here</title>
|
144
|
+
|
145
|
+
</head>
|
146
|
+
|
147
|
+
<body>
|
148
|
+
|
149
|
+
<% String linkdata = request.getParameter("linkdata");
|
116
150
|
|
117
151
|
%>
|
118
152
|
|
153
|
+
|
154
|
+
|
155
|
+
<jsp:forward page="driveBean.jsp".jsp/>
|
156
|
+
|
119
157
|
</body>
|
120
158
|
|
121
|
-
</html>
|
122
|
-
|
123
159
|
|
124
160
|
|
125
161
|
```
|
126
162
|
|
127
163
|
```ここに言語を入力
|
128
164
|
|
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
|
-
|
165
|
+
【BeanAccessDB.java】
|
166
|
+
|
167
|
+
package bean;
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
import java.lang.reflect.InvocationTargetException;
|
172
|
+
|
173
|
+
import java.sql.Connection;
|
174
|
+
|
175
|
+
import java.sql.DriverManager;
|
176
|
+
|
177
|
+
import java.sql.PreparedStatement;
|
178
|
+
|
179
|
+
import java.sql.ResultSet;
|
180
|
+
|
181
|
+
import java.sql.SQLException;
|
182
|
+
|
183
|
+
import java.util.ArrayList;
|
184
|
+
|
185
|
+
import java.util.List;
|
186
|
+
|
187
|
+
import org.mariadb.jdbc.Driver;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
public class BeanAccessDB {
|
192
|
+
|
193
|
+
private String id;
|
194
|
+
|
195
|
+
private String name;
|
196
|
+
|
197
|
+
private int price;
|
198
|
+
|
199
|
+
private String photo;
|
200
|
+
|
201
|
+
private int number;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
public BeanAccessDB() {
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
public BeanAccessDB(String id, String name, int price, String photo, int number) {
|
212
|
+
|
213
|
+
this.id = id;
|
214
|
+
|
215
|
+
this.name = name;
|
216
|
+
|
217
|
+
this.price = price;
|
218
|
+
|
219
|
+
this.photo = photo;
|
220
|
+
|
221
|
+
this.number = number;
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
public String getId() {
|
228
|
+
|
229
|
+
return id;
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
public String getName() {
|
234
|
+
|
235
|
+
return name;
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
public int getPrice() {
|
242
|
+
|
243
|
+
return price;
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
public String getPhoto() {
|
250
|
+
|
251
|
+
return photo;
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
public int getNumber() {
|
258
|
+
|
259
|
+
return number;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
public List<BeanAccessDB> DBtoList() {
|
266
|
+
|
267
|
+
List<BeanAccessDB> list = new ArrayList<BeanAccessDB>();
|
268
|
+
|
269
|
+
Connection con = null;
|
270
|
+
|
271
|
+
PreparedStatement ps = null;
|
272
|
+
|
273
|
+
try {
|
274
|
+
|
275
|
+
Driver.class.getDeclaredConstructor().newInstance();
|
276
|
+
|
277
|
+
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
String sql = "select * from sushi; ";
|
284
|
+
|
285
|
+
ps = con.prepareStatement(sql.toString());
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
ResultSet rs = ps.executeQuery();
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
while (rs.next()) {
|
294
|
+
|
295
|
+
String id = rs.getString("id");
|
296
|
+
|
297
|
+
System.out.println(id);
|
298
|
+
|
299
|
+
String name = rs.getString("name");
|
300
|
+
|
301
|
+
int price = rs.getInt("price");
|
302
|
+
|
303
|
+
String photo = rs.getString("photo");
|
304
|
+
|
305
|
+
int number = rs.getInt("number");
|
306
|
+
|
307
|
+
list.add(new BeanAccessDB(id, name, price, photo, number));
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
} catch (Exception e) {
|
314
|
+
|
315
|
+
e.printStackTrace();
|
316
|
+
|
317
|
+
} finally {
|
318
|
+
|
319
|
+
try {
|
320
|
+
|
321
|
+
if (ps != null) {
|
322
|
+
|
323
|
+
ps.close();
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
if (con != null) {
|
328
|
+
|
329
|
+
con.close();
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
} catch (Exception e) {
|
334
|
+
|
335
|
+
e.printStackTrace();
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
return list;
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
public List<BeanAccessDB> updateDB(String tempid){
|
348
|
+
|
349
|
+
Connection con = null;
|
350
|
+
|
351
|
+
PreparedStatement ps = null;
|
352
|
+
|
353
|
+
try {
|
354
|
+
|
355
|
+
Driver.class.getDeclaredConstructor().newInstance();
|
356
|
+
|
357
|
+
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
String sql = "update sushi set number = number + 1 where id = ? ";
|
362
|
+
|
363
|
+
ps = con.prepareStatement(sql.toString());
|
364
|
+
|
365
|
+
ps.setString(1, tempid);
|
366
|
+
|
367
|
+
ps.executeUpdate();
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
} catch (Exception e) {
|
372
|
+
|
373
|
+
e.printStackTrace();
|
374
|
+
|
375
|
+
} finally {
|
376
|
+
|
377
|
+
try {
|
378
|
+
|
379
|
+
if (ps != null) {
|
380
|
+
|
381
|
+
ps.close();
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
if (con != null) {
|
386
|
+
|
387
|
+
con.close();
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
} catch (Exception e) {
|
392
|
+
|
393
|
+
e.printStackTrace();
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
return null;
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
|
160
410
|
|
161
411
|
|
162
412
|
|
@@ -164,292 +414,40 @@
|
|
164
414
|
|
165
415
|
```ここに言語を入力
|
166
416
|
|
167
|
-
【B
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
imp
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
i
|
178
|
-
|
179
|
-
i
|
180
|
-
|
181
|
-
i
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
im
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
i
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
private int price;
|
200
|
-
|
201
|
-
private String photo;
|
202
|
-
|
203
|
-
private int number;
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
public BeanAccessDB() {
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
public BeanAccessDB(String id, String name, int price, String photo, int number) {
|
214
|
-
|
215
|
-
this.id = id;
|
216
|
-
|
217
|
-
this.name = name;
|
218
|
-
|
219
|
-
this.price = price;
|
220
|
-
|
221
|
-
this.photo = photo;
|
222
|
-
|
223
|
-
this.number = number;
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
public String getId() {
|
230
|
-
|
231
|
-
return id;
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
public String getName() {
|
236
|
-
|
237
|
-
return name;
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
public int getPrice() {
|
244
|
-
|
245
|
-
return price;
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
public String getPhoto() {
|
252
|
-
|
253
|
-
return photo;
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
public int getNumber() {
|
260
|
-
|
261
|
-
return number;
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
public List<BeanAccessDB> DBtoList() {
|
268
|
-
|
269
|
-
List<BeanAccessDB> list = new ArrayList<BeanAccessDB>();
|
270
|
-
|
271
|
-
Connection con = null;
|
272
|
-
|
273
|
-
PreparedStatement ps = null;
|
274
|
-
|
275
|
-
try {
|
276
|
-
|
277
|
-
Driver.class.getDeclaredConstructor().newInstance();
|
278
|
-
|
279
|
-
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
String sql = "select * from sushi; ";
|
286
|
-
|
287
|
-
ps = con.prepareStatement(sql.toString());
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
ResultSet rs = ps.executeQuery();
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
while (rs.next()) {
|
296
|
-
|
297
|
-
String id = rs.getString("id");
|
298
|
-
|
299
|
-
System.out.println(id);
|
300
|
-
|
301
|
-
String name = rs.getString("name");
|
302
|
-
|
303
|
-
int price = rs.getInt("price");
|
304
|
-
|
305
|
-
String photo = rs.getString("photo");
|
306
|
-
|
307
|
-
int number = rs.getInt("number");
|
308
|
-
|
309
|
-
list.add(new BeanAccessDB(id, name, price, photo, number));
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
} catch (Exception e) {
|
316
|
-
|
317
|
-
e.printStackTrace();
|
318
|
-
|
319
|
-
} finally {
|
320
|
-
|
321
|
-
try {
|
322
|
-
|
323
|
-
if (ps != null) {
|
324
|
-
|
325
|
-
ps.close();
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
if (con != null) {
|
330
|
-
|
331
|
-
con.close();
|
332
|
-
|
333
|
-
}
|
334
|
-
|
335
|
-
} catch (Exception e) {
|
336
|
-
|
337
|
-
e.printStackTrace();
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
return list;
|
344
|
-
|
345
|
-
}
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
public List<BeanAccessDB> updateDB(String tempid){
|
350
|
-
|
351
|
-
Connection con = null;
|
352
|
-
|
353
|
-
PreparedStatement ps = null;
|
354
|
-
|
355
|
-
try {
|
356
|
-
|
357
|
-
Driver.class.getDeclaredConstructor().newInstance();
|
358
|
-
|
359
|
-
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
String sql = "update sushi set number = number + 1 where id = ? ";
|
364
|
-
|
365
|
-
ps = con.prepareStatement(sql.toString());
|
366
|
-
|
367
|
-
ps.setString(1, tempid);
|
368
|
-
|
369
|
-
ps.executeUpdate();
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
} catch (Exception e) {
|
374
|
-
|
375
|
-
e.printStackTrace();
|
376
|
-
|
377
|
-
} finally {
|
378
|
-
|
379
|
-
try {
|
380
|
-
|
381
|
-
if (ps != null) {
|
382
|
-
|
383
|
-
ps.close();
|
384
|
-
|
385
|
-
}
|
386
|
-
|
387
|
-
if (con != null) {
|
388
|
-
|
389
|
-
con.close();
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
} catch (Exception e) {
|
394
|
-
|
395
|
-
e.printStackTrace();
|
396
|
-
|
397
|
-
}
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
return null;
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
}
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
417
|
+
【SQL TABLE sushi】
|
418
|
+
|
419
|
+
select * from sushi;
|
420
|
+
|
421
|
+
+-----+------------+-------+--------------+--------+
|
422
|
+
|
423
|
+
| id | name | price | photo | number |
|
424
|
+
|
425
|
+
+-----+------------+-------+--------------+--------+
|
426
|
+
|
427
|
+
| 001 | えび | 240 | ebi.png | 0 |
|
428
|
+
|
429
|
+
| 002 | いか | 210 | ika.png | 0 |
|
430
|
+
|
431
|
+
| 003 | いくら | 300 | ikura.png | 0 |
|
432
|
+
|
433
|
+
| 004 | まぐろ | 150 | maguro.png | 0 |
|
434
|
+
|
435
|
+
| 005 | しめさば | 200 | simesaba.png | 0 |
|
436
|
+
|
437
|
+
| 006 | たまご | 140 | tamago.png | 0 |
|
438
|
+
|
439
|
+
| 007 | うに | 400 | uni.png | 0 |
|
440
|
+
|
441
|
+
| 008 | 太巻き | 500 | futomaki.png | 0 |
|
442
|
+
|
443
|
+
| 009 | 手巻き寿司 | 550 | temaki.png | 0 |
|
444
|
+
|
445
|
+
| 010 | ちらし寿司 | 1200 | tirasi.png | 0 |
|
446
|
+
|
447
|
+
+-----+------------+-------+--------------+--------+
|
414
448
|
|
415
449
|
```
|
416
450
|
|
417
|
-
```ここに言語を入力
|
418
|
-
|
419
|
-
【SQL TABLE sushi】
|
420
|
-
|
421
|
-
select * from sushi;
|
422
|
-
|
423
|
-
+-----+------------+-------+--------------+--------+
|
424
|
-
|
425
|
-
| id | name | price | photo | number |
|
426
|
-
|
427
|
-
+-----+------------+-------+--------------+--------+
|
428
|
-
|
429
|
-
| 001 | えび | 240 | ebi.png | 0 |
|
430
|
-
|
431
|
-
| 002 | いか | 210 | ika.png | 0 |
|
432
|
-
|
433
|
-
| 003 | いくら | 300 | ikura.png | 0 |
|
434
|
-
|
435
|
-
| 004 | まぐろ | 150 | maguro.png | 0 |
|
436
|
-
|
437
|
-
| 005 | しめさば | 200 | simesaba.png | 0 |
|
438
|
-
|
439
|
-
| 006 | たまご | 140 | tamago.png | 0 |
|
440
|
-
|
441
|
-
| 007 | うに | 400 | uni.png | 0 |
|
442
|
-
|
443
|
-
| 008 | 太巻き | 500 | futomaki.png | 0 |
|
444
|
-
|
445
|
-
| 009 | 手巻き寿司 | 550 | temaki.png | 0 |
|
446
|
-
|
447
|
-
| 010 | ちらし寿司 | 1200 | tirasi.png | 0 |
|
448
|
-
|
449
|
-
+-----+------------+-------+--------------+--------+
|
450
|
-
|
451
|
-
```
|
452
|
-
|
453
451
|
|
454
452
|
|
455
453
|
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,81 +52,363 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
55
|
-
###
|
59
|
+
### 該当のソースコード
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```【driveBean.jsp】
|
64
|
+
|
65
|
+
【driveBean.jsp】
|
66
|
+
|
67
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
68
|
+
|
69
|
+
pageEncoding="UTF-8"%>
|
70
|
+
|
71
|
+
<% request.setCharacterEncoding("UTF-8"); %>
|
72
|
+
|
73
|
+
<%@ page import="java.util.*" %>
|
74
|
+
|
75
|
+
<%@ page import="bean.*" %>
|
76
|
+
|
77
|
+
<jsp:useBean id="obj" class="bean.BeanAccessDB" />
|
78
|
+
|
79
|
+
<!DOCTYPE html>
|
80
|
+
|
81
|
+
<html><head><title>DBを読む</title></head>
|
82
|
+
|
83
|
+
<body>
|
84
|
+
|
85
|
+
<%
|
86
|
+
|
87
|
+
List<BeanAccessDB> list = obj.DBtoList(); // 下部に説明あり
|
88
|
+
|
89
|
+
int sum = 0;
|
90
|
+
|
91
|
+
for(int i = 0; i < list.size(); i++){
|
92
|
+
|
93
|
+
obj = list.get(i);// get()メソッドでArrayListから1件データを取出し、BeanAccessDBクラスのオブジェクトに入れる
|
94
|
+
|
95
|
+
%>
|
96
|
+
|
97
|
+
<% sum += obj.getPrice();%>
|
98
|
+
|
99
|
+
<a href="driveBean2.jsp?linkdata=<%= obj.getId() %>"> <%= obj.getId() %> </a>
|
100
|
+
|
101
|
+
<%= obj.getName() %>、
|
102
|
+
|
103
|
+
<%= obj.getPrice() %>
|
104
|
+
|
105
|
+
<img src="images/寿司の画像/<%= obj.getPhoto() %>" width="70" height="70" />
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
<%}%>
|
112
|
+
|
113
|
+
<%
|
114
|
+
|
115
|
+
out.print("合計:" + sum + "円");
|
116
|
+
|
117
|
+
%>
|
118
|
+
|
119
|
+
</body>
|
120
|
+
|
121
|
+
</html>
|
56
122
|
|
57
123
|
|
58
124
|
|
59
125
|
```
|
60
126
|
|
61
|
-
|
127
|
+
```ここに言語を入力
|
128
|
+
|
129
|
+
【driveBean2.jsp】
|
130
|
+
|
131
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
132
|
+
|
133
|
+
pageEncoding="UTF-8"%>
|
134
|
+
|
135
|
+
<% request.setCharacterEncoding("UTF-8"); %>
|
136
|
+
|
137
|
+
<!DOCTYPE html>
|
138
|
+
|
139
|
+
<html>
|
140
|
+
|
141
|
+
<head>
|
142
|
+
|
143
|
+
<meta charset="ISO-8859-1">
|
144
|
+
|
145
|
+
<title>Insert title here</title>
|
146
|
+
|
147
|
+
</head>
|
148
|
+
|
149
|
+
<body>
|
150
|
+
|
151
|
+
<% String linkdata = request.getParameter("linkdata");
|
152
|
+
|
153
|
+
%>
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
<jsp:forward page="driveBean.jsp".jsp/>
|
158
|
+
|
159
|
+
</body>
|
160
|
+
|
161
|
+
|
62
162
|
|
63
163
|
```
|
64
164
|
|
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
|
-
i
|
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
|
-
|
165
|
+
```ここに言語を入力
|
166
|
+
|
167
|
+
【BeanAccessDB.java】
|
168
|
+
|
169
|
+
package bean;
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
import java.lang.reflect.InvocationTargetException;
|
174
|
+
|
175
|
+
import java.sql.Connection;
|
176
|
+
|
177
|
+
import java.sql.DriverManager;
|
178
|
+
|
179
|
+
import java.sql.PreparedStatement;
|
180
|
+
|
181
|
+
import java.sql.ResultSet;
|
182
|
+
|
183
|
+
import java.sql.SQLException;
|
184
|
+
|
185
|
+
import java.util.ArrayList;
|
186
|
+
|
187
|
+
import java.util.List;
|
188
|
+
|
189
|
+
import org.mariadb.jdbc.Driver;
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
public class BeanAccessDB {
|
194
|
+
|
195
|
+
private String id;
|
196
|
+
|
197
|
+
private String name;
|
198
|
+
|
199
|
+
private int price;
|
200
|
+
|
201
|
+
private String photo;
|
202
|
+
|
203
|
+
private int number;
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
public BeanAccessDB() {
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
public BeanAccessDB(String id, String name, int price, String photo, int number) {
|
214
|
+
|
215
|
+
this.id = id;
|
216
|
+
|
217
|
+
this.name = name;
|
218
|
+
|
219
|
+
this.price = price;
|
220
|
+
|
221
|
+
this.photo = photo;
|
222
|
+
|
223
|
+
this.number = number;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
public String getId() {
|
230
|
+
|
231
|
+
return id;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
public String getName() {
|
236
|
+
|
237
|
+
return name;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
public int getPrice() {
|
244
|
+
|
245
|
+
return price;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
public String getPhoto() {
|
252
|
+
|
253
|
+
return photo;
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
public int getNumber() {
|
260
|
+
|
261
|
+
return number;
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
public List<BeanAccessDB> DBtoList() {
|
268
|
+
|
269
|
+
List<BeanAccessDB> list = new ArrayList<BeanAccessDB>();
|
270
|
+
|
271
|
+
Connection con = null;
|
272
|
+
|
273
|
+
PreparedStatement ps = null;
|
274
|
+
|
275
|
+
try {
|
276
|
+
|
277
|
+
Driver.class.getDeclaredConstructor().newInstance();
|
278
|
+
|
279
|
+
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
String sql = "select * from sushi; ";
|
286
|
+
|
287
|
+
ps = con.prepareStatement(sql.toString());
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
ResultSet rs = ps.executeQuery();
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
while (rs.next()) {
|
296
|
+
|
297
|
+
String id = rs.getString("id");
|
298
|
+
|
299
|
+
System.out.println(id);
|
300
|
+
|
301
|
+
String name = rs.getString("name");
|
302
|
+
|
303
|
+
int price = rs.getInt("price");
|
304
|
+
|
305
|
+
String photo = rs.getString("photo");
|
306
|
+
|
307
|
+
int number = rs.getInt("number");
|
308
|
+
|
309
|
+
list.add(new BeanAccessDB(id, name, price, photo, number));
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
} catch (Exception e) {
|
316
|
+
|
317
|
+
e.printStackTrace();
|
318
|
+
|
319
|
+
} finally {
|
320
|
+
|
321
|
+
try {
|
322
|
+
|
323
|
+
if (ps != null) {
|
324
|
+
|
325
|
+
ps.close();
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
if (con != null) {
|
330
|
+
|
331
|
+
con.close();
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
} catch (Exception e) {
|
336
|
+
|
337
|
+
e.printStackTrace();
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
return list;
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
public List<BeanAccessDB> updateDB(String tempid){
|
350
|
+
|
351
|
+
Connection con = null;
|
352
|
+
|
353
|
+
PreparedStatement ps = null;
|
354
|
+
|
355
|
+
try {
|
356
|
+
|
357
|
+
Driver.class.getDeclaredConstructor().newInstance();
|
358
|
+
|
359
|
+
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
String sql = "update sushi set number = number + 1 where id = ? ";
|
364
|
+
|
365
|
+
ps = con.prepareStatement(sql.toString());
|
366
|
+
|
367
|
+
ps.setString(1, tempid);
|
368
|
+
|
369
|
+
ps.executeUpdate();
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
} catch (Exception e) {
|
374
|
+
|
375
|
+
e.printStackTrace();
|
376
|
+
|
377
|
+
} finally {
|
378
|
+
|
379
|
+
try {
|
380
|
+
|
381
|
+
if (ps != null) {
|
382
|
+
|
383
|
+
ps.close();
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
if (con != null) {
|
388
|
+
|
389
|
+
con.close();
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
} catch (Exception e) {
|
394
|
+
|
395
|
+
e.printStackTrace();
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
return null;
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
|
130
412
|
|
131
413
|
|
132
414
|
|
@@ -134,335 +416,49 @@
|
|
134
416
|
|
135
417
|
```ここに言語を入力
|
136
418
|
|
137
|
-
【
|
419
|
+
【SQL TABLE sushi】
|
138
|
-
|
139
|
-
|
420
|
+
|
140
|
-
|
141
|
-
pageEncoding="UTF-8"%>
|
142
|
-
|
143
|
-
<% request.setCharacterEncoding("UTF-8"); %>
|
144
|
-
|
145
|
-
|
421
|
+
select * from sushi;
|
146
|
-
|
422
|
+
|
147
|
-
|
423
|
+
+-----+------------+-------+--------------+--------+
|
148
|
-
|
149
|
-
|
424
|
+
|
150
|
-
|
151
|
-
|
425
|
+
| id | name | price | photo | number |
|
426
|
+
|
152
|
-
|
427
|
+
+-----+------------+-------+--------------+--------+
|
428
|
+
|
429
|
+
| 001 | えび | 240 | ebi.png | 0 |
|
430
|
+
|
431
|
+
| 002 | いか | 210 | ika.png | 0 |
|
432
|
+
|
433
|
+
| 003 | いくら | 300 | ikura.png | 0 |
|
434
|
+
|
435
|
+
| 004 | まぐろ | 150 | maguro.png | 0 |
|
436
|
+
|
153
|
-
|
437
|
+
| 005 | しめさば | 200 | simesaba.png | 0 |
|
154
|
-
|
155
|
-
|
438
|
+
|
156
|
-
|
157
|
-
<body>
|
158
|
-
|
159
|
-
|
439
|
+
| 006 | たまご | 140 | tamago.png | 0 |
|
160
|
-
|
161
|
-
|
440
|
+
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
441
|
+
| 007 | うに | 400 | uni.png | 0 |
|
442
|
+
|
166
|
-
|
443
|
+
| 008 | 太巻き | 500 | futomaki.png | 0 |
|
444
|
+
|
445
|
+
| 009 | 手巻き寿司 | 550 | temaki.png | 0 |
|
446
|
+
|
167
|
-
|
447
|
+
| 010 | ちらし寿司 | 1200 | tirasi.png | 0 |
|
448
|
+
|
168
|
-
|
449
|
+
+-----+------------+-------+--------------+--------+
|
169
|
-
|
170
450
|
|
171
451
|
```
|
172
452
|
|
173
|
-
|
453
|
+
|
174
|
-
|
175
|
-
|
454
|
+
|
176
|
-
|
177
|
-
|
455
|
+
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
import java.lang.reflect.InvocationTargetException;
|
182
|
-
|
183
|
-
import java.sql.Connection;
|
184
|
-
|
185
|
-
import java.sql.DriverManager;
|
186
|
-
|
187
|
-
import java.sql.PreparedStatement;
|
188
|
-
|
189
|
-
import java.sql.ResultSet;
|
190
|
-
|
191
|
-
import java.sql.SQLException;
|
192
|
-
|
193
|
-
import java.util.ArrayList;
|
194
|
-
|
195
|
-
import java.util.List;
|
196
|
-
|
197
|
-
import org.mariadb.jdbc.Driver;
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
public class BeanAccessDB {
|
202
|
-
|
203
|
-
private String id;
|
204
|
-
|
205
|
-
private String name;
|
206
|
-
|
207
|
-
private int price;
|
208
|
-
|
209
|
-
private String photo;
|
210
|
-
|
211
|
-
private int number;
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
public BeanAccessDB() {
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
public BeanAccessDB(String id, String name, int price, String photo, int number) {
|
222
|
-
|
223
|
-
this.id = id;
|
224
|
-
|
225
|
-
this.name = name;
|
226
|
-
|
227
|
-
this.price = price;
|
228
|
-
|
229
|
-
this.photo = photo;
|
230
|
-
|
231
|
-
this.number = number;
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
public String getId() {
|
238
|
-
|
239
|
-
return id;
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
public String getName() {
|
244
|
-
|
245
|
-
return name;
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
public int getPrice() {
|
252
|
-
|
253
|
-
return price;
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
public String getPhoto() {
|
260
|
-
|
261
|
-
return photo;
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
public int getNumber() {
|
268
|
-
|
269
|
-
return number;
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
public List<BeanAccessDB> DBtoList() {
|
276
|
-
|
277
|
-
List<BeanAccessDB> list = new ArrayList<BeanAccessDB>();
|
278
|
-
|
279
|
-
Connection con = null;
|
280
|
-
|
281
|
-
PreparedStatement ps = null;
|
282
|
-
|
283
|
-
try {
|
284
|
-
|
285
|
-
Driver.class.getDeclaredConstructor().newInstance();
|
286
|
-
|
287
|
-
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
String sql = "select * from sushi; ";
|
294
|
-
|
295
|
-
ps = con.prepareStatement(sql.toString());
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
ResultSet rs = ps.executeQuery();
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
while (rs.next()) {
|
304
|
-
|
305
|
-
String id = rs.getString("id");
|
306
|
-
|
307
|
-
System.out.println(id);
|
308
|
-
|
309
|
-
String name = rs.getString("name");
|
310
|
-
|
311
|
-
int price = rs.getInt("price");
|
312
|
-
|
313
|
-
String photo = rs.getString("photo");
|
314
|
-
|
315
|
-
int number = rs.getInt("number");
|
316
|
-
|
317
|
-
list.add(new BeanAccessDB(id, name, price, photo, number));
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
} catch (Exception e) {
|
324
|
-
|
325
|
-
e.printStackTrace();
|
326
|
-
|
327
|
-
} finally {
|
328
|
-
|
329
|
-
try {
|
330
|
-
|
331
|
-
if (ps != null) {
|
332
|
-
|
333
|
-
ps.close();
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
if (con != null) {
|
338
|
-
|
339
|
-
con.close();
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
} catch (Exception e) {
|
344
|
-
|
345
|
-
e.printStackTrace();
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
return list;
|
352
|
-
|
353
|
-
}
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
public List<BeanAccessDB> updateDB(String tempid){
|
358
|
-
|
359
|
-
Connection con = null;
|
360
|
-
|
361
|
-
PreparedStatement ps = null;
|
362
|
-
|
363
|
-
try {
|
364
|
-
|
365
|
-
Driver.class.getDeclaredConstructor().newInstance();
|
366
|
-
|
367
|
-
con = DriverManager.getConnection("jdbc:mariadb://localhost/studyDB", "root", "");
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
String sql = "update sushi set number = number + 1 where id = ? ";
|
372
|
-
|
373
|
-
ps = con.prepareStatement(sql.toString());
|
374
|
-
|
375
|
-
ps.setString(1, tempid);
|
376
|
-
|
377
|
-
ps.executeUpdate();
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
} catch (Exception e) {
|
382
|
-
|
383
|
-
e.printStackTrace();
|
384
|
-
|
385
|
-
} finally {
|
386
|
-
|
387
|
-
try {
|
388
|
-
|
389
|
-
if (ps != null) {
|
390
|
-
|
391
|
-
ps.close();
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
if (con != null) {
|
396
|
-
|
397
|
-
con.close();
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
} catch (Exception e) {
|
402
|
-
|
403
|
-
e.printStackTrace();
|
404
|
-
|
405
|
-
}
|
406
|
-
|
407
|
-
}
|
408
|
-
|
409
|
-
return null;
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
```
|
424
|
-
|
425
|
-
```ここに言語を入力
|
426
|
-
|
427
|
-
【SQL TABLE sushi】
|
428
|
-
|
429
|
-
select * from sushi;
|
430
|
-
|
431
|
-
+-----+------------+-------+--------------+--------+
|
432
|
-
|
433
|
-
| id | name | price | photo | number |
|
434
|
-
|
435
|
-
+-----+------------+-------+--------------+--------+
|
436
|
-
|
437
|
-
| 001 | えび | 240 | ebi.png | 0 |
|
438
|
-
|
439
|
-
| 002 | いか | 210 | ika.png | 0 |
|
440
|
-
|
441
|
-
| 003 | いくら | 300 | ikura.png | 0 |
|
442
|
-
|
443
|
-
| 004 | まぐろ | 150 | maguro.png | 0 |
|
444
|
-
|
445
|
-
| 005 | しめさば | 200 | simesaba.png | 0 |
|
446
|
-
|
447
|
-
| 006 | たまご | 140 | tamago.png | 0 |
|
448
|
-
|
449
|
-
| 007 | うに | 400 | uni.png | 0 |
|
450
|
-
|
451
|
-
| 008 | 太巻き | 500 | futomaki.png | 0 |
|
452
|
-
|
453
|
-
| 009 | 手巻き寿司 | 550 | temaki.png | 0 |
|
454
|
-
|
455
|
-
| 010 | ちらし寿司 | 1200 | tirasi.png | 0 |
|
456
|
-
|
457
|
-
+-----+------------+-------+--------------+--------+
|
458
|
-
|
459
|
-
```
|
460
456
|
|
461
457
|
### 試したこと
|
462
458
|
|
463
|
-
|
464
|
-
|
465
|
-
ここ
|
459
|
+
自分で書ける範囲のコードは書きましたがここまでで限界でした。
|
460
|
+
|
461
|
+
|
466
462
|
|
467
463
|
|
468
464
|
|
@@ -471,53 +467,3 @@
|
|
471
467
|
|
472
468
|
|
473
469
|
ここにより詳細な情報を記載してください。
|
474
|
-
|
475
|
-
### 前提・実現したいこと
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
ここに質問の内容を詳しく書いてください。
|
480
|
-
|
481
|
-
(例)PHP(CakePHP)で●●なシステムを作っています。
|
482
|
-
|
483
|
-
■■な機能を実装中に以下のエラーメッセージが発生しました。
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
### 発生している問題・エラーメッセージ
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
```
|
492
|
-
|
493
|
-
エラーメッセージ
|
494
|
-
|
495
|
-
```
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
### 該当のソースコード
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
```ここに言語名を入力
|
504
|
-
|
505
|
-
ソースコード
|
506
|
-
|
507
|
-
```
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
### 試したこと
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
ここに問題に対して試したことを記載してください。
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
### 補足情報(FW/ツールのバージョンなど)
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
ここにより詳細な情報を記載してください。
|