質問編集履歴

2

実行手順の追加

2017/01/22 04:47

投稿

yuta1985
yuta1985

スコア12

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

パスワードが見えていたので隠した

2017/01/22 04:47

投稿

yuta1985
yuta1985

スコア12

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  String host = "root";
40
40
 
41
- String password = "az033697";
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 = "az033697";
259
+ String password = "******";
260
260
 
261
261
 
262
262