質問編集履歴

1

DB接続共通処理のクラスを作成し、ご指摘頂いたforName()を使いJDBCドライバをつなげました。

2020/06/16 12:40

投稿

yyuukkii
yyuukkii

スコア7

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,24 @@
24
24
 
25
25
 
26
26
 
27
+
28
+
29
+
30
+
31
+ forName()をConnectionUtilクラスに追加したところ別のエラーが発生しました。
32
+
33
+
34
+
35
+ java.lang.IllegalStateException: レスポンスをコミットした後でフォワードできません
36
+
37
+ servlet.LoginServlet.doPost(LoginServlet.java:55)
38
+
39
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
40
+
41
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
42
+
43
+ org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
44
+
27
45
 
28
46
 
29
47
  試したこと
@@ -32,11 +50,13 @@
32
50
 
33
51
  - jarファイルをWEB-INF/libフォルダに入れると解決するという記事を見て試しましたが解決されませんでした。
34
52
 
35
-
53
+ - ConnectionUtilというクラスに接続共通処理をつくりそこにForName()を入れました。
54
+
36
-
55
+ -デバッガで確認しLoginInfoDaoのResultSet rsの変数の値がnullになってしまいます。
37
-
38
-
56
+
57
+
58
+
39
- Daoクラス
59
+ ConnectionUtil DB接続共通処理
40
60
 
41
61
 
42
62
 
@@ -46,10 +66,100 @@
46
66
 
47
67
 
48
68
 
69
+ import java.lang.reflect.InvocationTargetException;
70
+
49
71
  import java.sql.Connection;
50
72
 
51
73
  import java.sql.DriverManager;
52
74
 
75
+ import java.sql.SQLException;
76
+
77
+
78
+
79
+ public class ConnectionUtil {
80
+
81
+
82
+
83
+ //JDBCドライバーと接続先データベース名を代入
84
+
85
+ private static String driverName = "com.mysql.cj.jdbc.Driver";
86
+
87
+ private static String url = "jdbc:mysql://localhost:3306/company?characterEncoding=UTF8&serverTimezone=JST";
88
+
89
+
90
+
91
+ //userアカウントを代入
92
+
93
+ private static final String user = "root";
94
+
95
+ private static final String pass = "Sy";
96
+
97
+
98
+
99
+
100
+
101
+ //DB接続のメソッド
102
+
103
+ public Connection getConnection() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
104
+
105
+
106
+
107
+ Connection conn = null;
108
+
109
+
110
+
111
+ try {
112
+
113
+
114
+
115
+
116
+
117
+ Class.forName(driverName).getDeclaredConstructor().newInstance(); //newInstance()の書き方が正しいのか不安なところです。
118
+
119
+
120
+
121
+ } catch (ClassNotFoundException e1) {
122
+
123
+ e1.printStackTrace();
124
+
125
+ }
126
+
127
+
128
+
129
+ try {
130
+
131
+ conn = DriverManager.getConnection(url, user, pass);
132
+
133
+
134
+
135
+ } catch (SQLException e) {
136
+
137
+ e.printStackTrace();
138
+
139
+ }
140
+
141
+ return conn;
142
+
143
+ }
144
+
145
+ }
146
+
147
+ ```
148
+
149
+
150
+
151
+ LoginInfoDao
152
+
153
+ ```
154
+
155
+ package dao;
156
+
157
+
158
+
159
+ import java.lang.reflect.InvocationTargetException;
160
+
161
+ import java.sql.Connection;
162
+
53
163
  import java.sql.PreparedStatement;
54
164
 
55
165
  import java.sql.ResultSet;
@@ -62,216 +172,236 @@
62
172
 
63
173
 
64
174
 
