質問編集履歴
2
問題点を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,16 +1,14 @@
|
|
1
|
-
### Java DBの情報更新が出来ないです。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
ここに質問の内容を詳しく書いてください。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
Javaで会員更新機能を作っています。
|
10
|
-
|
11
|
-
executeUpdate();
|
1
|
+
### executeUpdate();を利用した更新
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
12
|
-
|
7
|
+
Javaで会員情報更新機能を作ってるのですが、POST送信先がNULLで受け渡しが出来ていないのとexecuteUpdate();の使い方が誤った状態と思って作業したのですが、分からないので書き込みました。
|
8
|
+
|
9
|
+
|
10
|
+
|
13
|
-
###
|
11
|
+
### 会員情報変更POSTクラス
|
14
12
|
|
15
13
|
|
16
14
|
|
@@ -94,12 +92,38 @@
|
|
94
92
|
|
95
93
|
<textarea name="memo" rows="4" cols="41" maxlength="100">${customer.memo}</textarea>
|
96
94
|
|
95
|
+
<input type="hidden" name="id" value="${customer.id}">
|
96
|
+
|
97
97
|
<input type="submit" value="更新">
|
98
98
|
|
99
99
|
</c:forEach>
|
100
100
|
|
101
101
|
</form>
|
102
102
|
|
103
|
+
<form action="delete" method="post">
|
104
|
+
|
105
|
+
<c:forEach var="customer" items="${customerList}">
|
106
|
+
|
107
|
+
<input type="hidden" name="id" value="${customer.id}">
|
108
|
+
|
109
|
+
<input type="hidden" name="email" value="${customer.email}">
|
110
|
+
|
111
|
+
<input type="hidden" name="password" value="${customer.password}">
|
112
|
+
|
113
|
+
<input type="hidden" name="name" value="${customer.name}">
|
114
|
+
|
115
|
+
<input type="hidden" name="gender" value="${customer.gender}">
|
116
|
+
|
117
|
+
<input type="hidden" name="blood" value="${customer.blood}">
|
118
|
+
|
119
|
+
<input type="hidden" name="memo" value="${customer.memo}">
|
120
|
+
|
121
|
+
<input type="submit" value="削除">
|
122
|
+
|
123
|
+
</c:forEach>
|
124
|
+
|
125
|
+
</form>
|
126
|
+
|
103
127
|
</div>
|
104
128
|
|
105
129
|
</body>
|
@@ -110,7 +134,7 @@
|
|
110
134
|
|
111
135
|
|
112
136
|
|
113
|
-
###
|
137
|
+
### POSTされた値をDBに接続するクラス
|
114
138
|
|
115
139
|
|
116
140
|
|
@@ -172,7 +196,9 @@
|
|
172
196
|
|
173
197
|
ps.executeUpdate();
|
174
198
|
|
175
|
-
|
199
|
+
ps.close();
|
200
|
+
|
201
|
+
coon.commit();
|
176
202
|
|
177
203
|
} catch (SQLException | ClassNotFoundException e) {
|
178
204
|
|
@@ -188,7 +214,7 @@
|
|
188
214
|
|
189
215
|
|
190
216
|
|
191
|
-
###
|
217
|
+
### DBクラスを呼び出すクラス
|
192
218
|
|
193
219
|
|
194
220
|
|
@@ -200,8 +226,6 @@
|
|
200
226
|
|
201
227
|
import java.io.IOException;
|
202
228
|
|
203
|
-
import java.util.List;
|
204
|
-
|
205
229
|
|
206
230
|
|
207
231
|
import javax.servlet.ServletContext;
|
@@ -218,8 +242,6 @@
|
|
218
242
|
|
219
243
|
|
220
244
|
|
221
|
-
import data.Customer;
|
222
|
-
|
223
245
|
import service.UpdateService;
|
224
246
|
|
225
247
|
|
@@ -250,9 +272,7 @@
|
|
250
272
|
|
251
273
|
UpdateService updateservice = new UpdateService();
|
252
274
|
|
253
|
-
|
275
|
+
updateservice.updateCustomer(email,password,name,gender,blood,old,id);
|
254
|
-
|
255
|
-
request.setAttribute("customerList", customerList);
|
256
276
|
|
257
277
|
ServletContext sc = getServletContext();
|
258
278
|
|
1
コード変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,9 +4,7 @@
|
|
4
4
|
|
5
5
|
ここに質問の内容を詳しく書いてください。
|
6
6
|
|
7
|
-
|
7
|
+
|
8
|
-
|
9
|
-
■■な機能を実装中に以下のエラーメッセージが発生しました。
|
10
8
|
|
11
9
|
Javaで会員更新機能を作っています。
|
12
10
|
|
@@ -128,99 +126,137 @@
|
|
128
126
|
|
129
127
|
import java.sql.PreparedStatement;
|
130
128
|
|
131
|
-
import java.sql.ResultSet;
|
132
|
-
|
133
129
|
import java.sql.SQLException;
|
134
130
|
|
131
|
+
|
132
|
+
|
133
|
+
public class UpdateService {
|
134
|
+
|
135
|
+
public void updateCustomer (String email, String password, String name, String gender, String blood, String old, String id) {
|
136
|
+
|
137
|
+
String dbName = "system_user";
|
138
|
+
|
139
|
+
String dbPropaties = "?cvharacterEncoding=UTF-8&serverTimezone=JST";
|
140
|
+
|
141
|
+
String dbUrl ="jdbc:mySQL://localhost:3306/" + dbName +dbPropaties;
|
142
|
+
|
143
|
+
String dbUser = "root";
|
144
|
+
|
145
|
+
String dbPassword = "admin";
|
146
|
+
|
147
|
+
String sql ="update customer set email=?, password=?, name=?, gender=?, blood=?, old=? where id=?;";
|
148
|
+
|
149
|
+
try {
|
150
|
+
|
151
|
+
Class.forName("com.mysql.jdbc.Driver");
|
152
|
+
|
153
|
+
Connection coon = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
|
154
|
+
|
155
|
+
PreparedStatement ps = coon.prepareStatement(sql);
|
156
|
+
|
157
|
+
//プリペアステートメントでマイページで選択した値を代入
|
158
|
+
|
159
|
+
ps.setString(1, email);
|
160
|
+
|
161
|
+
ps.setString(2, password);
|
162
|
+
|
163
|
+
ps.setString(3, name);
|
164
|
+
|
165
|
+
ps.setString(4, gender);
|
166
|
+
|
167
|
+
ps.setString(5, blood);
|
168
|
+
|
169
|
+
ps.setString(6, old);
|
170
|
+
|
171
|
+
ps.setString(7, id);
|
172
|
+
|
173
|
+
ps.executeUpdate();
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
} catch (SQLException | ClassNotFoundException e) {
|
178
|
+
|
179
|
+
e.printStackTrace();
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
### updadteメソッドを呼び出すクラス
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
```ここに言語を入力
|
196
|
+
|
197
|
+
package controller;
|
198
|
+
|
199
|
+
|
200
|
+
|
135
|
-
import java.
|
201
|
+
import java.io.IOException;
|
136
202
|
|
137
203
|
import java.util.List;
|
138
204
|
|
139
205
|
|
140
206
|
|
207
|
+
import javax.servlet.ServletContext;
|
208
|
+
|
209
|
+
import javax.servlet.ServletException;
|
210
|
+
|
211
|
+
import javax.servlet.annotation.WebServlet;
|
212
|
+
|
213
|
+
import javax.servlet.http.HttpServlet;
|
214
|
+
|
215
|
+
import javax.servlet.http.HttpServletRequest;
|
216
|
+
|
217
|
+
import javax.servlet.http.HttpServletResponse;
|
218
|
+
|
219
|
+
|
220
|
+
|
141
221
|
import data.Customer;
|
142
222
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
St
|
156
|
-
|
157
|
-
String d
|
158
|
-
|
159
|
-
String
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
tr
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
ps.setString(5, blood);
|
182
|
-
|
183
|
-
ps.setString(6, old);
|
184
|
-
|
185
|
-
ps.setString(7, id);
|
186
|
-
|
187
|
-
ps.executeUpdate();
|
188
|
-
|
189
|
-
ps.close();
|
190
|
-
|
191
|
-
ResultSet rs = ps.executeQuery();
|
192
|
-
|
193
|
-
while (rs.next()) {
|
194
|
-
|
195
|
-
Customer customer = new Customer();
|
196
|
-
|
197
|
-
customer.setId(rs.getInt(id));
|
198
|
-
|
199
|
-
customer.setPassword(rs.getString(password));
|
200
|
-
|
201
|
-
customer.setEmail(rs.getString(email));
|
202
|
-
|
203
|
-
customer.setName(rs.getString(name));
|
204
|
-
|
205
|
-
customer.setGender(rs.getString(gender));
|
206
|
-
|
207
|
-
customer.setBlood(rs.getString(blood));
|
208
|
-
|
209
|
-
customer.setOld(rs.getInt(old));
|
210
|
-
|
211
|
-
//レコードの件数分customerListに追加する
|
212
|
-
|
213
|
-
customerList.add(customer);
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
} catch (SQLException | ClassNotFoundException e) {
|
218
|
-
|
219
|
-
e.printStackTrace();
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
return customerList;
|
223
|
+
import service.UpdateService;
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
@WebServlet("/update")
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
public class UpdateController extends HttpServlet{
|
232
|
+
|
233
|
+
protected void doPost (HttpServletRequest request, HttpServletResponse response)
|
234
|
+
|
235
|
+
throws ServletException, IOException{//22行目MySQLにログインする
|
236
|
+
|
237
|
+
String id = request.getParameter("id");
|
238
|
+
|
239
|
+
String email = request.getParameter("email");
|
240
|
+
|
241
|
+
String password = request.getParameter("password");
|
242
|
+
|
243
|
+
String name = request.getParameter("name");
|
244
|
+
|
245
|
+
String gender = request.getParameter("gender");
|
246
|
+
|
247
|
+
String blood = request.getParameter("blood");
|
248
|
+
|
249
|
+
String old = request.getParameter("old");
|
250
|
+
|
251
|
+
UpdateService updateservice = new UpdateService();
|
252
|
+
|
253
|
+
List<Customer> customerList = updateservice.updateCustomer(email,password,name,gender,blood,old,id);
|
254
|
+
|
255
|
+
request.setAttribute("customerList", customerList);
|
256
|
+
|
257
|
+
ServletContext sc = getServletContext();
|
258
|
+
|
259
|
+
sc.getRequestDispatcher("/" + "update.jsp").forward(request, response);
|
224
260
|
|
225
261
|
}
|
226
262
|
|
@@ -230,84 +266,6 @@
|
|
230
266
|
|
231
267
|
|
232
268
|
|
233
|
-
### updadteメソッドを呼び出すクラス
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
```ここに言語を入力
|
238
|
-
|
239
|
-
package controller;
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
import java.io.IOException;
|
244
|
-
|
245
|
-
import java.util.List;
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
import javax.servlet.ServletContext;
|
250
|
-
|
251
|
-
import javax.servlet.ServletException;
|
252
|
-
|
253
|
-
import javax.servlet.annotation.WebServlet;
|
254
|
-
|
255
|
-
import javax.servlet.http.HttpServlet;
|
256
|
-
|
257
|
-
import javax.servlet.http.HttpServletRequest;
|
258
|
-
|
259
|
-
import javax.servlet.http.HttpServletResponse;
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
import data.Customer;
|
264
|
-
|
265
|
-
import service.UpdateService;
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
@WebServlet("/update")
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
public class UpdateController extends HttpServlet{
|
274
|
-
|
275
|
-
protected void doPost (HttpServletRequest request, HttpServletResponse response)
|
276
|
-
|
277
|
-
throws ServletException, IOException{//22行目MySQLにログインする
|
278
|
-
|
279
|
-
String id = request.getParameter("id");
|
280
|
-
|
281
|
-
String email = request.getParameter("email");
|
282
|
-
|
283
|
-
String password = request.getParameter("password");
|
284
|
-
|
285
|
-
String name = request.getParameter("name");
|
286
|
-
|
287
|
-
String gender = request.getParameter("gender");
|
288
|
-
|
289
|
-
String blood = request.getParameter("blood");
|
290
|
-
|
291
|
-
String old = request.getParameter("old");
|
292
|
-
|
293
|
-
UpdateService updateservice = new UpdateService();
|
294
|
-
|
295
|
-
List<Customer> customerList = updateservice.updateCustomer(email,password,name,gender,blood,old,id);
|
296
|
-
|
297
|
-
request.setAttribute("customerList", customerList);
|
298
|
-
|
299
|
-
ServletContext sc = getServletContext();
|
300
|
-
|
301
|
-
sc.getRequestDispatcher("/" + "update.jsp").forward(request, response);
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
```
|
308
|
-
|
309
|
-
|
310
|
-
|
311
269
|
### 補足情報(FW/ツールのバージョンなど)
|
312
270
|
|
313
271
|
|