質問編集履歴
7
リストで返すようにコードを書きなおしましたが、リストで返したデータの取り出し方が調べてもわかりません!
title
CHANGED
File without changes
|
body
CHANGED
@@ -22,6 +22,7 @@
|
|
22
22
|
|
23
23
|
import java.io.IOException;
|
24
24
|
import java.io.PrintWriter;
|
25
|
+
import java.util.List;
|
25
26
|
|
26
27
|
import javax.servlet.ServletException;
|
27
28
|
import javax.servlet.http.HttpServlet;
|
@@ -45,7 +46,7 @@
|
|
45
46
|
|
46
47
|
//ビジネスロジックを呼び出す
|
47
48
|
BusinessLogic logic_ex = new BusinessLogic();
|
48
|
-
SurveyDto resultDto = logic_ex.executeSelectSurvey();
|
49
|
+
List<SurveyDto> resultDto = logic_ex.executeSelectSurvey();
|
49
50
|
|
50
51
|
|
51
52
|
//DTO内のデータを取り出す
|
@@ -67,13 +68,13 @@
|
|
67
68
|
out.println( "<h2>アンケート回答一覧</h2>" ) ;
|
68
69
|
|
69
70
|
out.println( "<table class=\"list\" border=1 id=\"TABLE\">" ) ;
|
70
|
-
out.println( " <tr bgcolor=\"#c0c0c0\"> ");
|
71
|
+
out.println( " <tr bgcolor=\"#c0c0c0\"> "); // trタグ:1行分の情報を表す(1行目)
|
71
|
-
out.println( " <th>名前</th> ");
|
72
|
+
out.println( " <th>名前</th> "); // thタグ:見出しを表す(1列目)
|
72
|
-
out.println( " <th>年齢</th> ");
|
73
|
+
out.println( " <th>年齢</th> "); // thタグ:見出しを表す(2列目)
|
73
|
-
out.println( " <th>性別</th> ");
|
74
|
+
out.println( " <th>性別</th> "); // thタグ:見出しを表す(3列目)
|
74
|
-
out.println( " <th>満足度</th> ");
|
75
|
+
out.println( " <th>満足度</th> "); // thタグ:見出しを表す(4列目)
|
75
|
-
out.println( " <th>ご意見・ご感想</th> ");
|
76
|
+
out.println( " <th>ご意見・ご感想</th> "); // thタグ:見出しを表す(5列目)
|
76
|
-
out.println( " </tr> ");
|
77
|
+
out.println( " </tr> "); // trタグ(閉じ)
|
77
78
|
|
78
79
|
out.println( "<tr> ");
|
79
80
|
out.println( " <td>" + name_ex + "</td> ");
|
@@ -85,7 +86,7 @@
|
|
85
86
|
|
86
87
|
out.println( "</table> ");
|
87
88
|
|
88
|
-
out.println(" <a href=\"InputSurvey\">アンケート画面へ戻る</a> ");
|
89
|
+
out.println(" <a href=\"InputSurvey\">アンケート画面へ戻る</a> "); //前の画面に戻るリンクの設定
|
89
90
|
|
90
91
|
out.println( "</body> ");
|
91
92
|
out.println( "</html> ");
|
@@ -98,25 +99,15 @@
|
|
98
99
|
}
|
99
100
|
|
100
101
|
}
|
102
|
+
|
101
103
|
```
|
102
104
|
【②】MySQLに送信するためのセレクト文
|
103
105
|
```java
|
104
|
-
public SurveyDto doSelect() {
|
106
|
+
public List<SurveyDto> doSelect() {
|
105
107
|
|
106
|
-
//JDBCドライバの相対パス
|
107
|
-
//※バージョンによって変わる可能性があります(MySQL5系の場合は「com.mysql.jdbc.Driver」)
|
108
108
|
String driverName = "com.mysql.cj.jdbc.Driver";
|
109
|
-
|
110
|
-
//接続先のデータベース
|
111
|
-
//※データベース名が「test_db」でない場合は該当の箇所を変更してください
|
112
109
|
String jdbcUrl = "jdbc:mysql://localhost/test_db?characterEncoding=UTF-8&serverTimezone=JST&useSSL=false";
|
113
|
-
|
114
|
-
//接続するユーザー名
|
115
|
-
//※ユーザー名が「test_user」でない場合は該当の箇所を変更してください
|
116
110
|
String userId = "test_user";
|
117
|
-
|
118
|
-
//接続するユーザーのパスワード
|
119
|
-
//※パスワードが「test_pass」でない場合は該当の箇所を変更してください
|
120
111
|
String userPass = "test_pass";
|
121
112
|
|
122
113
|
try {
|
@@ -125,15 +116,14 @@
|
|
125
116
|
e.printStackTrace();
|
126
117
|
}
|
127
118
|
|
128
|
-
Connection con = null ;
|
119
|
+
Connection con = null ;
|
129
|
-
PreparedStatement ps = null ;
|
120
|
+
PreparedStatement ps = null ;
|
130
|
-
ResultSet rs = null ;
|
121
|
+
ResultSet rs = null ;
|
131
122
|
|
132
|
-
SurveyDto
|
123
|
+
List<SurveyDto> dtoList = new ArrayList<SurveyDto>();
|
133
124
|
|
134
125
|
try {
|
135
126
|
con = DriverManager.getConnection(jdbcUrl, userId, userPass);
|
136
|
-
|
137
127
|
//SQL文の生成(SELECT)
|
138
128
|
StringBuffer buf = new StringBuffer() ;
|
139
129
|
buf.append(" SELECT ");
|
@@ -145,20 +135,19 @@
|
|
145
135
|
buf.append(" FROM ");
|
146
136
|
buf.append(" SURVEY ");
|
147
137
|
|
148
|
-
//PreparedStatementオブジェクトを生成&発行するSQLをセット
|
149
138
|
ps = con.prepareStatement(buf.toString());
|
150
139
|
|
151
|
-
//SQL文の送信&抽出結果(ResultSetオブジェクト)の取得
|
152
140
|
rs = ps.executeQuery();
|
153
141
|
|
154
142
|
//ResultSetオブジェクトから1レコード分のデータをDTOに格納
|
155
|
-
|
143
|
+
while(rs.next()){
|
156
|
-
|
144
|
+
SurveyDto dto_list = new SurveyDto();
|
157
|
-
|
145
|
+
dto_list.setName( rs.getString( "NAME" ) );
|
158
|
-
|
146
|
+
dto_list.setAge( rs.getInt( "AGE" ) );
|
159
|
-
|
147
|
+
dto_list.setSex( rs.getInt( "SEX" ) );
|
160
|
-
|
148
|
+
dto_list.setSatisfactionLevel( rs.getInt( "SATISFACTION_LEVEL" ) );
|
161
|
-
|
149
|
+
dto_list.setMessage( rs.getString( "MESSAGE" ) );
|
150
|
+
dtoList.add(dto_list) ;
|
162
151
|
}
|
163
152
|
|
164
153
|
} catch (SQLException e) {
|
@@ -166,45 +155,40 @@
|
|
166
155
|
|
167
156
|
|
168
157
|
} finally {
|
169
|
-
|
170
158
|
//ResultSetオブジェクトの接続解除
|
171
|
-
if (rs != null) {
|
159
|
+
if (rs != null) {
|
172
160
|
try {
|
173
|
-
rs.close();
|
161
|
+
rs.close();
|
174
162
|
} catch (SQLException e) {
|
175
163
|
e.printStackTrace();
|
176
164
|
}
|
177
165
|
}
|
178
|
-
|
179
166
|
//PreparedStatementオブジェクトの接続解除
|
180
|
-
if (ps != null) {
|
167
|
+
if (ps != null) {
|
181
168
|
try {
|
182
|
-
ps.close();
|
169
|
+
ps.close();
|
183
170
|
} catch (SQLException e) {
|
184
171
|
e.printStackTrace();
|
185
172
|
}
|
186
173
|
}
|
187
|
-
|
188
174
|
//Connectionオブジェクトの接続解除
|
189
|
-
if (con != null) {
|
175
|
+
if (con != null) {
|
190
176
|
try {
|
191
|
-
con.close();
|
177
|
+
con.close();
|
192
178
|
} catch (SQLException e) {
|
193
179
|
e.printStackTrace();
|
194
180
|
}
|
195
181
|
}
|
196
182
|
}
|
197
|
-
|
198
|
-
//抽出データを戻す
|
199
|
-
|
183
|
+
return dtoList;
|
200
184
|
}
|
201
185
|
|
202
186
|
```
|
203
187
|
【追記:BusinessLogicクラスのコード一部抜粋】
|
204
188
|
```java
|
205
|
-
public SurveyDto executeSelectSurvey() {
|
189
|
+
public List<SurveyDto> executeSelectSurvey() {
|
206
190
|
SurveyDao dao_ex = new SurveyDao();
|
207
|
-
SurveyDto extractedDto = dao_ex.doSelect();
|
191
|
+
List<SurveyDto> extractedDto = dao_ex.doSelect();
|
208
192
|
return extractedDto ;
|
209
193
|
}
|
210
194
|
```
|
@@ -258,4 +242,17 @@
|
|
258
242
|
}
|
259
243
|
|
260
244
|
```
|
261
|
-
【追記】修正依頼をうけてコードを修正しました!
|
245
|
+
【追記】修正依頼をうけてコードを修正しました!Listでdtoを返すようにコードを書き換えましたが、リストを格納しているdtoからどうやってデータを取得すればよいかわかりません。SowAllSurveyの以下の部分でエラーが出てしまいます。
|
246
|
+
```java
|
247
|
+
//DTO内のデータを取り出す
|
248
|
+
String name_ex = resultDto.getName();
|
249
|
+
String age_ex = String.valueOf(resultDto.getAge());
|
250
|
+
String sex_ex = String.valueOf(resultDto.getSex());
|
251
|
+
String satisfactionLevel_ex = String.valueOf(resultDto.getSatisfactionLevel());
|
252
|
+
String message_ex = resultDto.getMessage();
|
253
|
+
|
254
|
+
```
|
255
|
+
【エラー内容】--------------------
|
256
|
+
例)メソッドgetName()は型List<SurveyDto>で未定義です。
|
257
|
+
ーーーーーーーーーーーーーーーーーーーーーーーーーーー
|
258
|
+
SurveyDtoクラスでList<SurveyDto>で定義しろということでしょうか?でも自分なりに調べたところリストを作るときもdtoクラスはいじらなくてもよいとあったのですが…。
|
6
コードを修正いたしました!!
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,26 +8,7 @@
|
|
8
8
|
(3)そのリンクをおすと、送信されたデータをセレクトして、今まで回答されたアンケートの結果の一覧が表としてブラウザに表示される画面へ画面遷移させる。
|
9
9
|
|
10
10
|
このうち(1)と(2)はうまくいきましたが、(3)のうまくデータを抽出してきて、アンケート入力画面からアンケート結果一覧へ画面遷移させようとするとエラーがおきてしまいます。とにかく(3)の画面遷移をうまくいくようにしたいのです、が、何が問題でうまくいかないのかさっぱりわかりません!!アリゴリズムがまちがっているのかコーディングがまちがっているのか、もしコーディングが間違っていれば正しいコードを教えてください!
|
11
|
-
【エラー】
|
12
|
-
Type Exception Report
|
13
11
|
|
14
|
-
Message null
|
15
|
-
|
16
|
-
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
|
17
|
-
|
18
|
-
Exception
|
19
|
-
java.lang.NumberFormatException: null
|
20
|
-
java.lang.Integer.parseInt(Integer.java:542)
|
21
|
-
java.lang.Integer.parseInt(Integer.java:615)
|
22
|
-
work.SaveSurvey.doPost(SaveSurvey.java:40)
|
23
|
-
work.SaveSurvey.doGet(SaveSurvey.java:26)
|
24
|
-
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
|
25
|
-
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
|
26
|
-
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
|
27
|
-
|
28
|
-
|
29
|
-
Note The full stack trace of the root cause is available in the server logs.
|
30
|
-
|
31
12
|
【各クラスで行っていること】
|
32
13
|
①InputSurveyクラス:ブラウザに表示させるためのHTML文書(アンケート入力画面)の出力と「アンケート提出ボタン」と「アンケート結果一覧へ」というリンクをコーディングしています。
|
33
14
|
②SaveSurveyクラス:リクエストパラメーターを取得し、アンケートデータを作成(DTOクラスをインスタンス化)し、BusinessLogicクラスをインスタンス化。また、データベース(アンケート)の送信が上手くいったかどうかに応じた画面を表示するようにコーディング。
|
@@ -40,8 +21,7 @@
|
|
40
21
|
package work;
|
41
22
|
|
42
23
|
import java.io.IOException;
|
43
|
-
import java.
|
24
|
+
import java.io.PrintWriter;
|
44
|
-
import java.util.List;
|
45
25
|
|
46
26
|
import javax.servlet.ServletException;
|
47
27
|
import javax.servlet.http.HttpServlet;
|
@@ -75,45 +55,41 @@
|
|
75
55
|
String satisfactionLevel_ex = String.valueOf(resultDto.getSatisfactionLevel());
|
76
56
|
String message_ex = resultDto.getMessage();
|
77
57
|
|
78
|
-
//
|
58
|
+
//出力用のストリームの取得
|
79
|
-
List<String> postContentList = new ArrayList<String>() ;
|
80
|
-
postContentList.add( name_ex ) ;
|
81
|
-
postContentList.add( age_ex ) ;
|
82
|
-
postContentList.add( sex_ex ) ;
|
83
|
-
postContentList.add( satisfactionLevel_ex ) ;
|
84
|
-
|
59
|
+
PrintWriter out = response.getWriter();
|
85
60
|
|
86
61
|
//HTML文書(HTMLテーブル作成Sample画面)の出力
|
87
|
-
|
62
|
+
out.println( "<html>" ) ;
|
88
|
-
|
63
|
+
out.println( "<head>" ) ;
|
89
|
-
|
64
|
+
out.println( "<title>アンケート回答一覧</title>" ) ;
|
90
|
-
|
65
|
+
out.println( "</head>" ) ;
|
91
|
-
|
66
|
+
out.println( "<body>" ) ;
|
92
|
-
|
67
|
+
out.println( "<h2>アンケート回答一覧</h2>" ) ;
|
93
68
|
|
94
|
-
|
69
|
+
out.println( "<table class=\"list\" border=1 id=\"TABLE\">" ) ;
|
95
|
-
|
70
|
+
out.println( " <tr bgcolor=\"#c0c0c0\"> ");
|
96
|
-
|
71
|
+
out.println( " <th>名前</th> ");
|
97
|
-
|
72
|
+
out.println( " <th>年齢</th> ");
|
98
|
-
|
73
|
+
out.println( " <th>性別</th> ");
|
99
|
-
|
74
|
+
out.println( " <th>満足度</th> ");
|
100
|
-
|
75
|
+
out.println( " <th>ご意見・ご感想</th> ");
|
101
|
-
|
76
|
+
out.println( " </tr> ");
|
102
77
|
|
103
|
-
for (int i = 0; i < postContentList.size(); i++) {
|
104
|
-
|
78
|
+
out.println( "<tr> ");
|
79
|
+
out.println( " <td>" + name_ex + "</td> ");
|
80
|
+
out.println( " <td>" + age_ex + "</td> ");
|
81
|
+
out.println( " <td>" + sex_ex + "</td> ");
|
105
|
-
|
82
|
+
out.println( " <td>" + satisfactionLevel_ex + "</td> ");
|
83
|
+
out.println( " <td>" + message_ex + "</td> ");
|
106
|
-
|
84
|
+
out.println( "</tr> ");
|
107
85
|
|
108
|
-
|
86
|
+
out.println( "</table> ");
|
109
87
|
|
110
|
-
|
88
|
+
out.println(" <a href=\"InputSurvey\">アンケート画面へ戻る</a> ");
|
111
89
|
|
112
|
-
|
90
|
+
out.println( "</body> ");
|
91
|
+
out.println( "</html> ");
|
113
92
|
|
114
|
-
System.out.println( "</body> ");
|
115
|
-
System.out.println( "</html> ");
|
116
|
-
|
117
93
|
}
|
118
94
|
|
119
95
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
@@ -178,11 +154,11 @@
|
|
178
154
|
//ResultSetオブジェクトから1レコード分のデータをDTOに格納
|
179
155
|
if(rs.next()){
|
180
156
|
dto = new SurveyDto();
|
181
|
-
dto.setName( rs.getString( "
|
157
|
+
dto.setName( rs.getString( "NAME" ) );
|
182
|
-
dto.setAge( rs.getInt( "
|
158
|
+
dto.setAge( rs.getInt( "AGE" ) );
|
183
|
-
dto.setSex( rs.getInt( "
|
159
|
+
dto.setSex( rs.getInt( "SEX" ) );
|
184
|
-
dto.setSatisfactionLevel( rs.getInt( "
|
160
|
+
dto.setSatisfactionLevel( rs.getInt( "SATISFACTION_LEVEL" ) );
|
185
|
-
dto.setMessage( rs.getString( "
|
161
|
+
dto.setMessage( rs.getString( "MESSAGE" ) );
|
186
162
|
}
|
187
163
|
|
188
164
|
} catch (SQLException e) {
|
@@ -282,4 +258,4 @@
|
|
282
258
|
}
|
283
259
|
|
284
260
|
```
|
285
|
-
|
261
|
+
【追記】修正依頼をうけてコードを修正しました!テーブルはブラウザに出るようになったのですが、MYSQLのデータの一番初めのデータしかでてくれません。一応アンケート結果一覧なので登録されている全てのデータを選択したいのですが…。
|
5
Savesurveyクラスのコードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -232,4 +232,54 @@
|
|
232
232
|
return extractedDto ;
|
233
233
|
}
|
234
234
|
```
|
235
|
+
|
236
|
+
【追記:Savesurveyクラスのコード】
|
237
|
+
```java
|
238
|
+
public class SaveSurvey extends HttpServlet {
|
239
|
+
private static final long serialVersionUID = 1L;
|
240
|
+
|
241
|
+
public SaveSurvey() {
|
242
|
+
super();
|
243
|
+
}
|
244
|
+
|
245
|
+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
246
|
+
throws ServletException, IOException {
|
247
|
+
doPost(request, response);
|
248
|
+
}
|
249
|
+
|
250
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
251
|
+
throws ServletException, IOException {
|
252
|
+
|
253
|
+
response.setContentType("text/html;charset=UTF-8");
|
254
|
+
request.setCharacterEncoding("UTF-8");
|
255
|
+
|
256
|
+
//リクエストパラメータを取得
|
257
|
+
String name = request.getParameter("NAME");
|
258
|
+
int age = Integer.parseInt(request.getParameter("AGE"));
|
259
|
+
int sex = Integer.parseInt(request.getParameter("SEX")) ;
|
260
|
+
int satisfactionLevel = Integer.parseInt(request.getParameter("SATISFACTION_LEVEL")) ;
|
261
|
+
String message = request.getParameter("MESSAGE");
|
262
|
+
|
263
|
+
//アンケートデータ(SurveyDto型)の作成
|
264
|
+
SurveyDto dto = new SurveyDto();
|
265
|
+
dto.setName( name );
|
266
|
+
dto.setAge( age );
|
267
|
+
dto.setSex( sex );
|
268
|
+
dto.setSatisfactionLevel( satisfactionLevel );
|
269
|
+
dto.setMessage( message );
|
270
|
+
dto.setTime( new Timestamp(System.currentTimeMillis()) );
|
271
|
+
//アンケートデータをDBに登録
|
272
|
+
BusinessLogic logic = new BusinessLogic();
|
273
|
+
boolean succesInsert = logic.executeInsertSurvey(dto);
|
274
|
+
|
275
|
+
//DB操作の成功/失敗に応じて表示させる画面を振り分ける
|
276
|
+
if (succesInsert) {
|
277
|
+
response.sendRedirect("htmls/finish.html");
|
278
|
+
} else {
|
279
|
+
response.sendRedirect("htmls/error.html");
|
280
|
+
}
|
281
|
+
}
|
282
|
+
}
|
283
|
+
|
284
|
+
```
|
235
285
|
現時点でEclipse上になんのエラーマークもないです。ですが、アンケート一覧が表示されません...。どうすればよいのか...。
|
4
もう一度考え直してコードを全体的に書き換えました!!
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,8 +8,7 @@
|
|
8
8
|
(3)そのリンクをおすと、送信されたデータをセレクトして、今まで回答されたアンケートの結果の一覧が表としてブラウザに表示される画面へ画面遷移させる。
|
9
9
|
|
10
10
|
このうち(1)と(2)はうまくいきましたが、(3)のうまくデータを抽出してきて、アンケート入力画面からアンケート結果一覧へ画面遷移させようとするとエラーがおきてしまいます。とにかく(3)の画面遷移をうまくいくようにしたいのです、が、何が問題でうまくいかないのかさっぱりわかりません!!アリゴリズムがまちがっているのかコーディングがまちがっているのか、もしコーディングが間違っていれば正しいコードを教えてください!
|
11
|
-
【エラー
|
11
|
+
【エラー】
|
12
|
-
HTTP Status 500 – Internal Server Error
|
13
12
|
Type Exception Report
|
14
13
|
|
15
14
|
Message null
|
@@ -28,7 +27,6 @@
|
|
28
27
|
|
29
28
|
|
30
29
|
Note The full stack trace of the root cause is available in the server logs.
|
31
|
-
Apache Tomcat/8.5.51
|
32
30
|
|
33
31
|
【各クラスで行っていること】
|
34
32
|
①InputSurveyクラス:ブラウザに表示させるためのHTML文書(アンケート入力画面)の出力と「アンケート提出ボタン」と「アンケート結果一覧へ」というリンクをコーディングしています。
|
@@ -50,11 +48,6 @@
|
|
50
48
|
import javax.servlet.http.HttpServletRequest;
|
51
49
|
import javax.servlet.http.HttpServletResponse;
|
52
50
|
|
53
|
-
/**----------------------------------------------------------------------*
|
54
|
-
*■■■ShowAllSurveyクラス■■■
|
55
|
-
*概要:サーブレット
|
56
|
-
*詳細:HTML文書(回答入力画面の結果)を出力する。
|
57
|
-
*----------------------------------------------------------------------**/
|
58
51
|
public class ShowAllSurvey extends HttpServlet {
|
59
52
|
private static final long serialVersionUID = 1L;
|
60
53
|
|
@@ -70,24 +63,25 @@
|
|
70
63
|
//リクエスト(受信データ)の文字コードを設定
|
71
64
|
request.setCharacterEncoding("UTF-8");
|
72
65
|
|
73
|
-
//リクエストパラメータを取得
|
74
|
-
String name = request.getParameter("NAME"); //リクエストパラメータ(NAME)
|
75
|
-
String age = request.getParameter("AGE"); //リクエストパラメータ(AGE)
|
76
|
-
String sex = request.getParameter("SEX"); //リクエストパラメータ(SEX)
|
77
|
-
String satisfactionLevel = request.getParameter("SATISFACTION_LEVEL") ; //リクエストパラメータ(SATISFACTION_LEVEL)
|
78
|
-
String message = request.getParameter("MESSAGE"); //リクエストパラメータ(MESSAGE)
|
79
|
-
|
80
66
|
//ビジネスロジックを呼び出す
|
81
67
|
BusinessLogic logic_ex = new BusinessLogic();
|
82
|
-
logic_ex.executeSelectSurvey();
|
68
|
+
SurveyDto resultDto = logic_ex.executeSelectSurvey();
|
83
69
|
|
70
|
+
|
71
|
+
//DTO内のデータを取り出す
|
72
|
+
String name_ex = resultDto.getName();
|
73
|
+
String age_ex = String.valueOf(resultDto.getAge());
|
74
|
+
String sex_ex = String.valueOf(resultDto.getSex());
|
75
|
+
String satisfactionLevel_ex = String.valueOf(resultDto.getSatisfactionLevel());
|
76
|
+
String message_ex = resultDto.getMessage();
|
77
|
+
|
84
78
|
//投稿内容のリスト
|
85
79
|
List<String> postContentList = new ArrayList<String>() ;
|
86
|
-
postContentList.add(
|
80
|
+
postContentList.add( name_ex ) ;
|
87
|
-
postContentList.add(
|
81
|
+
postContentList.add( age_ex ) ;
|
88
|
-
postContentList.add(
|
82
|
+
postContentList.add( sex_ex ) ;
|
89
|
-
postContentList.add(
|
83
|
+
postContentList.add( satisfactionLevel_ex ) ;
|
90
|
-
postContentList.add(
|
84
|
+
postContentList.add( message_ex ) ;
|
91
85
|
|
92
86
|
//HTML文書(HTMLテーブル作成Sample画面)の出力
|
93
87
|
System.out.println( "<html>" ) ;
|
@@ -128,17 +122,11 @@
|
|
128
122
|
}
|
129
123
|
|
130
124
|
}
|
131
|
-
|
132
125
|
```
|
133
126
|
【②】MySQLに送信するためのセレクト文
|
134
127
|
```java
|
135
|
-
public static String COMMA = ","; //コンマ(定数)
|
136
|
-
|
128
|
+
public SurveyDto doSelect() {
|
137
129
|
|
138
|
-
//-------------------------------------------
|
139
|
-
//データベースへの接続情報
|
140
|
-
//-------------------------------------------
|
141
|
-
|
142
130
|
//JDBCドライバの相対パス
|
143
131
|
//※バージョンによって変わる可能性があります(MySQL5系の場合は「com.mysql.jdbc.Driver」)
|
144
132
|
String driverName = "com.mysql.cj.jdbc.Driver";
|
@@ -155,33 +143,21 @@
|
|
155
143
|
//※パスワードが「test_pass」でない場合は該当の箇所を変更してください
|
156
144
|
String userPass = "test_pass";
|
157
145
|
|
158
|
-
//-------------------------------------------
|
159
|
-
//① JDBCドライバのロード
|
160
|
-
//-------------------------------------------
|
161
146
|
try {
|
162
|
-
Class.forName(driverName);
|
147
|
+
Class.forName(driverName);
|
163
148
|
} catch (ClassNotFoundException e) {
|
164
149
|
e.printStackTrace();
|
165
150
|
}
|
166
151
|
|
167
|
-
|
168
|
-
//JDBCの接続に使用するオブジェクトを宣言
|
169
|
-
//※finallyブロックでも扱うためtryブロック内で宣言してはいけないことに注意
|
170
152
|
Connection con = null ; // Connection(DB接続情報)格納用変数
|
171
153
|
PreparedStatement ps = null ; // PreparedStatement(SQL発行用オブジェクト)格納用変数
|
172
154
|
ResultSet rs = null ; // ResultSet(SQL抽出結果)格納用変数
|
173
|
-
boolean isSuccess = true ;
|
174
155
|
|
156
|
+
SurveyDto dto = null;
|
157
|
+
|
175
158
|
try {
|
176
|
-
//-------------------------------------------
|
177
|
-
// ②接続の確立(Connectionオブジェクトの取得)
|
178
|
-
//-------------------------------------------
|
179
159
|
con = DriverManager.getConnection(jdbcUrl, userId, userPass);
|
180
160
|
|
181
|
-
//-------------------------------------------
|
182
|
-
// ③SQL文の送信 & ④抽出結果の取得
|
183
|
-
//-------------------------------------------
|
184
|
-
|
185
161
|
//SQL文の生成(SELECT)
|
186
162
|
StringBuffer buf = new StringBuffer() ;
|
187
163
|
buf.append(" SELECT ");
|
@@ -199,59 +175,31 @@
|
|
199
175
|
//SQL文の送信&抽出結果(ResultSetオブジェクト)の取得
|
200
176
|
rs = ps.executeQuery();
|
201
177
|
|
202
|
-
//ResultSetオブジェクトから1レコード
|
178
|
+
//ResultSetオブジェクトから1レコード分のデータをDTOに格納
|
203
|
-
|
179
|
+
if(rs.next()){
|
204
|
-
|
205
|
-
//1レコード分のデータを取得&加工(各カラムをコンマ綴りで結合)
|
206
|
-
|
180
|
+
dto = new SurveyDto();
|
207
|
-
|
181
|
+
dto.setName( rs.getString( "name" ) );
|
208
|
-
rsbuf.append(COMMA);
|
209
|
-
|
182
|
+
dto.setAge( rs.getInt( "age" ) );
|
210
|
-
rsbuf.append(COMMA);
|
211
|
-
|
183
|
+
dto.setSex( rs.getInt( "sex" ) );
|
212
|
-
rsbuf.append(COMMA);
|
213
|
-
|
184
|
+
dto.setSatisfactionLevel( rs.getInt( "satisfactionLevel" ) );
|
214
|
-
rsbuf.append(COMMA);
|
215
|
-
|
185
|
+
dto.setMessage( rs.getString( "message" ) );
|
216
|
-
|
217
|
-
//加工作成した1レコード分のデータを表示
|
218
|
-
System.out.println(rsbuf.toString());
|
219
186
|
}
|
220
187
|
|
221
|
-
//SQL文の実行
|
222
|
-
ps.executeUpdate();
|
223
|
-
|
224
188
|
} catch (SQLException e) {
|
225
189
|
e.printStackTrace();
|
226
190
|
|
227
|
-
//実行結果を例外発生として更新
|
228
|
-
isSuccess = false ;
|
229
191
|
|
230
192
|
} finally {
|
231
|
-
//-------------------------------------------
|
232
|
-
//トランザクションの終了
|
233
|
-
//-------------------------------------------
|
234
|
-
if(isSuccess){
|
235
|
-
//明示的にコミットを実施
|
236
|
-
try {
|
237
|
-
con.commit();
|
238
|
-
} catch (SQLException e) {
|
239
|
-
e.printStackTrace();
|
240
|
-
}
|
241
193
|
|
242
|
-
}else{
|
243
|
-
|
194
|
+
//ResultSetオブジェクトの接続解除
|
195
|
+
if (rs != null) { //接続が確認できている場合のみ実施
|
244
196
|
try {
|
245
|
-
|
197
|
+
rs.close(); //接続の解除
|
246
198
|
} catch (SQLException e) {
|
247
199
|
e.printStackTrace();
|
248
200
|
}
|
249
201
|
}
|
250
202
|
|
251
|
-
//-------------------------------------------
|
252
|
-
//接続の解除
|
253
|
-
//-------------------------------------------
|
254
|
-
|
255
203
|
//PreparedStatementオブジェクトの接続解除
|
256
204
|
if (ps != null) { //接続が確認できている場合のみ実施
|
257
205
|
try {
|
@@ -269,21 +217,19 @@
|
|
269
217
|
e.printStackTrace();
|
270
218
|
}
|
271
219
|
}
|
272
|
-
|
273
220
|
}
|
274
221
|
|
275
|
-
//
|
222
|
+
//抽出データを戻す
|
276
|
-
return
|
223
|
+
return dto;
|
277
224
|
}
|
278
225
|
|
279
|
-
|
226
|
+
```
|
227
|
+
【追記:BusinessLogicクラスのコード一部抜粋】
|
228
|
+
```java
|
229
|
+
public SurveyDto executeSelectSurvey() {
|
230
|
+
SurveyDao dao_ex = new SurveyDao();
|
231
|
+
SurveyDto extractedDto = dao_ex.doSelect();
|
232
|
+
return extractedDto ;
|
280
233
|
}
|
281
|
-
|
282
234
|
```
|
283
|
-
|
235
|
+
現時点でEclipse上になんのエラーマークもないです。ですが、アンケート一覧が表示されません...。どうすればよいのか...。
|
284
|
-
```
|
285
|
-
public void executeSelectSurvey() {
|
286
|
-
SurveyDao dao_Ex = new SurveyDao();
|
287
|
-
dao_Ex.doSelect();
|
288
|
-
}
|
289
|
-
```
|
3
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
複数のクラスが複雑に絡んでいるのでやりたいことを順番立てて箇条書きにしたいと思います。複数のクラスにたくさんコードを書いたのですべてここに乗せると字数が足りなくなってしまいますので、【①】javaでHTMLを使ってブラウザに表示させるためのコードを書いたクラス全体と、【②】MySQLに送信するためのセレクト文、のみここに乗せたいと思います。ほかは追記で頂いた場合コードを載せ
|
1
|
+
複数のクラスが複雑に絡んでいるのでやりたいことを順番立てて箇条書きにしたいと思います。複数のクラスにたくさんコードを書いたのですべてここに乗せると字数が足りなくなってしまいますので、【①】javaでHTMLを使ってブラウザに表示させるためのコードを書いたクラス全体と、【②】MySQLに送信するためのセレクト文、のみここに乗せたいと思います。ほかは追記で頂いた場合コードをコメント欄に載せようかと思いますので、コメントお願いします。
|
2
2
|
|
3
3
|
【要約】
|
4
4
|
(1)ブラウザにアンケート入力フォームを表示させる
|
2
doSelectの呼び出し元です
title
CHANGED
File without changes
|
body
CHANGED
@@ -279,4 +279,11 @@
|
|
279
279
|
|
280
280
|
}
|
281
281
|
|
282
|
+
```
|
283
|
+
【追記:doSelectメソッドの呼び出し元:BusinessLogicクラスの中身の一部抜粋】
|
284
|
+
```
|
285
|
+
public void executeSelectSurvey() {
|
286
|
+
SurveyDao dao_Ex = new SurveyDao();
|
287
|
+
dao_Ex.doSelect();
|
288
|
+
}
|
282
289
|
```
|
1
自分のしてもらいたいことを明示しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
(3)そのリンクをおすと、送信されたデータをセレクトして、今まで回答されたアンケートの結果の一覧が表としてブラウザに表示される画面へ画面遷移させる。
|
9
9
|
|
10
|
-
このうち(1)と(2)はうまくいきましたが、(3)のうまくデータを抽出してきて、アンケート入力画面からアンケート結果一覧へ画面遷移させようとするとエラーがおきてしまいます。
|
10
|
+
このうち(1)と(2)はうまくいきましたが、(3)のうまくデータを抽出してきて、アンケート入力画面からアンケート結果一覧へ画面遷移させようとするとエラーがおきてしまいます。とにかく(3)の画面遷移をうまくいくようにしたいのです、が、何が問題でうまくいかないのかさっぱりわかりません!!アリゴリズムがまちがっているのかコーディングがまちがっているのか、もしコーディングが間違っていれば正しいコードを教えてください!
|
11
11
|
【エラー内容】
|
12
12
|
HTTP Status 500 – Internal Server Error
|
13
13
|
Type Exception Report
|