質問編集履歴

6

変更

2016/04/13 10:19

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -270,6 +270,10 @@
270
270
 
271
271
  ```
272
272
 
273
+
274
+
275
+ MutterDAOです
276
+
273
277
  ```java
274
278
 
275
279
  package dao;
@@ -304,7 +308,7 @@
304
308
 
305
309
  private final String DRIVER_NAME="org.h2.Driver";
306
310
 
307
- private final String JDBC_URL="jdbc:h2:file:/Users/endoutaichi/Documents/networkspace/docoTsubuu/h2data.dat";
311
+ private final String JDBC_URL="jdbc:h2:file:/User/taichi/Documents/networkspace/docoTsubuu/h2data.dat";
308
312
 
309
313
  private final String DB_USER="sa";
310
314
 

5

コードの追加

2016/04/13 10:19

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -269,3 +269,203 @@
269
269
 
270
270
 
271
271
  ```
272
+
273
+ ```java
274
+
275
+ package dao;
276
+
277
+
278
+
279
+ import java.sql.Connection;
280
+
281
+ import java.sql.DriverManager;
282
+
283
+ import java.sql.PreparedStatement;
284
+
285
+ import java.sql.ResultSet;
286
+
287
+ import java.sql.SQLException;
288
+
289
+ import java.util.ArrayList;
290
+
291
+ import java.util.List;
292
+
293
+
294
+
295
+ import model.Mutter;
296
+
297
+
298
+
299
+
300
+
301
+ public class MutterDAO {
302
+
303
+
304
+
305
+ private final String DRIVER_NAME="org.h2.Driver";
306
+
307
+ private final String JDBC_URL="jdbc:h2:file:/Users/endoutaichi/Documents/networkspace/docoTsubuu/h2data.dat";
308
+
309
+ private final String DB_USER="sa";
310
+
311
+ private final String DB_PASS="";
312
+
313
+
314
+
315
+ public List<Mutter> findAll(){
316
+
317
+ Connection conn=null;
318
+
319
+ List<Mutter>mutterList=new ArrayList<Mutter>();
320
+
321
+ try{
322
+
323
+ Class.forName(DRIVER_NAME);
324
+
325
+ conn=DriverManager.getConnection(JDBC_URL,DB_USER,DB_PASS);
326
+
327
+
328
+
329
+ //SELECT文の準備
330
+
331
+ String sql="SELECT ID,NAME,TEXT FROM MUTTER ORDER BY ID DESC";
332
+
333
+ PreparedStatement pStmt=conn.prepareStatement(sql);
334
+
335
+
336
+
337
+ //SELECTを実行
338
+
339
+ ResultSet rs=pStmt.executeQuery();
340
+
341
+
342
+
343
+ //SELECT文の結果をArrayList二格納
344
+
345
+ while(rs.next()){
346
+
347
+ int id=rs.getInt("ID");
348
+
349
+ String userName=rs.getString("NAME");
350
+
351
+ String text=rs.getString("NAME");
352
+
353
+ Mutter mutter=new Mutter(id,userName,text);
354
+
355
+ mutterList.add(mutter);
356
+
357
+ }
358
+
359
+ }catch(SQLException e){
360
+
361
+ e.printStackTrace();
362
+
363
+ return null;
364
+
365
+ }catch(ClassNotFoundException e){
366
+
367
+ e.printStackTrace();
368
+
369
+ return null;
370
+
371
+ }finally{
372
+
373
+ //データベース切断
374
+
375
+ if(conn!=null){
376
+
377
+ try{
378
+
379
+ conn.close();
380
+
381
+ }catch(SQLException e){
382
+
383
+ e.printStackTrace();
384
+
385
+ return null;
386
+
387
+ }
388
+
389
+ }
390
+
391
+ }
392
+
393
+ return mutterList;
394
+
395
+ }
396
+
397
+ public boolean create(Mutter mutter){
398
+
399
+ Connection conn=null;
400
+
401
+ try{
402
+
403
+ //データベースへ接続
404
+
405
+ conn=DriverManager.getConnection(JDBC_URL,DB_USER,DB_PASS);
406
+
407
+
408
+
409
+ //INSERT文の準備(idは自動連番なので指定しなくてもいい)
410
+
411
+ String sql="INSERT INTO MUTTER(NAME,TEXT)VALUES(?,?)";
412
+
413
+ PreparedStatement pStmt=conn.prepareStatement(sql);
414
+
415
+
416
+
417
+ //INSERT文中の「?」二使用する値を設定しSQLを完成
418
+
419
+ pStmt.setString(1, mutter.getUserName());
420
+
421
+ pStmt.setString(2, mutter.getText());
422
+
423
+
424
+
425
+ //INSERT文を実行
426
+
427
+ int result=pStmt.executeUpdate();
428
+
429
+
430
+
431
+ if(result !=1){
432
+
433
+ return false;
434
+
435
+ }
436
+
437
+ }catch(SQLException e){
438
+
439
+ e.printStackTrace();
440
+
441
+ return false;
442
+
443
+ }finally{
444
+
445
+ //データベース切断
446
+
447
+ if(conn!=null){
448
+
449
+ try{
450
+
451
+ conn.close();
452
+
453
+ }catch(SQLException e){
454
+
455
+ e.printStackTrace();
456
+
457
+ }
458
+
459
+ }
460
+
461
+ }
462
+
463
+ return true;
464
+
465
+ }
466
+
467
+ }
468
+
469
+
470
+
471
+ ```

4

文の訂正

2016/04/13 10:18

投稿

edoooooo
edoooooo

スコア476

test CHANGED
@@ -1 +1 @@
1
- この画像のコメント入力欄の下に,コメントした履歴を出力できません、JSTLのcoreタグで書いているはずなのに、なぜ出力されないのでしょうか?
1
+ この画像のコメント入力欄の下に,コメントした履歴を出力できません、JSTLのcoreタグで書いているはずなのに、なぜ出力されないのでしょうか?Main.javaのコードを追加しました。
test CHANGED
File without changes

3

コードの追加

2016/04/13 08:57

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,204 @@
6
6
 
7
7
 
8
8
 
9
+ Main.java(servlet)のコードです。追加しました。どうぞよろしくお願いします。
10
+
11
+
12
+
13
+ ```java
14
+
15
+ package servlet;
16
+
17
+
18
+
19
+
20
+
21
+ import java.util.List;
22
+
23
+ import javax.servlet.RequestDispatcher;
24
+
25
+
26
+
27
+ import java.io.IOException;
28
+
29
+
30
+
31
+
32
+
33
+ import javax.servlet.ServletException;
34
+
35
+ import javax.servlet.annotation.WebServlet;
36
+
37
+ import javax.servlet.http.HttpServlet;
38
+
39
+ import javax.servlet.http.HttpServletRequest;
40
+
41
+ import javax.servlet.http.HttpServletResponse;
42
+
43
+ import javax.servlet.http.HttpSession;
44
+
45
+
46
+
47
+
48
+
49
+ import model.GetMutterListLogic;
50
+
51
+ import model.Mutter;
52
+
53
+ import model.User;
54
+
55
+ import model.PostMutterLogic;
56
+
57
+
58
+
59
+ /**
60
+
61
+ * Servlet implementation class Main
62
+
63
+ */
64
+
65
+ @WebServlet("/Main")
66
+
67
+ public class Main extends HttpServlet {
68
+
69
+
70
+
71
+ private static final long serialVersionUID = 1L;
72
+
73
+
74
+
75
+ /**
76
+
77
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
78
+
79
+ */
80
+
81
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
82
+
83
+
84
+
85
+ //つぶやきリストを取得して、リクエストスコープに保存
86
+
87
+ GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
88
+
89
+ List<Mutter>mutterList=getMutterListLogic.execute();
90
+
91
+ request.setAttribute("mutterList", mutterList);
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+ //ログインしているか確認するため
100
+
101
+ //セッションスコープからユーザー情報を取得
102
+
103
+ HttpSession session=request.getSession();
104
+
105
+ User loginUser=(User)session.getAttribute("loginUser");
106
+
107
+
108
+
109
+ if(loginUser==null){//ログインしていない場合
110
+
111
+ //リダイレクト
112
+
113
+ response.sendRedirect("/docoTsubuu/");
114
+
115
+ }else{//ログイン済みの場合
116
+
117
+ //フォワード
118
+
119
+ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
120
+
121
+ dispatcher.forward(request, response);
122
+
123
+ }
124
+
125
+
126
+
127
+ }
128
+
129
+ protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
130
+
131
+ //リクエストパラメータの取得
132
+
133
+ request.setCharacterEncoding("UTF-8");
134
+
135
+ String text=request.getParameter("text");
136
+
137
+
138
+
139
+ //入力値チェック
140
+
141
+ if(text!=null&&text.length()!=0){
142
+
143
+
144
+
145
+ //セッションスコープに保存されたユーザー情報を取得
146
+
147
+ HttpSession session=request.getSession();
148
+
149
+ User loginUser=(User)session.getAttribute("loginUser");
150
+
151
+
152
+
153
+ //つぶやきをつぶやきリストに追加
154
+
155
+ Mutter mutter=new Mutter(loginUser.getName(),text);
156
+
157
+ PostMutterLogic postMutterLogic=new PostMutterLogic();
158
+
159
+ postMutterLogic.execute(mutter);
160
+
161
+
162
+
163
+ //つぶやきリストを習得して、リクエストスコープに保存
164
+
165
+ GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
166
+
167
+ List<Mutter> mutterList=getMutterListLogic.execute();
168
+
169
+ request.setAttribute("mutterList", mutterList);
170
+
171
+
172
+
173
+
174
+
175
+ }else{
176
+
177
+ //エラーメッセージをリクエストスコープに保存
178
+
179
+
180
+
181
+ request.setAttribute("errorMsg", "つぶやきが入力されていません");
182
+
183
+ }
184
+
185
+ //メイン画面にフォワード
186
+
187
+ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
188
+
189
+ dispatcher.forward(request, response);
190
+
191
+
192
+
193
+ // TODO Auto-generated method stub
194
+
195
+ response.getWriter().append("Served at: ").append(request.getContextPath());
196
+
197
+ }
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ ```
206
+
9
207
  main.jspのコードです。
10
208
 
11
209
  ```java

2

変更

2016/04/13 08:55

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
+ ![イメージ説明](16cde89281904b33ec08d75f25dcd47c.png)
2
+
1
3
  ![イメージ説明](f5116878f94c51dc9f3af783a5bea08d.png)
2
-
3
- ![イメージ説明](16cde89281904b33ec08d75f25dcd47c.png)
4
4
 
5
5
  この画像のコメント入力欄の下に,コメントした履歴を出力できません、JSTLのcoreタグで書いているはずなのに、なぜ出力されないのでしょうか?
6
6
 

1

画像の追加

2016/04/12 13:45

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,5 @@
1
+ ![イメージ説明](f5116878f94c51dc9f3af783a5bea08d.png)
2
+
1
3
  ![イメージ説明](16cde89281904b33ec08d75f25dcd47c.png)
2
4
 
3
5
  この画像のコメント入力欄の下に,コメントした履歴を出力できません、JSTLのcoreタグで書いているはずなのに、なぜ出力されないのでしょうか?