質問編集履歴
2
実行手順の追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
データベースにinsertできない。
|
test
CHANGED
@@ -1,303 +1,343 @@
|
|
1
|
-
以下のtestクラスを実行するとデータベースにデータの格納ができるのですが、
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
i
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
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
|
-
|
130
|
-
|
131
|
-
/
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
i
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
i
|
148
|
-
|
149
|
-
i
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
/
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
re
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
Str
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
o
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
/
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
i
|
228
|
-
|
229
|
-
im
|
230
|
-
|
231
|
-
i
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
1
|
+
以下のtestクラスを実行するとデータベースにデータの格納ができるのですが、
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
servletクラスでgetParameterを用い、jspページに入力された値を受け取り、
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
Daoクラスに用意したsql文実行用のメソッド(testクラスと同じ内容の、
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
データをinsertするsql文)を用いてデータベースに値を格納しようとすると
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
メソッドの戻り値が『ClassNotFoundException』になってしまいデータの格納ができません。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
自分では解決できないのでご協力お願いいたします。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
<<実行手順>>
|
26
|
+
|
27
|
+
動的WEBプロジェクト<プロパティー<javaのビルド・パス<ライブラリー<外部jarの追加でjdbcドライバーmysql-connecter-java-5.1.40-bin.jarを追加しています。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
==========================testクラス=========================
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
package drinkMachine;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
import java.sql.*;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
public class test {
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
public static void main(String[] args) {
|
48
|
+
|
49
|
+
// TODO 自動生成されたメソッド・スタブ
|
50
|
+
|
51
|
+
try{
|
52
|
+
|
53
|
+
Connection conn = null;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
Class.forName("com.mysql.jdbc.Driver");
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
String url = "jdbc:mysql://localhost/example1";
|
62
|
+
|
63
|
+
String host = "root";
|
64
|
+
|
65
|
+
String password = "******";
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
conn = DriverManager.getConnection(url,host,password);
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
String sql = "INSERT INTO drinkMachine(code,name,unitprice,count) VALUES(?,?,?,?)";
|
74
|
+
|
75
|
+
PreparedStatement pstmt = conn.prepareStatement(sql);
|
76
|
+
|
77
|
+
pstmt.setString(1,"3");
|
78
|
+
|
79
|
+
pstmt.setString(2,"オレンジジュース");
|
80
|
+
|
81
|
+
pstmt.setString(3,"2");
|
82
|
+
|
83
|
+
pstmt.setString(4,"2");
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
int num = pstmt.executeUpdate();
|
88
|
+
|
89
|
+
System.out.println(num);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
}catch(ClassNotFoundException e){
|
94
|
+
|
95
|
+
e.printStackTrace();
|
96
|
+
|
97
|
+
System.out.println("a");
|
98
|
+
|
99
|
+
}catch(SQLException e){
|
100
|
+
|
101
|
+
e.printStackTrace();
|
102
|
+
|
103
|
+
System.out.println("b");
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
==========================testクラス=========================
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
==========================jspページ==========================
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
128
|
+
|
129
|
+
pageEncoding="UTF-8"%>
|
130
|
+
|
131
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
132
|
+
|
133
|
+
<html>
|
134
|
+
|
135
|
+
<head>
|
136
|
+
|
137
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
138
|
+
|
139
|
+
<title>Insert title here</title>
|
140
|
+
|
141
|
+
</head>
|
142
|
+
|
143
|
+
<body>
|
144
|
+
|
145
|
+
<form action="/drinkMachine/addController" method="post">
|
146
|
+
|
147
|
+
商品コード<input type="text" name="code"><br>
|
148
|
+
|
149
|
+
商品名<input type="text" name="name"><br>
|
150
|
+
|
151
|
+
金額<input type="text" name="price"><br>
|
152
|
+
|
153
|
+
数量<input type="text" name="count"><br>
|
154
|
+
|
155
|
+
<input type="submit" value="送信">
|
156
|
+
|
157
|
+
</form>
|
158
|
+
|
159
|
+
</body>
|
160
|
+
|
161
|
+
</html>
|
162
|
+
|
163
|
+
==========================jspページ==========================
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
=========================servletクラス========================
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
package drinkMachine;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
import java.io.*;
|
178
|
+
|
179
|
+
import javax.servlet.ServletException;
|
180
|
+
|
181
|
+
import javax.servlet.http.HttpServlet;
|
182
|
+
|
183
|
+
import javax.servlet.http.HttpServletRequest;
|
184
|
+
|
185
|
+
import javax.servlet.http.HttpServletResponse;
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
/**
|
190
|
+
|
191
|
+
•Servlet implementation class addController
|
192
|
+
|
193
|
+
*/
|
194
|
+
|
195
|
+
public class addController extends HttpServlet {
|
196
|
+
|
197
|
+
private static final long serialVersionUID = 1L;
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
/**
|
202
|
+
|
203
|
+
•@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
204
|
+
|
205
|
+
*/
|
206
|
+
|
207
|
+
/**
|
208
|
+
|
209
|
+
•@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
210
|
+
|
211
|
+
*/
|
212
|
+
|
213
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
214
|
+
|
215
|
+
// TODO Auto-generated method stub
|
216
|
+
|
217
|
+
doGet(request, response);
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
PrintWriter out = response.getWriter();
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
request.setCharacterEncoding("UTF-8");
|
226
|
+
|
227
|
+
String code = request.getParameter("code");
|
228
|
+
|
229
|
+
String name =request.getParameter("name");
|
230
|
+
|
231
|
+
String price = request.getParameter("price");
|
232
|
+
|
233
|
+
String count = request.getParameter("count");
|
234
|
+
|
235
|
+
T001_ITEMDao ti = new T001_ITEMDao();
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
int str = ti.addItem(name);
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
out.print(str);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
=========================servletクラス========================
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
==========================Daoクラス===========================
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
package drinkMachine;
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
import java.sql.Connection;
|
262
|
+
|
263
|
+
import java.sql.DriverManager;
|
264
|
+
|
265
|
+
import java.sql.PreparedStatement;
|
266
|
+
|
267
|
+
import java.sql.SQLException;
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
public class T001_ITEMDao {
|
238
272
|
|
239
273
|
|
240
274
|
|
241
275
|
public int addItem(String name){
|
242
276
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
277
|
+
|
278
|
+
|
279
|
+
try{
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
Connection conn = null;
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
Class.forName("com.mysql.jdbc.Driver");
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
String url = "jdbc:mysql://localhost/example1";
|
292
|
+
|
293
|
+
String host = "root";
|
294
|
+
|
295
|
+
String password = "******";
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
conn = DriverManager.getConnection(url,host,password);
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
String sql = "INSERT INTO drinkMachine(code,name,unitprice,count) VALUES(?,?,?,?)";
|
304
|
+
|
305
|
+
PreparedStatement pstmt = conn.prepareStatement(sql);
|
306
|
+
|
307
|
+
pstmt.setString(1,"3");
|
308
|
+
|
309
|
+
pstmt.setString(2,"オレンジジュース");
|
310
|
+
|
311
|
+
pstmt.setString(3,"2");
|
312
|
+
|
313
|
+
pstmt.setString(4,"2");
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
int num = pstmt.executeUpdate();
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
return num;
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
}catch(ClassNotFoundException e){
|
326
|
+
|
327
|
+
e.printStackTrace();
|
328
|
+
|
329
|
+
return 2;
|
330
|
+
|
331
|
+
}catch(SQLException e){
|
332
|
+
|
333
|
+
e.printStackTrace();
|
334
|
+
|
335
|
+
return 3;
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
========================Daoクラス===========================
|
1
パスワードが見えていたので隠した
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
String host = "root";
|
40
40
|
|
41
|
-
String password = "
|
41
|
+
String password = "*******";
|
42
42
|
|
43
43
|
|
44
44
|
|
@@ -256,7 +256,7 @@
|
|
256
256
|
|
257
257
|
String host = "root";
|
258
258
|
|
259
|
-
String password = "
|
259
|
+
String password = "******";
|
260
260
|
|
261
261
|
|
262
262
|
|