質問編集履歴

1

情報の追加

2016/04/03 16:18

投稿

manmos
manmos

スコア55

test CHANGED
File without changes
test CHANGED
@@ -153,3 +153,323 @@
153
153
 
154
154
 
155
155
  どうぞ宜しくお願い致します。
156
+
157
+
158
+
159
+ 追記です。
160
+
161
+
162
+
163
+ 実装に何を使っているというのがよくわからないのですが、各コードを記載すればよろしいでしょうか。
164
+
165
+
166
+
167
+ Dao:Serect用メソッド
168
+
169
+
170
+
171
+ ```
172
+
173
+ public class listDao {
174
+
175
+
176
+
177
+ private static final String url = "jdbc:mysql://localhost:3306/test";
178
+
179
+ private static final String user = "root";
180
+
181
+ private static final String password = "";
182
+
183
+
184
+
185
+ private static Connection conn = null; // Connectionクラス
186
+
187
+ private static PreparedStatement pstmt = null; // Statementクラス
188
+
189
+ private static ResultSet rs = null; // ResultSetクラス
190
+
191
+
192
+
193
+ public ArrayList<Listinfo> findAll(String[] args) {
194
+
195
+ ArrayList<Listinfo> listInfos = new ArrayList<Listinfo>();
196
+
197
+
198
+
199
+ String sql = "SELECT * FROM e_einfo";
200
+
201
+
202
+
203
+ try {
204
+
205
+ Class.forName("com.mysql.jdbc.Driver"); // ドライバクラスのロード
206
+
207
+
208
+
209
+ // Connection,Statement,ResultSetのクラスを作成
210
+
211
+
212
+
213
+ conn = DriverManager.getConnection(url, user, password); // Connectionクラス生成
214
+
215
+ pstmt = conn.prepareStatement(sql); // Statementクラス生成
216
+
217
+ rs = pstmt.executeQuery(); // ResultSetクラス生成
218
+
219
+
220
+
221
+ while (rs.next()) {
222
+
223
+ // 結果表からデータを取得
224
+
225
+ Listinfo list = new Listinfo();
226
+
227
+
228
+
229
+ // list.setAge(rs.getInt("age"));
230
+
231
+
232
+
233
+ list.setEmployeeId(rs.getString("employee_id"));
234
+
235
+ list.setName(rs.getString("name"));
236
+
237
+ list.setNameHiragana(rs.getString("name_hiragana"));
238
+
239
+
240
+
241
+ String birthday = rs.getString("birthday");
242
+
243
+ int age = convertAge(birthday);
244
+
245
+ list.setAge(age);
246
+
247
+
248
+
249
+ listInfos.add(list);
250
+
251
+
252
+
253
+ }
254
+
255
+
256
+
257
+ } catch (SQLException e) {
258
+
259
+ e.printStackTrace();
260
+
261
+ } catch (ClassNotFoundException e) {
262
+
263
+ } finally {
264
+
265
+ if (rs != null) {
266
+
267
+ try {
268
+
269
+ rs.close();
270
+
271
+ } catch (SQLException e) {
272
+
273
+ }
274
+
275
+ rs = null;
276
+
277
+ }
278
+
279
+ if (pstmt != null) {
280
+
281
+ try {
282
+
283
+ pstmt.close();
284
+
285
+ } catch (SQLException e) {
286
+
287
+ }
288
+
289
+ pstmt = null;
290
+
291
+ }
292
+
293
+ if (conn != null) {
294
+
295
+ try {
296
+
297
+ conn.close();
298
+
299
+ } catch (SQLException e) {
300
+
301
+ }
302
+
303
+ conn = null;
304
+
305
+ }
306
+
307
+
308
+
309
+ }
310
+
311
+ return listInfos;
312
+
313
+ }
314
+
315
+ ```
316
+
317
+ Dao:Insertメソッド
318
+
319
+
320
+
321
+ ```
322
+
323
+ public void InsertInformation(String employeeId, String name,
324
+
325
+ String name_hiragana, String birthday) throws ClassNotFoundException, SQLException {
326
+
327
+ // SQL文
328
+
329
+ String sql = "INSERT INTO employee_info (name, name_hiragana, birthday) values(?, ?, cast(? as date))";
330
+
331
+
332
+
333
+ try {
334
+
335
+ Class.forName("com.mysql.jdbc.Driver"); // ドライバクラスのロード
336
+
337
+
338
+
339
+ conn = DriverManager.getConnection(url, user, password); // Connectionクラス生成
340
+
341
+ pstmt = conn.prepareStatement(sql); // PreparedStatementクラス生成
342
+
343
+
344
+
345
+ pstmt.setString(1, name);
346
+
347
+ pstmt.setString(2, name_hiragana);
348
+
349
+ pstmt.setString(3, birthday);
350
+
351
+
352
+
353
+ pstmt.executeUpdate(); // INSERT文を実行
354
+
355
+ conn.close();
356
+
357
+ } catch (SQLException e) {
358
+
359
+ e.printStackTrace();
360
+
361
+ } catch (ClassNotFoundException e) {
362
+
363
+ } finally {
364
+
365
+ // 終了処理
366
+
367
+ // PreparedStatement終了
368
+
369
+ if (pstmt != null) {
370
+
371
+ try {
372
+
373
+ pstmt.close();
374
+
375
+ } catch (SQLException e) {
376
+
377
+ }
378
+
379
+ pstmt = null;
380
+
381
+ }
382
+
383
+ // Connection終了
384
+
385
+ if (conn != null) {
386
+
387
+ try {
388
+
389
+ conn.close();
390
+
391
+ } catch (SQLException e) {
392
+
393
+ }
394
+
395
+ conn = null;
396
+
397
+ }
398
+
399
+ }
400
+
401
+ }
402
+
403
+
404
+
405
+ ```
406
+
407
+
408
+
409
+ 最後に入力画面のServetです。
410
+
411
+
412
+
413
+ ```
414
+
415
+ /**
416
+
417
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
418
+
419
+ */
420
+
421
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
422
+
423
+ // HttpSession session = request.getSession();
424
+
425
+
426
+
427
+ request.setCharacterEncoding("UTF-8");
428
+
429
+ String Id = request.getParameter("Id");
430
+
431
+ String name = request.getParameter("name");
432
+
433
+ String name_hiragana = request.getParameter("nameHiragana");
434
+
435
+ String birthday = request.getParameter("birthday");
436
+
437
+
438
+
439
+
440
+
441
+ listDao dao = new listDao(); //Daoクラス生成
442
+
443
+ try {
444
+
445
+ dao.InsertInformation(employeeId, name, name_hiragana, birthday);
446
+
447
+ } catch (ClassNotFoundException e) {
448
+
449
+ // TODO 自動生成された catch ブロック
450
+
451
+ e.printStackTrace();
452
+
453
+ } catch (SQLException e) {
454
+
455
+ // TODO 自動生成された catch ブロック
456
+
457
+ e.printStackTrace();
458
+
459
+ } //挿入処理}
460
+
461
+ // session.setAttribute("Infomation", dao);
462
+
463
+
464
+
465
+ response.sendRedirect("./List");
466
+
467
+ }
468
+
469
+ }
470
+
471
+ ```
472
+
473
+
474
+
475
+ 以上になります。