質問編集履歴

2

書式の改善

2018/09/18 03:04

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,28 @@
4
4
 
5
5
  解決案をご教授いただけないでしょうか。
6
6
 
7
+ <試したこと>
8
+
9
+ ・SQLよりデータを取得できているか。別クラスを作成し、コンソール出力はできました。
10
+
11
+
12
+
13
+ ・tomcatのバージョンを変えてみましたが改善されませんでした。
14
+
15
+
16
+
17
+ ・サーブレットの以下内容をコメントアウトするとエラーはでません。
18
+
19
+   t_productDAO t_productBean = new t_productDAO();
20
+
21
+   List<t_productBean>productList = t_productBean.findAll();
22
+
23
+   request.setAttribute("productList", productList);
24
+
25
+ 以上です。
26
+
27
+
28
+
7
29
 
8
30
 
9
31
  エラー内容及びservlet、daoクラスのソースを記載しておきます。

1

書式の改善

2018/09/18 03:04

投稿

mutani
mutani

スコア20

test CHANGED
File without changes
test CHANGED
@@ -6,34 +6,30 @@
6
6
 
7
7
 
8
8
 
9
+ エラー内容及びservlet、daoクラスのソースを記載しておきます。
10
+
11
+
12
+
13
+ よろしくお願いいたします。
14
+
15
+ **エラー内容**
16
+
17
+ ```
18
+
9
19
  HTTPステータス 500 - Internal Server Error
10
20
 
11
-
12
-
13
-
14
-
15
21
  Type Exception Report
16
22
 
17
-
18
-
19
23
  メッセージ サーブレットの実行により例外を投げました
20
24
 
21
-
22
-
23
25
  説明 The server encountered an unexpected condition that prevented it from fulfilling the request.
24
26
 
25
-
26
-
27
27
  例外
28
28
 
29
29
  javax.servlet.ServletException: サーブレットの実行により例外を投げました
30
30
 
31
31
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
32
32
 
33
-
34
-
35
-
36
-
37
33
  原因
38
34
 
39
35
  java.lang.NoClassDefFoundError: java/sql/Driver
@@ -70,18 +66,250 @@
70
66
 
71
67
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
72
68
 
73
-
74
-
75
-
76
-
77
69
  注意 原因のすべてのスタックトレースは、のログに記録されています
78
70
 
79
-
80
-
81
-
82
-
83
71
  Apache Tomcat/9.0.10
84
72
 
73
+ ```
74
+
75
+ **servlet**
76
+
77
+ ```
78
+
79
+ package servlet;
80
+
81
+
82
+
83
+ import java.io.IOException;
84
+
85
+ import java.util.List;
86
+
87
+
88
+
89
+ import javax.servlet.RequestDispatcher;
90
+
91
+ import javax.servlet.ServletException;
92
+
93
+ import javax.servlet.annotation.WebServlet;
94
+
95
+ import javax.servlet.http.HttpServlet;
96
+
97
+ import javax.servlet.http.HttpServletRequest;
98
+
99
+ import javax.servlet.http.HttpServletResponse;
100
+
101
+
102
+
103
+ import dao.t_productDAO;
104
+
105
+ import model.t_productBean;
106
+
107
+
108
+
109
+ /**
110
+
111
+ * Servlet implementation class ProductList
112
+
113
+ */
114
+
115
+ @WebServlet("/ProductList")
116
+
117
+ public class ProductList extends HttpServlet {
118
+
119
+ private static final long serialVersionUID = 1L;
120
+
121
+
122
+
123
+ public ProductList() {
124
+
125
+ super();
126
+
127
+ }
128
+
129
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
130
+
131
+ response.setCharacterEncoding("UTF-8");
132
+
133
+
134
+
135
+ t_productDAO t_productBean = new t_productDAO();
136
+
137
+ List<t_productBean>productList = t_productBean.findAll();
138
+
139
+ request.setAttribute("productList", productList);
140
+
141
+
142
+
143
+ RequestDispatcher rd = request.getRequestDispatcher("/ProductList.jsp" );
144
+
145
+ rd.forward(request,response);
146
+
147
+ }
148
+
149
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
150
+
151
+ doGet(request, response);
152
+
153
+ }
154
+
155
+ }
156
+
157
+ ```
158
+
159
+ **DAO**
160
+
161
+ ```
162
+
163
+ package dao;
164
+
165
+
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
+
182
+
183
+ import model.t_productBean;
184
+
185
+
186
+
187
+ public class t_productDAO {
188
+
189
+ //ここからメンバ変数定義
190
+
191
+ private final String driver = "com.mysql.jdbc.Driver";
192
+
193
+ private final String url = "jdbc:mysql://localhost/wbr_inventory_control?characterEncoding=UTF-8&serverTimezone=JST";
194
+
195
+ private final String user = "testuser";
196
+
197
+ private final String password = "pass";
198
+
199
+
200
+
201
+ public List<t_productBean> findAll() {
202
+
203
+ Connection conn = null;
204
+
205
+ List<t_productBean>productList = new ArrayList<t_productBean>();//リスト名(リスト作成
206
+
207
+
208
+
209
+ try {
210
+
211
+ //JDBCドライバを読み込み
212
+
213
+ Class.forName(driver);
214
+
215
+
216
+
217
+ //データベースへの接続
218
+
219
+ conn = DriverManager.getConnection(url,user,password);
220
+
221
+
222
+
223
+ //SELECT文を準備
224
+
225
+ String sql = "SELECT * FROM t_product";
226
+
227
+ PreparedStatement pstmt = conn.prepareStatement(sql);
228
+
229
+
230
+
231
+ //SElECT文を実行し、結果を取得
232
+
233
+ ResultSet rs = pstmt.executeQuery();
234
+
235
+
236
+
237
+ while(rs.next()) { //select文の結果をArrayListに格納
238
+
239
+ int product_id = rs.getInt("product_id");
240
+
241
+ String product_genre_code = rs.getString("product_genre_code");
242
+
243
+ String product_name = rs.getString("product_name");
244
+
245
+ String product_maker = rs.getString("product_maker");
246
+
247
+ int product_price = rs.getInt("product_price");
248
+
249
+ int product_stock = rs.getInt("product_stock");
250
+
251
+ int product_sales = rs.getInt("product_sales");
252
+
253
+ String product_remarks = rs.getString("product_remarks");
254
+
255
+
256
+
257
+ t_productBean T_product = new t_productBean(product_id,product_genre_code,product_name
258
+
259
+ ,product_maker,product_price,product_stock,product_sales,product_remarks);
260
+
261
+ productList.add(T_product);
262
+
263
+
264
+
265
+ }
266
+
267
+
268
+
269
+ }catch(SQLException e) { //例外SQL例外あり
270
+
271
+ e.printStackTrace();
272
+
273
+ return null;
274
+
275
+ } catch (ClassNotFoundException e) { //ノットファウンドあり
276
+
277
+ e.printStackTrace();
278
+
279
+ return null;
280
+
281
+ }finally { //catchがなくても必ず実行
282
+
283
+ //データベース切断
284
+
285
+ if(conn != null) {
286
+
287
+ try {
288
+
289
+ conn.close();
290
+
291
+ }catch(SQLException e) {
292
+
293
+ e.printStackTrace();
294
+
295
+ return null;
296
+
297
+ }
298
+
299
+ }
300
+
301
+ }
302
+
303
+ return productList;
304
+
305
+ }
306
+
307
+ }
308
+
309
+ ```
310
+
311
+
312
+
85
313
 
86
314
 
87
315
  何卒よろしくお願いいたします。