質問編集履歴
2
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -294,27 +294,33 @@
|
|
294
294
|
|
295
295
|
|
296
296
|
|
297
|
+
ABBREVIATION:VAT FULLNAME:Holy See (Vatican City State) REGION:Europe SUBREGION:Southern Europe
|
298
|
+
|
297
|
-
I
|
299
|
+
ABBREVIATION:VCT FULLNAME:Saint Vincent and the Grenadines REGION:North America SUBREGION:Caribbean
|
298
|
-
|
300
|
+
|
299
|
-
I
|
301
|
+
ABBREVIATION:VEN FULLNAME:Venezuela REGION:South America SUBREGION:South America
|
300
|
-
|
302
|
+
|
301
|
-
I
|
303
|
+
ABBREVIATION:VGB FULLNAME:Virgin Islands, British REGION:North America SUBREGION:Caribbean
|
302
|
-
|
304
|
+
|
303
|
-
I
|
305
|
+
ABBREVIATION:VIR FULLNAME:Virgin Islands, U.S. REGION:North America SUBREGION:Caribbean
|
304
|
-
|
306
|
+
|
305
|
-
I
|
307
|
+
ABBREVIATION:VNM FULLNAME:Vietnam REGION:Asia SUBREGION:Southeast Asia
|
306
|
-
|
308
|
+
|
307
|
-
I
|
309
|
+
ABBREVIATION:VUT FULLNAME:Vanuatu REGION:Oceania SUBREGION:Melanesia
|
308
|
-
|
310
|
+
|
309
|
-
I
|
311
|
+
ABBREVIATION:WLF FULLNAME:Wallis and Futuna REGION:Oceania SUBREGION:Polynesia
|
310
|
-
|
312
|
+
|
311
|
-
I
|
313
|
+
ABBREVIATION:WSM FULLNAME:Samoa REGION:Oceania SUBREGION:Polynesia
|
312
|
-
|
314
|
+
|
313
|
-
I
|
315
|
+
ABBREVIATION:YEM FULLNAME:Yemen REGION:Asia SUBREGION:Middle East
|
314
|
-
|
316
|
+
|
315
|
-
I
|
317
|
+
ABBREVIATION:YUG FULLNAME:Yugoslavia REGION:Europe SUBREGION:Southern Europe
|
316
|
-
|
318
|
+
|
317
|
-
I
|
319
|
+
ABBREVIATION:ZAF FULLNAME:South Africa REGION:Africa SUBREGION:Southern Africa
|
320
|
+
|
321
|
+
ABBREVIATION:ZMB FULLNAME:Zambia REGION:Africa SUBREGION:Eastern Africa
|
322
|
+
|
323
|
+
ABBREVIATION:ZWE FULLNAME:Zimbabwe REGION:Africa SUBREGION:Eastern Africa
|
318
324
|
|
319
325
|
|
320
326
|
|
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,11 +54,275 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
+
package test3;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
import java.io.IOException;
|
62
|
+
|
63
|
+
import java.io.PrintWriter;
|
64
|
+
|
65
|
+
import java.sql.Connection;
|
66
|
+
|
67
|
+
import java.sql.DriverManager;
|
68
|
+
|
69
|
+
import java.sql.ResultSet;
|
70
|
+
|
71
|
+
import java.sql.SQLException;
|
72
|
+
|
73
|
+
import java.sql.Statement;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
import javax.servlet.ServletException;
|
78
|
+
|
79
|
+
import javax.servlet.http.HttpServlet;
|
80
|
+
|
81
|
+
import javax.servlet.http.HttpServletRequest;
|
82
|
+
|
83
|
+
import javax.servlet.http.HttpServletResponse;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
public class Test33 extends HttpServlet {
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
public static void main(String[] args) {
|
94
|
+
|
95
|
+
// TODO 自動生成されたメソッド・スタブ
|
96
|
+
|
97
|
+
try {
|
98
|
+
|
99
|
+
Class.forName("com.mysql.jdbc.Driver");
|
100
|
+
|
101
|
+
Connection conn = DriverManager.getConnection(
|
102
|
+
|
103
|
+
"jdbc:mysql://localhost:3306/world","root","root");
|
104
|
+
|
105
|
+
Statement st = conn.createStatement();
|
106
|
+
|
107
|
+
ResultSet rs =
|
108
|
+
|
109
|
+
st.executeQuery("select * from country");
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
while(rs.next()) {
|
114
|
+
|
115
|
+
String id = rs.getString(1);
|
116
|
+
|
117
|
+
String location = rs.getString(2);
|
118
|
+
|
119
|
+
String tid = rs.getString(3);
|
120
|
+
|
121
|
+
String time = rs.getString(4);
|
122
|
+
|
123
|
+
System.out.println
|
124
|
+
|
125
|
+
("ABBREVIATION:"+id+" FULLNAME:"+location+" REGION:"+tid+" SUBREGION:"+time);
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
rs.close();
|
130
|
+
|
131
|
+
st.close();
|
132
|
+
|
133
|
+
conn.close();
|
134
|
+
|
135
|
+
System.exit(0);
|
136
|
+
|
137
|
+
} catch (ClassNotFoundException e) {
|
138
|
+
|
139
|
+
System.out.println("ドライバを読み込めませんでした "+ e);
|
140
|
+
|
141
|
+
} catch (SQLException e) {
|
142
|
+
|
143
|
+
System.out.println("データベース接続エラー"+ e);
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
156
|
+
|
157
|
+
throws IOException, ServletException
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
response.setContentType("text/html");
|
162
|
+
|
163
|
+
PrintWriter writer = response.getWriter();
|
164
|
+
|
165
|
+
writer.println("<html>");
|
166
|
+
|
167
|
+
writer.println("<head>");
|
168
|
+
|
169
|
+
writer.println("<title>Sample Application Servlet Page</title>");
|
170
|
+
|
171
|
+
writer.println("</head>");
|
172
|
+
|
173
|
+
writer.println("<body bgcolor=white>");
|
174
|
+
|
175
|
+
writer.println("<table border=\"0\">");
|
176
|
+
|
177
|
+
writer.println("<tr>");
|
178
|
+
|
179
|
+
writer.println("<td>");
|
180
|
+
|
181
|
+
writer.println("<img src=\"images/tomcat.gif\">");
|
182
|
+
|
183
|
+
writer.println("</td>");
|
184
|
+
|
185
|
+
writer.println("<td>");
|
186
|
+
|
187
|
+
writer.println("<h1>Sample Application Servlet</h1>");
|
188
|
+
|
189
|
+
writer.println("This is the output of a servlet that is part of");
|
190
|
+
|
191
|
+
writer.println("the Hello, World application.ssss");
|
192
|
+
|
193
|
+
writer.println("</td>");
|
194
|
+
|
195
|
+
writer.println("</tr>");
|
196
|
+
|
197
|
+
writer.println("</table>");
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
try {
|
206
|
+
|
207
|
+
Class.forName("com.mysql.jdbc.Driver");
|
208
|
+
|
209
|
+
Connection conn = DriverManager.getConnection(
|
210
|
+
|
211
|
+
"jdbc:mysql://localhost:3306/world","root","root");
|
212
|
+
|
213
|
+
Statement st = conn.createStatement();
|
214
|
+
|
215
|
+
ResultSet rs =
|
216
|
+
|
217
|
+
st.executeQuery("select * from country");
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
while(rs.next()) {
|
222
|
+
|
223
|
+
String id = rs.getString(1);
|
224
|
+
|
225
|
+
String location = rs.getString(2);
|
226
|
+
|
227
|
+
String tid = rs.getString(3);
|
228
|
+
|
229
|
+
String time = rs.getString(4);
|
230
|
+
|
231
|
+
System.out.println
|
232
|
+
|
233
|
+
("ABBREVIATION:"+id+" FULLNAME:"+location+" REGION:"+tid+" SUBREGION:"+time);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
rs.close();
|
238
|
+
|
239
|
+
st.close();
|
240
|
+
|
241
|
+
conn.close();
|
242
|
+
|
243
|
+
System.exit(0);
|
244
|
+
|
245
|
+
} catch (ClassNotFoundException e) {
|
246
|
+
|
247
|
+
System.out.println("ドライバを読み込めませんでした "+ e);
|
248
|
+
|
249
|
+
} catch (SQLException e) {
|
250
|
+
|
251
|
+
System.out.println("データベース接続エラー"+ e);
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
public void doPost (HttpServletRequest request, HttpServletResponse response)
|
264
|
+
|
265
|
+
throws IOException, ServletException
|
266
|
+
|
267
|
+
{
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
288
|
+
|
57
289
|
### 試したこと
|
58
290
|
|
59
291
|
|
60
292
|
|
61
|
-
|
293
|
+
JAVAアプリケーションで実行した場合、コンソールに下記のように出ますので、public static void main(String[] args) は正常であると思われます。
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
ID:VCT loction:Saint Vincent and the Grenadines tid:North America time:Caribbean
|
298
|
+
|
299
|
+
ID:VEN loction:Venezuela tid:South America time:South America
|
300
|
+
|
301
|
+
ID:VGB loction:Virgin Islands, British tid:North America time:Caribbean
|
302
|
+
|
303
|
+
ID:VIR loction:Virgin Islands, U.S. tid:North America time:Caribbean
|
304
|
+
|
305
|
+
ID:VNM loction:Vietnam tid:Asia time:Southeast Asia
|
306
|
+
|
307
|
+
ID:VUT loction:Vanuatu tid:Oceania time:Melanesia
|
308
|
+
|
309
|
+
ID:WLF loction:Wallis and Futuna tid:Oceania time:Polynesia
|
310
|
+
|
311
|
+
ID:WSM loction:Samoa tid:Oceania time:Polynesia
|
312
|
+
|
313
|
+
ID:YEM loction:Yemen tid:Asia time:Middle East
|
314
|
+
|
315
|
+
ID:YUG loction:Yugoslavia tid:Europe time:Southern Europe
|
316
|
+
|
317
|
+
ID:ZAF loction:South Africa tid:Africa time:Southern Africa
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
次に、Tomcatサーバで実行した場合、public void doGet (HttpServletRequest request, HttpServletResponse response)が実行されると思いますし、実際にWebページはwriter.printlnで出力したHTMLが表示されるのですが、その後にあるDBの中身の結果は表示されず、
|
322
|
+
|
323
|
+
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
|
324
|
+
|
325
|
+
とコンソールに出てしまいます。
|
62
326
|
|
63
327
|
|
64
328
|
|