質問編集履歴
1
文章の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,61 +1,217 @@
|
|
1
1
|
javaのWebアプリケーション作成で質問があります。
|
2
2
|
|
3
|
-
MVCに沿ってファイルを分けて作成したいのですが、DBの更新処理
|
3
|
+
MVCに沿ってファイルを分けて作成したいのですが、DBの更新処理がうまくいきません。
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
|
7
|
-
Controller
|
5
|
+
Controller→Model間の正しいコードが分からなかったのでヒントでも教えていただけると助かります。
|
8
|
-
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
-
|
9
|
+
JSP(一部抜粋)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
|
14
|
-
|
15
|
-
編集画面で名前と価格を編集し、更新ボタンを押すと更新画面に移動する。
|
16
|
-
|
17
|
-
|
11
|
+
</tr>
|
18
|
-
|
19
|
-
|
12
|
+
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
13
|
+
<%
|
14
|
+
|
24
|
-
|
15
|
+
List<Bean> Prodcutlist = (List<Bean>)request.getAttribute("Prodcutlist");
|
16
|
+
|
25
|
-
|
17
|
+
for (Bean bean : Prodcutlist) {
|
18
|
+
|
19
|
+
%>
|
20
|
+
|
21
|
+
<tr>
|
22
|
+
|
23
|
+
<td><%=bean.getId()%></td>
|
24
|
+
|
25
|
+
<td><%=bean.getName()%></td>
|
26
|
+
|
27
|
+
<td><%=bean.getPrice()%></td>
|
26
28
|
|
27
29
|
<form action="/Product_management/edit" method="Get">
|
28
30
|
|
31
|
+
<input type="hidden" name="id" value="<%=bean.getId()%>">
|
32
|
+
|
33
|
+
<input type="hidden" name="name" value="<%=bean.getName()%>">
|
34
|
+
|
35
|
+
<input type="hidden" name="price" value="<%=bean.getPrice()%>">
|
36
|
+
|
37
|
+
<input type="submit" onClick="location.href='http://localhost:8080/Product_management/edit'" value="編集">
|
38
|
+
|
29
|
-
|
39
|
+
</form>
|
30
40
|
|
31
41
|
|
32
42
|
|
33
43
|
Controller
|
34
44
|
|
45
|
+
|
46
|
+
|
47
|
+
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
ManagementModel model = new ManagementModel();
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
ArrayList<Bean> Prodcutlist = model.getBeanlist();
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
req.setAttribute("Prodcutlist", Prodcutlist);
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
String name = req.getParameter("name");
|
64
|
+
|
65
|
+
String price = req.getParameter("price");
|
66
|
+
|
67
|
+
String id = req.getParameter("id");
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
RequestDispatcher rd = req.getRequestDispatcher("/EditingScreen.jsp");
|
72
|
+
|
73
|
+
rd.forward(req, res);
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
String name = req.getParameter("name");
|
84
|
+
|
85
|
+
String price = req.getParameter("price");
|
86
|
+
|
87
|
+
String id = req.getParameter("id");
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
EditModel dao = new EditModel();
|
92
|
+
|
35
|
-
|
93
|
+
dao.list(id, name, price);
|
94
|
+
|
95
|
+
|
96
|
+
|
36
|
-
|
97
|
+
res.sendRedirect("/EditingScreen.jsp");
|
98
|
+
|
37
|
-
|
99
|
+
}
|
38
100
|
|
39
101
|
|
40
102
|
|
41
103
|
Model
|
42
104
|
|
105
|
+
|
106
|
+
|
43
|
-
|
107
|
+
public class EditModel {
|
108
|
+
|
109
|
+
|
110
|
+
|
44
|
-
|
111
|
+
private Connection con_ = null;
|
112
|
+
|
45
|
-
|
113
|
+
private ResultSet rs_ = null;
|
114
|
+
|
46
|
-
|
115
|
+
private PreparedStatement ps_ = null;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
public void list(String id, String name, String price) {
|
120
|
+
|
121
|
+
|
122
|
+
|
47
|
-
|
123
|
+
try {
|
48
|
-
|
124
|
+
|
125
|
+
|
126
|
+
|
49
|
-
Connection con = null;
|
127
|
+
Connection con = null;
|
50
|
-
|
128
|
+
|
51
|
-
String url = "
|
129
|
+
String url = "jdbc:mysql://192.168.1.2/komori";
|
52
|
-
|
130
|
+
|
53
|
-
String user = "
|
131
|
+
String user = "contentsservice";
|
54
|
-
|
132
|
+
|
55
|
-
String password = "
|
133
|
+
String password = "contentsservice";
|
56
|
-
|
57
|
-
|
58
|
-
|
134
|
+
|
135
|
+
|
136
|
+
|
59
|
-
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
137
|
+
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
60
|
-
|
138
|
+
|
61
|
-
con = DriverManager.getConnection(url, user, password);
|
139
|
+
con = DriverManager.getConnection(url, user, password);
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
this.ps_ = this.con_.prepareStatement("update Prodcutlist set name=?,price=? Where id = ?");
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
this.ps_.setString(1,"id");
|
148
|
+
|
149
|
+
this.ps_.executeUpdate();
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
} catch (SQLException e) {
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
e.printStackTrace();
|
158
|
+
|
159
|
+
} catch (InstantiationException e) {
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
e.printStackTrace();
|
164
|
+
|
165
|
+
} catch (IllegalAccessException e) {
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
e.printStackTrace();
|
170
|
+
|
171
|
+
} catch (ClassNotFoundException e) {
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
e.printStackTrace();
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
public void close() {
|
184
|
+
|
185
|
+
try {
|
186
|
+
|
187
|
+
// データベースとの接続を解除する
|
188
|
+
|
189
|
+
if (this.con_ != null) {
|
190
|
+
|
191
|
+
this.con_.close();
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
if (this.ps_ != null) {
|
196
|
+
|
197
|
+
this.ps_.close();
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
if (this.rs_ != null) {
|
202
|
+
|
203
|
+
this.rs_.close();
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
} catch (SQLException se) {
|
208
|
+
|
209
|
+
// データベースとの接続解除に失敗した場合
|
210
|
+
|
211
|
+
se.printStackTrace();
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|