65
-
66
-
67
175
  public class LoginInfoDao {
68
176
 
69
177
 
70
178
 
71
- private static final String URL = "jdbc:mysql://localhost:3306/company?characterEncoding=UTF-8&serverTimezone=JST";
72
-
73
- private static final String USER = "root";
74
-
75
- private static final String PASS = "Sy";
76
-
77
-
78
-
79
- public LoginModel findAccount(LoginModel lm) {
80
-
81
-
82
-
83
- LoginModel returnLm = new LoginModel();
84
-
85
-
86
-
87
- try (Connection conn = DriverManager.getConnection(URL, USER, PASS)) {
88
-
89
-
90
-
91
- PreparedStatement ps;
92
-
93
-
94
-
95
- ps = conn.prepareStatement("select * from login_info where = login_id = ? and password = ?");
96
-
97
-
98
-
99
- ps.setString(1, lm.getLogin_id());
100
-
101
- ps.setString(2, lm.getPassword());
102
-
103
-
104
-
105
- ResultSet rs = ps.executeQuery();
106
-
107
-
108
-
109
- if (rs.next()) {
110
-
111
- returnLm.setLogin_id(rs.getString("login_id"));
112
-
113
- returnLm.setPassword(rs.getString("password"));
114
-
115
-
116
-
117
- } else {
118
-
119
- return null;
179
+ public LoginModel findAccount (String login_id, String password) throws SQLException {
180
+
181
+ LoginModel model = new LoginModel();
182
+
183
+ ConnectionUtil db = new ConnectionUtil();
184
+
185
+ Connection conn = null;
186
+
187
+ try {
188
+
189
+ conn = db.getConnection();
190
+
191
+ } catch (InstantiationException e1) {
192
+
193
+ e1.printStackTrace();
194
+
195
+ } catch (IllegalAccessException e1) {
196
+
197
+ e1.printStackTrace();
198
+
199
+ } catch (IllegalArgumentException e1) {
200
+
201
+ e1.printStackTrace();
202
+
203
+ } catch (InvocationTargetException e1) {
204
+
205
+ e1.printStackTrace();
206
+
207
+ } catch (NoSuchMethodException e1) {
208
+
209
+ e1.printStackTrace();
210
+
211
+ } catch (SecurityException e1) {
212
+
213
+ e1.printStackTrace();
214
+
215
+ }
216
+
217
+
218
+
219
+ String sql = "select * from login_info where login_id = ? and password = ?";
220
+
221
+
222
+
223
+ try {
224
+
225
+ PreparedStatement ps = conn.prepareStatement(sql);
226
+
227
+ ps.setString(1, login_id);
228
+
229
+ ps.setString(2, password);
230
+
231
+ ResultSet rs = ps.executeQuery();
232
+
233
+
234
+
235
+ while (rs.next()) {
236
+
237
+ model.setLogin_id(rs.getString("login_id"));
238
+
239
+ model.setPassword(rs.getString("password"));
240
+
241
+ }
242
+
243
+ } catch (SQLException e) {
244
+
245
+ e.printStackTrace();
246
+
247
+
248
+
249
+ } catch (Exception e) {
250
+
251
+ e.printStackTrace();
252
+
253
+
254
+
255
+ } finally {
256
+
257
+ conn.close();
258
+
259
+ }
260
+
261
+ return model;
262
+
263
+ }
264
+
265
+
266
+
267
+ ```
268
+
269
+
270
+
271
+
272
+
273
+ LoginServlet
274
+
275
+
276
+
277
+ ```
278
+
279
+ package servlet;
280
+
281
+
282
+
283
+ import java.io.IOException;
284
+
285
+ import java.sql.SQLException;
286
+
287
+
288
+
289
+ import javax.servlet.RequestDispatcher;
290
+
291
+ import javax.servlet.ServletException;
292
+
293
+ import javax.servlet.annotation.WebServlet;
294
+
295
+ import javax.servlet.http.HttpServlet;
296
+
297
+ import javax.servlet.http.HttpServletRequest;
298
+
299
+ import javax.servlet.http.HttpServletResponse;
300
+
301
+
302
+
303
+ import dao.LoginInfoDao;
304
+
305
+ import model.LoginModel;
306
+
307
+
308
+
309
+ /**
310
+
311
+ * Servlet implementation class LoginServlet
312
+
313
+ */
314
+
315
+ @WebServlet("/Login")
316
+
317
+ public class LoginServlet extends HttpServlet {
318
+
319
+ private static final long serialVersionUID = 1L;
320
+
321
+
322
+
323
+ public LoginServlet() {
324
+
325
+ super();
326
+
327
+
328
+
329
+ }
330
+
331
+
332
+
333
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
334
+
335
+ throws ServletException, IOException {
336
+
337
+ RequestDispatcher dispacher = request.getRequestDispatcher("/WEB-INF/jsp/Login.jsp");
338
+
339
+ dispacher.forward(request, response);
340
+
341
+
342
+
343
+ }
344
+
345
+
346
+
347
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
348
+
349
+ throws ServletException, IOException {
350
+
351
+
352
+
353
+ //login-id,password取得
354
+
355
+ String login_id = request.getParameter("login-id");
356
+
357
+ String password = request.getParameter("login-password");
358
+
359
+
360
+
361
+ LoginInfoDao dao = new LoginInfoDao();
362
+
363
+ LoginModel model = new LoginModel();
364
+
365
+
366
+
367
+ try {
368
+
369
+ model = dao.findAccount(login_id, password);
370
+
371
+
372
+
373
+ //ユーザーの有無検索
374
+
375
+ if (model != null) {
376
+
377
+
378
+
379
+ response.sendRedirect("/Manager/List");
380
+
381
+
120
382
 
121
383
  }
122
384
 
385
+ request.setAttribute("error", "入力内容が間違っています。");
386
+
387
+ request.getRequestDispatcher("/WEB-INF/jsp/Login.jsp").forward(request, response);
388
+
389
+
390
+
123
391
  } catch (SQLException e) {
124
392
 
125
393
  e.printStackTrace();
126
394
 
127
- return null;
128
-
129
- } finally {
130
-
131
-
132
-
133
395
  }
134
396
 
135
- return returnLm;
397
+
136
398
 
137
399
  }
138
400
 
401
+
402
+
139
403
  }
140
404
 
141
-
142
-
143
- ```
144
-
145
-
146
-
147
- Servlet クラス
148
-
149
-
150
-
151
- ```
152
-
153
- package servlet;
154
-
155
-
156
-
157
- import java.io.IOException;
158
-
159
-
160
-
161
- import javax.servlet.RequestDispatcher;
162
-
163
- import javax.servlet.ServletException;
164
-
165
- import javax.servlet.annotation.WebServlet;
166
-
167
- import javax.servlet.http.HttpServlet;
168
-
169
- import javax.servlet.http.HttpServletRequest;
170
-
171
- import javax.servlet.http.HttpServletResponse;
172
-
173
- import javax.servlet.http.HttpSession;
174
-
175
-
176
-
177
- import dao.LoginInfoDao;
178
-
179
- import model.LoginModel;
180
-
181
-
182
-
183
- /**
184
-
185
- * Servlet implementation class LoginServlet
186
-
187
- */
188
-
189
- @WebServlet("/Login")
190
-
191
- public class LoginServlet extends HttpServlet {
192
-
193
- private static final long serialVersionUID = 1L;
194
-
195
-
196
-
197
-
198
-
199
- public LoginServlet() {
200
-
201
- super();
202
-
203
-
204
-
205
- }
206
-
207
-
208
-
209
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
210
-
211
- RequestDispatcher dispacher = request.getRequestDispatcher("/WEB-INF/jsp/Login.jsp");
212
-
213
- dispacher.forward(request, response);
214
-
215
-
216
-
217
- }
218
-
219
-
220
-
221
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
222
-
223
-
224
-
225
- //login-id,password取得
226
-
227
- String login_id = request.getParameter("login-id");
228
-
229
- String password = request.getParameter("login-password");
230
-
231
-
232
-
233
- LoginModel lm = new LoginModel();
234
-
235
- lm.setLogin_id(login_id);
236
-
237
- lm.setPassword(password);
238
-
239
-
240
-
241
- //dao情報取得
242
-
243
- LoginInfoDao dao = new LoginInfoDao();
244
-
245
- LoginModel returnLm = dao.findAccount(lm);
246
-
247
-
248
-
249
-
250
-
251
- //ユーザーの有無検索
252
-
253
- if (returnLm != null ) {
254
-
255
- HttpSession session = request.getSession();
256
-
257
- response.sendRedirect("/Manager/List");
258
-
259
-
260
-
261
- }
262
-
263
- request.setAttribute("error", "入力内容が間違っています。");
264
-
265
- request.getRequestDispatcher("/WEB-INF/jsp/Login.jsp").forward(request, response);
266
-
267
-
268
-
269
- }
270
-
271
-
272
-
273
- }
274
-
275
405
  ```
276
406
 
277
407