質問編集履歴
4
追記情報
title
CHANGED
File without changes
|
body
CHANGED
@@ -376,4 +376,13 @@
|
|
376
376
|
|
377
377
|
}
|
378
378
|
|
379
|
-
```
|
379
|
+
```
|
380
|
+
追記4
|
381
|
+
|
382
|
+
Main.javaとTest.javaではDAOのfindAll()内の処理が異なるようです。
|
383
|
+
1 : Main.java、Test.javaはともにClass.forName( DRIVER_NAME );までは読み込む
|
384
|
+
2 : Main.javaはその後finally{}へ飛ぶ
|
385
|
+
3 : Test.javaは順調にcon = DriverManager.getConnection( JDBC_URL, DB_USER, DB_PASS );を読み込む
|
386
|
+
4 : 仮にClass.forName()でエラーを出していた場合、ClassNotFoundExceptionでエラーを捕捉していない
|
387
|
+
|
388
|
+
もはやソースコード上の問題なのか不明です。
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -273,4 +273,107 @@
|
|
273
273
|
```
|
274
274
|
|
275
275
|
|
276
|
-
こう見ると、そもそもサーブレットクラスのMain.javaで取得ができていないようですね、、。
|
276
|
+
こう見ると、そもそもサーブレットクラスのMain.javaで取得ができていないようですね、、。
|
277
|
+
|
278
|
+
---
|
279
|
+
追記3
|
280
|
+
|
281
|
+
DAOクラスのfindAll()までデバッグを行いました。
|
282
|
+
|
283
|
+
結果、Test.javaではfindAll()でDBからの情報を取得し、しっかりと返り値を返しています。しかし、Main.javaではfindAll()ではtry{}が全てスキップしており、finally{}のみ実行されていました。(コードは最後に書きます)
|
284
|
+
|
285
|
+
関係しているファイルは最初に記載した通りで、今回の流れではDBにSELECT文しか送っておらず、かなり簡単なもののはずですが、どこがおかしいのか、、、。
|
286
|
+
|
287
|
+
```java
|
288
|
+
package dao;
|
289
|
+
|
290
|
+
import java.sql.Connection;
|
291
|
+
import java.sql.DriverManager;
|
292
|
+
import java.sql.PreparedStatement;
|
293
|
+
import java.sql.ResultSet;
|
294
|
+
import java.sql.SQLException;
|
295
|
+
import java.util.ArrayList;
|
296
|
+
import java.util.List;
|
297
|
+
|
298
|
+
import model.Mutter;
|
299
|
+
|
300
|
+
public class MutterDAO{
|
301
|
+
private final String DRIVER_NAME = "com.mysql.jdbc.Driver";
|
302
|
+
private final String JDBC_URL = "jdbc:mysql://localhost:8889/docoTsubu";
|
303
|
+
private final String DB_USER = "○○○○○○○";
|
304
|
+
private final String DB_PASS = "○○○○○○○";
|
305
|
+
|
306
|
+
public List< Mutter > findAll(){
|
307
|
+
Connection con = null;
|
308
|
+
List< Mutter > mutterList = new ArrayList<>();
|
309
|
+
|
310
|
+
try {
|
311
|
+
Class.forName( DRIVER_NAME );
|
312
|
+
con = DriverManager.getConnection( JDBC_URL, DB_USER, DB_PASS );
|
313
|
+
|
314
|
+
// SQLの準備
|
315
|
+
String sql = "SELECT id, name, text FROM mutter ORDER BY id DESC";
|
316
|
+
PreparedStatement pstmt = con.prepareStatement( sql );
|
317
|
+
ResultSet rs = pstmt.executeQuery();
|
318
|
+
|
319
|
+
while( rs.next() ) {
|
320
|
+
int id = rs.getInt( "id" );
|
321
|
+
String userName = rs.getString( "name" );
|
322
|
+
String text = rs.getString( "text" );
|
323
|
+
Mutter mutter = new Mutter( id, userName, text );
|
324
|
+
mutterList.add( mutter );
|
325
|
+
}
|
326
|
+
|
327
|
+
} catch ( ClassNotFoundException e ) {
|
328
|
+
e.printStackTrace();
|
329
|
+
} catch ( SQLException e ) {
|
330
|
+
e.printStackTrace();
|
331
|
+
} finally {
|
332
|
+
if( con != null ) {
|
333
|
+
try {
|
334
|
+
con.close();
|
335
|
+
|
336
|
+
} catch ( SQLException e ) {
|
337
|
+
e.printStackTrace();
|
338
|
+
}
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
return mutterList;
|
343
|
+
}
|
344
|
+
|
345
|
+
public boolean create( Mutter mutter ){
|
346
|
+
Connection con = null;
|
347
|
+
try {
|
348
|
+
con = DriverManager.getConnection( JDBC_URL, DB_USER, DB_PASS );
|
349
|
+
|
350
|
+
String sql = "INSERT INTO mutter( name, text ) VALUES( ?, ? )";
|
351
|
+
PreparedStatement pstmt = con.prepareStatement( sql );
|
352
|
+
pstmt.setString( 1, mutter.getUserName() );
|
353
|
+
pstmt.setString( 2, mutter.getText() );
|
354
|
+
|
355
|
+
int result = pstmt.executeUpdate();
|
356
|
+
|
357
|
+
if( result != 1 ) {
|
358
|
+
return false;
|
359
|
+
}
|
360
|
+
|
361
|
+
} catch ( SQLException e ) {
|
362
|
+
e.printStackTrace();
|
363
|
+
} finally {
|
364
|
+
if( con != null ) {
|
365
|
+
try {
|
366
|
+
con.close();
|
367
|
+
|
368
|
+
} catch ( SQLException e ) {
|
369
|
+
e.printStackTrace();
|
370
|
+
}
|
371
|
+
}
|
372
|
+
}
|
373
|
+
|
374
|
+
return true;
|
375
|
+
}
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
```
|
2
デバッグ結果追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -212,4 +212,65 @@
|
|
212
212
|
}
|
213
213
|
|
214
214
|
コード
|
215
|
-
```
|
215
|
+
```
|
216
|
+
|
217
|
+
---
|
218
|
+
追記2
|
219
|
+
|
220
|
+
下記にデバッグ結果を書きます
|
221
|
+
|
222
|
+
```java
|
223
|
+
protected void doGet( HttpServletRequest request, HttpServletResponse response )
|
224
|
+
throws ServletException, IOException{
|
225
|
+
|
226
|
+
GetMutterListLogic gm = new GetMutterListLogic();
|
227
|
+
List< Mutter > mutterList = gm.execute();
|
228
|
+
request.setAttribute( "mutterList", mutterList );
|
229
|
+
// ↑ブレークポイント
|
230
|
+
}
|
231
|
+
/*
|
232
|
+
* mutterList : ArrayList<E>
|
233
|
+
* elementData : Object[0]
|
234
|
+
* modCount : 0
|
235
|
+
* size : 0
|
236
|
+
*
|
237
|
+
*/
|
238
|
+
|
239
|
+
```
|
240
|
+
|
241
|
+
```java
|
242
|
+
package test;
|
243
|
+
|
244
|
+
import java.util.List;
|
245
|
+
|
246
|
+
import model.GetMutterListLogic;
|
247
|
+
import model.Mutter;
|
248
|
+
|
249
|
+
public class Test{
|
250
|
+
|
251
|
+
public static void main( String[] args ){
|
252
|
+
GetMutterListLogic gm = new GetMutterListLogic();
|
253
|
+
List< Mutter > mutterLista = gm.execute();
|
254
|
+
|
255
|
+
for( Mutter mutter : mutterLista ) {
|
256
|
+
// ↑ブレークポイント
|
257
|
+
System.out.print( mutter.getUserName() + ":" );
|
258
|
+
System.out.println( mutter.getText() );
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
/*
|
266
|
+
*
|
267
|
+
* mutterList
|
268
|
+
* elementData : Object[10]
|
269
|
+
* modCount : 2
|
270
|
+
* size : 2
|
271
|
+
*
|
272
|
+
*/
|
273
|
+
```
|
274
|
+
|
275
|
+
|
276
|
+
こう見ると、そもそもサーブレットクラスのMain.javaで取得ができていないようですね、、。
|
1
追加情報
title
CHANGED
File without changes
|
body
CHANGED
@@ -135,4 +135,81 @@
|
|
135
135
|
|
136
136
|
******/
|
137
137
|
|
138
|
+
```
|
139
|
+
|
140
|
+
---
|
141
|
+
追記情報です。
|
142
|
+
```java
|
143
|
+
package servlet;
|
144
|
+
|
145
|
+
import java.io.IOException;
|
146
|
+
import java.util.List;
|
147
|
+
|
148
|
+
import javax.servlet.RequestDispatcher;
|
149
|
+
import javax.servlet.ServletException;
|
150
|
+
import javax.servlet.annotation.WebServlet;
|
151
|
+
import javax.servlet.http.HttpServlet;
|
152
|
+
import javax.servlet.http.HttpServletRequest;
|
153
|
+
import javax.servlet.http.HttpServletResponse;
|
154
|
+
import javax.servlet.http.HttpSession;
|
155
|
+
|
156
|
+
import model.GetMutterListLogic;
|
157
|
+
import model.Mutter;
|
158
|
+
import model.PostMutterLogic;
|
159
|
+
import model.User;
|
160
|
+
|
161
|
+
@WebServlet( "/Main" )
|
162
|
+
public class Main extends HttpServlet{
|
163
|
+
private static final long serialVersionUID = 1L;
|
164
|
+
|
165
|
+
protected void doGet( HttpServletRequest request, HttpServletResponse response )
|
166
|
+
throws ServletException, IOException{
|
167
|
+
|
168
|
+
// DBからつぶやきリストを取得→リクエストスコープに保存
|
169
|
+
GetMutterListLogic gm = new GetMutterListLogic();
|
170
|
+
List< Mutter > mutterList = gm.execute();
|
171
|
+
request.setAttribute( "mutterList", mutterList );
|
172
|
+
|
173
|
+
// ここでセッションIDをキーにインスタンスを取得
|
174
|
+
HttpSession session = request.getSession();
|
175
|
+
User loginUser = ( User ) session.getAttribute( "loginUser" );
|
176
|
+
|
177
|
+
if( loginUser == null ) {
|
178
|
+
response.sendRedirect( "/docoTsubu/" );
|
179
|
+
} else {
|
180
|
+
RequestDispatcher dispatcher = request.getRequestDispatcher( "/WEB-INF/jsp/main.jsp" );
|
181
|
+
dispatcher.forward( request, response );
|
182
|
+
}
|
183
|
+
|
184
|
+
}
|
185
|
+
|
186
|
+
protected void doPost( HttpServletRequest request, HttpServletResponse response )
|
187
|
+
throws ServletException, IOException{
|
188
|
+
|
189
|
+
String text = request.getParameter( "text" );
|
190
|
+
|
191
|
+
if( text != null && text.length() != 0 ) {
|
192
|
+
HttpSession session = request.getSession();
|
193
|
+
User loginUser = ( User ) request.getAttribute( "loginUser" );
|
194
|
+
|
195
|
+
Mutter mutter = new Mutter( loginUser.getName(), text );
|
196
|
+
PostMutterLogic pm = new PostMutterLogic();
|
197
|
+
pm.execute( mutter );
|
198
|
+
|
199
|
+
} else {
|
200
|
+
request.setAttribute( "errorMsg", "つぶやきが入力されていません" );
|
201
|
+
}
|
202
|
+
|
203
|
+
GetMutterListLogic gm = new GetMutterListLogic();
|
204
|
+
List< Mutter > mutterList = gm.execute();
|
205
|
+
request.setAttribute( "mutterList", mutterList );
|
206
|
+
|
207
|
+
RequestDispatcher dispatcher = request.getRequestDispatcher( "/WEB-INF/jsp/main.jsp" );
|
208
|
+
dispatcher.forward( request, response );
|
209
|
+
|
210
|
+
}
|
211
|
+
|
212
|
+
}
|
213
|
+
|
214
|
+
コード
|
138
215
|
```
|