質問編集履歴

5

脱字

2020/06/16 01:11

投稿

c111000
c111000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -4,494 +4,4 @@
4
4
 
5
5
  ```
6
6
 
7
- Login.java
8
-
9
- package jp.co.sss.book.servlet;
10
-
11
-
12
-
13
- import javax.servlet.annotation.WebServlet;
14
-
15
- import javax.servlet.http.HttpServlet;
16
-
17
- import javax.servlet.http.HttpServletRequest;
18
-
19
- import javax.servlet.http.HttpServletResponse;
20
-
21
- import javax.servlet.http.HttpSession;
22
-
23
-
24
-
25
- import jp.co.sss.book.bean.BookUser;
26
-
27
- import jp.co.sss.book.dao.BookUserDAO;
28
-
29
-
30
-
31
- /**
32
-
33
- * ログイン用サーブレット
34
-
35
- *
7
+ L
36
-
37
- * @author system_shared
38
-
39
- */
40
-
41
- @WebServlet(urlPatterns = { "/login" })
42
-
43
- public class Login extends HttpServlet {
44
-
45
- public void doPost(HttpServletRequest request, HttpServletResponse response){
46
-
47
- try {
48
-
49
- String bookUserId = request.getParameter("bookUserId");
50
-
51
- String password = request.getParameter("password");
52
-
53
-
54
-
55
- BookUser bookUser = BookUserDAO.findByUserIDAndPassword(bookUserId, password);
56
-
57
-
58
-
59
- HttpSession session = request.getSession();
60
-
61
- session.setAttribute("bookuser",bookUser);
62
-
63
-
64
-
65
- if (bookUser != null) {
66
-
67
- request.getRequestDispatcher("/select/list.jsp").forward(request, response);
68
-
69
-
70
-
71
- } else {
72
-
73
- request.setAttribute("errMessage", "ユーザID、またはパスワードが間違っています。");
74
-
75
- request.getRequestDispatcher("/index.jsp").forward(request, response);
76
-
77
- }
78
-
79
-
80
-
81
- } catch (Exception e) {
82
-
83
- e.printStackTrace();
84
-
85
-
86
-
87
- }
88
-
89
- }
90
-
91
- }
92
-
93
- ```
94
-
95
- ```
96
-
97
- list.jsp
98
-
99
- <%@ page contentType="text/html; charset=UTF-8"%>
100
-
101
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
102
-
103
- <!DOCTYPE html>
104
-
105
- <html>
106
-
107
- <head>
108
-
109
- <meta charset="UTF-8">
110
-
111
- <link href="css/style.css" rel="stylesheet" type="text/css" />
112
-
113
- <title>書籍一覧</title>
114
-
115
- </head>
116
-
117
- <body>
118
-
119
- <%@ include file="../header.jsp"%>
120
-
121
- <div id="main">
122
-
123
-
124
-
125
- <h2>書籍一覧画面</h2>
126
-
127
-
128
-
129
- <form action="<%= request.getContextPath() %>/selectname" method="post">
130
-
131
- 書籍名:<input type="text" name="bookName"/>
132
-
133
- <input type="submit" value="検索"/><br/>
134
-
135
- </form>
136
-
137
-
138
-
139
- <form action="<%= request.getContextPath() %>/selectgenre" method="post">
140
-
141
- ジャンル名:
142
-
143
- <select name="genreId">
144
-
145
- <option value="1">文学</option>
146
-
147
- <option value="2">経済</option>
148
-
149
- <option value="3">技術</option>
150
-
151
- </select>
152
-
153
- <input type="submit" value="検索"/><br/>
154
-
155
- </form>
156
-
157
- <table border ="1">
158
-
159
- <tr>
160
-
161
- <th>書籍ID</th>
162
-
163
- <th>書籍名</th>
164
-
165
- <th>著者</th>
166
-
167
- <th>発行日</th>
168
-
169
- <th>在庫</th>
170
-
171
- <th>ジャンル名</th>
172
-
173
- </tr>
174
-
175
-
176
-
177
- <c:forEach var="book" items="${bookList }">
178
-
179
- <tr>
180
-
181
- <th>${book,book_id}</th>
182
-
183
- <th>${book,book_name}</th>
184
-
185
- <th>${book,author}</th>
186
-
187
- <th>${book,publication_date}</th>
188
-
189
- <th>${book,stock}</th>
190
-
191
- <th>${book,genre_Id}</th>
192
-
193
- </c:forEach>
194
-
195
- </table>
196
-
197
- </div>
198
-
199
- </body>
200
-
201
- </html>
202
-
203
- ```
204
-
205
- ```
206
-
207
- BookDAO.java
208
-
209
- package jp.co.sss.book.dao;
210
-
211
-
212
-
213
- import java.sql.Connection;
214
-
215
- import java.sql.PreparedStatement;
216
-
217
- import java.sql.ResultSet;
218
-
219
- import java.sql.SQLException;
220
-
221
- import java.util.ArrayList;
222
-
223
- import java.util.List;
224
-
225
-
226
-
227
- import jp.co.sss.book.bean.Book;
228
-
229
- import jp.co.sss.book.bean.Genre;
230
-
231
-
232
-
233
-
234
-
235
- public class BookDAO {
236
-
237
-
238
-
239
- //全件
240
-
241
- public static List<Book> findBookAll() throws SQLException{
242
-
243
- List<Book> bookList = new ArrayList<>();
244
-
245
-
246
-
247
- Connection con = DBManager.getConnection();
248
-
249
- PreparedStatement ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g ON b.genre_id = g.genre_id");
250
-
251
-
252
-
253
- ResultSet rs = ps.executeQuery();
254
-
255
-
256
-
257
- while (rs.next()) {
258
-
259
- Book book = new Book();
260
-
261
- book.setBookId(rs.getInt("book_id"));
262
-
263
- book.setBookName(rs.getString("book_name"));
264
-
265
- book.setAuthor(rs.getString("author"));
266
-
267
- book.setPublicatoindate(rs.getString("publicatoin_date"));
268
-
269
- book.setStock(rs.getInt("stock"));
270
-
271
- Genre genre = new Genre();
272
-
273
- genre.setGenreName(rs.getString("genre_name"));
274
-
275
- book.setGenre(genre);
276
-
277
-
278
-
279
- bookList.add(book);
280
-
281
-
282
-
283
- }
284
-
285
- DBManager.close(ps, con);
286
-
287
- return bookList;
288
-
289
- }
290
-
291
-
292
-
293
- //書籍名
294
-
295
- public static List<Book> findByBookName(String bookName) throws SQLException{
296
-
297
- Connection con = null;
298
-
299
- PreparedStatement ps = null;
300
-
301
- List<Book> bookList = new ArrayList<>();
302
-
303
-
304
-
305
- con = DBManager.getConnection();
306
-
307
- ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g"
308
-
309
- + " ON b.genre_id = g.genre_id WHERE genre_name LIKE ?");
310
-
311
- ps.setString(1,bookName);
312
-
313
- ResultSet rs = ps.executeQuery();
314
-
315
-
316
-
317
- while (rs.next()){
318
-
319
- Book book = new Book();
320
-
321
- book.setBookId(rs.getInt("book_id"));
322
-
323
- book.setBookName(rs.getString("book_name"));
324
-
325
- book.setAuthor(rs.getString("author"));
326
-
327
- book.setPublicatoindate(rs.getString("publicatoin_date"));
328
-
329
- book.setStock(rs.getInt("stock"));
330
-
331
- Genre genre = new Genre();
332
-
333
- genre.setGenreName(rs.getString("genre_name"));
334
-
335
- book.setGenre(genre);
336
-
337
-
338
-
339
- bookList.add(book);
340
-
341
- }
342
-
343
-
344
-
345
- DBManager.close(ps,con);
346
-
347
-
348
-
349
- return bookList;
350
-
351
- }
352
-
353
- //ジャンルID
354
-
355
- public static List<Book> findByGenreId(int genreId) throws SQLException{
356
-
357
- Connection con = null;
358
-
359
- PreparedStatement ps = null;
360
-
361
- List<Book> bookList = new ArrayList<>();
362
-
363
-
364
-
365
- con = DBManager.getConnection();
366
-
367
- ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g"
368
-
369
- + " ON b.genre_id = g.genre_id WHERE genre_id LIKE ?");
370
-
371
- ps.setInt(1,genreId);
372
-
373
- ResultSet rs = ps.executeQuery();
374
-
375
-
376
-
377
- while (rs.next()){
378
-
379
- Book book = new Book();
380
-
381
- book.setBookId(rs.getInt("book_id"));
382
-
383
- book.setBookName(rs.getString("book_name"));
384
-
385
- book.setAuthor(rs.getString("author"));
386
-
387
- book.setPublicatoindate(rs.getString("publicatoin_date"));
388
-
389
- book.setStock(rs.getInt("stock"));
390
-
391
- Genre genre = new Genre();
392
-
393
- genre.setGenreName(rs.getString("genre_name"));
394
-
395
- book.setGenre(genre);
396
-
397
-
398
-
399
- bookList.add(book);
400
-
401
- }
402
-
403
- DBManager.close(ps,con);
404
-
405
-
406
-
407
- return bookList;
408
-
409
- }
410
-
411
-
412
-
413
- }
414
-
415
-
416
-
417
-
418
-
419
- ```
420
-
421
- ```selectAll
422
-
423
- package jp.co.sss.book.servlet;
424
-
425
-
426
-
427
- import java.util.List;
428
-
429
-
430
-
431
- import javax.servlet.annotation.WebServlet;
432
-
433
- import javax.servlet.http.HttpServlet;
434
-
435
- import javax.servlet.http.HttpServletRequest;
436
-
437
- import javax.servlet.http.HttpServletResponse;
438
-
439
- import javax.servlet.http.HttpSession;
440
-
441
-
442
-
443
- import jp.co.sss.book.bean.Book;
444
-
445
- import jp.co.sss.book.dao.BookDAO;
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
- /**
454
-
455
- * Servlet implementation class Select
456
-
457
- */
458
-
459
- @WebServlet(urlPatterns = {"/SelectAll"})
460
-
461
- public class SelectAll extends HttpServlet {
462
-
463
- public void doGet(HttpServletRequest request, HttpServletResponse response) {
464
-
465
- doPost(request, response);
466
-
467
- }
468
-
469
-
470
-
471
- public void doPost(HttpServletRequest request, HttpServletResponse response){
472
-
473
- try{
474
-
475
- List<Book> bookList = BookDAO.findBookAll();
476
-
477
-
478
-
479
- HttpSession session = request.getSession();
480
-
481
- session.setAttribute("bookList", bookList);
482
-
483
- request.getRequestDispatcher("/select/list.jsp").forward(request, response);
484
-
485
-
486
-
487
- }catch (Exception e) {
488
-
489
- e.printStackTrace();
490
-
491
- }
492
-
493
- }
494
-
495
-
496
-
497
- ```

4

脱字

2020/06/16 01:11

投稿

c111000
c111000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  データベースからテーブルを持ってきてJSPで表示させたい(全件検索)です。ログインはできている前提で
2
2
 
3
- Login.javaからBookDAOを通しlist.jspでDBデータ表示させたいです。うまくデータ表示できません。どのエラーがでてしまうのかわからず、苦戦してます。ご教授いただければ幸いです
3
+ Login.javaからBookDAO、selectAllを通しlist.jspでDBデータ表示させたいです。うまくデータ表示できません。どのエラーがでてしまうのかわからず、苦戦してます。ご教授いただければ幸いです
4
4
 
5
5
  ```
6
6
 
@@ -417,3 +417,81 @@
417
417
 
418
418
 
419
419
  ```
420
+
421
+ ```selectAll
422
+
423
+ package jp.co.sss.book.servlet;
424
+
425
+
426
+
427
+ import java.util.List;
428
+
429
+
430
+
431
+ import javax.servlet.annotation.WebServlet;
432
+
433
+ import javax.servlet.http.HttpServlet;
434
+
435
+ import javax.servlet.http.HttpServletRequest;
436
+
437
+ import javax.servlet.http.HttpServletResponse;
438
+
439
+ import javax.servlet.http.HttpSession;
440
+
441
+
442
+
443
+ import jp.co.sss.book.bean.Book;
444
+
445
+ import jp.co.sss.book.dao.BookDAO;
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+ /**
454
+
455
+ * Servlet implementation class Select
456
+
457
+ */
458
+
459
+ @WebServlet(urlPatterns = {"/SelectAll"})
460
+
461
+ public class SelectAll extends HttpServlet {
462
+
463
+ public void doGet(HttpServletRequest request, HttpServletResponse response) {
464
+
465
+ doPost(request, response);
466
+
467
+ }
468
+
469
+
470
+
471
+ public void doPost(HttpServletRequest request, HttpServletResponse response){
472
+
473
+ try{
474
+
475
+ List<Book> bookList = BookDAO.findBookAll();
476
+
477
+
478
+
479
+ HttpSession session = request.getSession();
480
+
481
+ session.setAttribute("bookList", bookList);
482
+
483
+ request.getRequestDispatcher("/select/list.jsp").forward(request, response);
484
+
485
+
486
+
487
+ }catch (Exception e) {
488
+
489
+ e.printStackTrace();
490
+
491
+ }
492
+
493
+ }
494
+
495
+
496
+
497
+ ```

3

脱字

2020/06/11 12:03

投稿

c111000
c111000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- データベースからテーブルを持ってきてJSPで表示させたい(全件検索)です。
1
+ データベースからテーブルを持ってきてJSPで表示させたい(全件検索)です。ログインはできている前提で
2
2
 
3
3
  Login.javaからBookDAOを通しlist.jspでDBデータ表示させたいです。うまくデータ表示できません。どのエラーがでてしまうのかわからず、苦戦してます。ご教授いただければ幸いです
4
4
 

2

脱字

2020/06/11 11:52

投稿

c111000
c111000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,15 +1,105 @@
1
1
  データベースからテーブルを持ってきてJSPで表示させたい(全件検索)です。
2
2
 
3
- 流れとしてはindex.jspからログインし Login.java → list.jspでDBデータ表示させたいです。ログインまでは実装できたのですがうまくデータ表示できません。どのエラーがでてしまうのかわからず、苦戦してます。ご教授いただければ幸いです
3
+ Login.javaからBookDAOを通しlist.jspでDBデータ表示させたいです。うまくデータ表示できません。どのエラーがでてしまうのかわからず、苦戦してます。ご教授いただければ幸いです
4
-
5
-
6
-
4
+
7
- ```
5
+ ```
6
+
8
-
7
+ Login.java
8
+
9
+ package jp.co.sss.book.servlet;
10
+
11
+
12
+
13
+ import javax.servlet.annotation.WebServlet;
14
+
15
+ import javax.servlet.http.HttpServlet;
16
+
17
+ import javax.servlet.http.HttpServletRequest;
18
+
19
+ import javax.servlet.http.HttpServletResponse;
20
+
21
+ import javax.servlet.http.HttpSession;
22
+
23
+
24
+
25
+ import jp.co.sss.book.bean.BookUser;
26
+
27
+ import jp.co.sss.book.dao.BookUserDAO;
28
+
29
+
30
+
31
+ /**
32
+
33
+ * ログイン用サーブレット
34
+
35
+ *
36
+
37
+ * @author system_shared
38
+
39
+ */
40
+
41
+ @WebServlet(urlPatterns = { "/login" })
42
+
43
+ public class Login extends HttpServlet {
44
+
45
+ public void doPost(HttpServletRequest request, HttpServletResponse response){
46
+
47
+ try {
48
+
49
+ String bookUserId = request.getParameter("bookUserId");
50
+
51
+ String password = request.getParameter("password");
52
+
53
+
54
+
55
+ BookUser bookUser = BookUserDAO.findByUserIDAndPassword(bookUserId, password);
56
+
57
+
58
+
59
+ HttpSession session = request.getSession();
60
+
61
+ session.setAttribute("bookuser",bookUser);
62
+
63
+
64
+
65
+ if (bookUser != null) {
66
+
67
+ request.getRequestDispatcher("/select/list.jsp").forward(request, response);
68
+
69
+
70
+
71
+ } else {
72
+
73
+ request.setAttribute("errMessage", "ユーザID、またはパスワードが間違っています。");
74
+
75
+ request.getRequestDispatcher("/index.jsp").forward(request, response);
76
+
77
+ }
78
+
79
+
80
+
81
+ } catch (Exception e) {
82
+
83
+ e.printStackTrace();
84
+
85
+
86
+
87
+ }
88
+
89
+ }
90
+
91
+ }
92
+
93
+ ```
94
+
95
+ ```
96
+
9
- index.jsp
97
+ list.jsp
10
98
 
11
99
  <%@ page contentType="text/html; charset=UTF-8"%>
12
100
 
101
+ <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
102
+
13
103
  <!DOCTYPE html>
14
104
 
15
105
  <html>
@@ -20,458 +110,310 @@
20
110
 
21
111
  <link href="css/style.css" rel="stylesheet" type="text/css" />
22
112
 
23
- <title>ログイン画面</title>
113
+ <title>書籍一覧</title>
24
114
 
25
115
  </head>
26
116
 
27
117
  <body>
28
118
 
29
- <header>
30
-
31
- <h3>書籍一覧システム</h3>
119
+ <%@ include file="../header.jsp"%>
32
-
33
- </header>
34
120
 
35
121
  <div id="main">
36
122
 
123
+
124
+
37
- <p>ユーザーIDとパスワードを入力してください。</p>
125
+ <h2>書籍一覧画面</h2>
38
-
39
- <p class="error">${errMessage}</p>
126
+
40
-
127
+
128
+
41
- <form action="/book_list/login" method="post">
129
+ <form action="<%= request.getContextPath() %>/selectname" method="post">
42
-
130
+
43
- ユーザーID:<input type="text" name="bookUserId"><br>
131
+ 書籍名:<input type="text" name="bookName"/>
44
-
45
- パスワード:<input type="password" name="password"><br>
132
+
46
-
47
- <input type="submit" value="ログイン">
133
+ <input type="submit" value="検索"/><br/>
48
-
134
+
49
- </form>
135
+ </form>
136
+
137
+
138
+
139
+ <form action="<%= request.getContextPath() %>/selectgenre" method="post">
140
+
141
+ ジャンル名:
142
+
143
+ <select name="genreId">
144
+
145
+ <option value="1">文学</option>
146
+
147
+ <option value="2">経済</option>
148
+
149
+ <option value="3">技術</option>
150
+
151
+ </select>
152
+
153
+ <input type="submit" value="検索"/><br/>
154
+
155
+ </form>
156
+
157
+ <table border ="1">
158
+
159
+ <tr>
160
+
161
+ <th>書籍ID</th>
162
+
163
+ <th>書籍名</th>
164
+
165
+ <th>著者</th>
166
+
167
+ <th>発行日</th>
168
+
169
+ <th>在庫</th>
170
+
171
+ <th>ジャンル名</th>
172
+
173
+ </tr>
174
+
175
+
176
+
177
+ <c:forEach var="book" items="${bookList }">
178
+
179
+ <tr>
180
+
181
+ <th>${book,book_id}</th>
182
+
183
+ <th>${book,book_name}</th>
184
+
185
+ <th>${book,author}</th>
186
+
187
+ <th>${book,publication_date}</th>
188
+
189
+ <th>${book,stock}</th>
190
+
191
+ <th>${book,genre_Id}</th>
192
+
193
+ </c:forEach>
194
+
195
+ </table>
50
196
 
51
197
  </div>
52
198
 
53
- <%@ include file="footer.jsp"%>
54
-
55
199
  </body>
56
200
 
57
201
  </html>
58
202
 
59
-
60
-
61
- ```
62
-
63
- ```
64
-
65
- Login.java
66
-
67
- package jp.co.sss.book.servlet;
68
-
69
-
70
-
71
- import javax.servlet.annotation.WebServlet;
72
-
73
- import javax.servlet.http.HttpServlet;
74
-
75
- import javax.servlet.http.HttpServletRequest;
76
-
77
- import javax.servlet.http.HttpServletResponse;
78
-
79
- import javax.servlet.http.HttpSession;
80
-
81
-
82
-
83
- import jp.co.sss.book.bean.BookUser;
84
-
85
- import jp.co.sss.book.dao.BookUserDAO;
86
-
87
-
88
-
89
- /**
90
-
91
- * ログイン用サーブレット
92
-
93
- *
94
-
95
- * @author system_shared
96
-
97
- */
98
-
99
- @WebServlet(urlPatterns = { "/login" })
100
-
101
- public class Login extends HttpServlet {
102
-
103
- public void doPost(HttpServletRequest request, HttpServletResponse response){
104
-
105
- try {
106
-
107
- String bookUserId = request.getParameter("bookUserId");
108
-
109
- String password = request.getParameter("password");
110
-
111
-
112
-
113
- BookUser bookUser = BookUserDAO.findByUserIDAndPassword(bookUserId, password);
114
-
115
-
116
-
117
- HttpSession session = request.getSession();
118
-
119
- session.setAttribute("bookuser",bookUser);
120
-
121
-
122
-
123
- if (bookUser != null) {
124
-
125
- request.getRequestDispatcher("/select/list.jsp").forward(request, response);
126
-
127
-
128
-
129
- } else {
130
-
131
- request.setAttribute("errMessage", "ユーザID、またはパスワードが間違っています。");
132
-
133
- request.getRequestDispatcher("/index.jsp").forward(request, response);
203
+ ```
204
+
205
+ ```
206
+
207
+ BookDAO.java
208
+
209
+ package jp.co.sss.book.dao;
210
+
211
+
212
+
213
+ import java.sql.Connection;
214
+
215
+ import java.sql.PreparedStatement;
216
+
217
+ import java.sql.ResultSet;
218
+
219
+ import java.sql.SQLException;
220
+
221
+ import java.util.ArrayList;
222
+
223
+ import java.util.List;
224
+
225
+
226
+
227
+ import jp.co.sss.book.bean.Book;
228
+
229
+ import jp.co.sss.book.bean.Genre;
230
+
231
+
232
+
233
+
234
+
235
+ public class BookDAO {
236
+
237
+
238
+
239
+ //全件
240
+
241
+ public static List<Book> findBookAll() throws SQLException{
242
+
243
+ List<Book> bookList = new ArrayList<>();
244
+
245
+
246
+
247
+ Connection con = DBManager.getConnection();
248
+
249
+ PreparedStatement ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g ON b.genre_id = g.genre_id");
250
+
251
+
252
+
253
+ ResultSet rs = ps.executeQuery();
254
+
255
+
256
+
257
+ while (rs.next()) {
258
+
259
+ Book book = new Book();
260
+
261
+ book.setBookId(rs.getInt("book_id"));
262
+
263
+ book.setBookName(rs.getString("book_name"));
264
+
265
+ book.setAuthor(rs.getString("author"));
266
+
267
+ book.setPublicatoindate(rs.getString("publicatoin_date"));
268
+
269
+ book.setStock(rs.getInt("stock"));
270
+
271
+ Genre genre = new Genre();
272
+
273
+ genre.setGenreName(rs.getString("genre_name"));
274
+
275
+ book.setGenre(genre);
276
+
277
+
278
+
279
+ bookList.add(book);
280
+
281
+
134
282
 
135
283
  }
136
284
 
137
-
138
-
139
- } catch (Exception e) {
285
+ DBManager.close(ps, con);
140
-
286
+
141
- e.printStackTrace();
287
+ return bookList;
142
-
143
-
144
288
 
145
289
  }
146
290
 
291
+
292
+
293
+ //書籍名
294
+
295
+ public static List<Book> findByBookName(String bookName) throws SQLException{
296
+
297
+ Connection con = null;
298
+
299
+ PreparedStatement ps = null;
300
+
301
+ List<Book> bookList = new ArrayList<>();
302
+
303
+
304
+
305
+ con = DBManager.getConnection();
306
+
307
+ ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g"
308
+
309
+ + " ON b.genre_id = g.genre_id WHERE genre_name LIKE ?");
310
+
311
+ ps.setString(1,bookName);
312
+
313
+ ResultSet rs = ps.executeQuery();
314
+
315
+
316
+
317
+ while (rs.next()){
318
+
319
+ Book book = new Book();
320
+
321
+ book.setBookId(rs.getInt("book_id"));
322
+
323
+ book.setBookName(rs.getString("book_name"));
324
+
325
+ book.setAuthor(rs.getString("author"));
326
+
327
+ book.setPublicatoindate(rs.getString("publicatoin_date"));
328
+
329
+ book.setStock(rs.getInt("stock"));
330
+
331
+ Genre genre = new Genre();
332
+
333
+ genre.setGenreName(rs.getString("genre_name"));
334
+
335
+ book.setGenre(genre);
336
+
337
+
338
+
339
+ bookList.add(book);
340
+
147
- }
341
+ }
342
+
343
+
344
+
345
+ DBManager.close(ps,con);
346
+
347
+
348
+
349
+ return bookList;
350
+
351
+ }
352
+
353
+ //ジャンルID
354
+
355
+ public static List<Book> findByGenreId(int genreId) throws SQLException{
356
+
357
+ Connection con = null;
358
+
359
+ PreparedStatement ps = null;
360
+
361
+ List<Book> bookList = new ArrayList<>();
362
+
363
+
364
+
365
+ con = DBManager.getConnection();
366
+
367
+ ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g"
368
+
369
+ + " ON b.genre_id = g.genre_id WHERE genre_id LIKE ?");
370
+
371
+ ps.setInt(1,genreId);
372
+
373
+ ResultSet rs = ps.executeQuery();
374
+
375
+
376
+
377
+ while (rs.next()){
378
+
379
+ Book book = new Book();
380
+
381
+ book.setBookId(rs.getInt("book_id"));
382
+
383
+ book.setBookName(rs.getString("book_name"));
384
+
385
+ book.setAuthor(rs.getString("author"));
386
+
387
+ book.setPublicatoindate(rs.getString("publicatoin_date"));
388
+
389
+ book.setStock(rs.getInt("stock"));
390
+
391
+ Genre genre = new Genre();
392
+
393
+ genre.setGenreName(rs.getString("genre_name"));
394
+
395
+ book.setGenre(genre);
396
+
397
+
398
+
399
+ bookList.add(book);
400
+
401
+ }
402
+
403
+ DBManager.close(ps,con);
404
+
405
+
406
+
407
+ return bookList;
408
+
409
+ }
410
+
411
+
148
412
 
149
413
  }
150
414
 
415
+
416
+
417
+
418
+
151
- ```
419
+ ```
152
-
153
- ```
154
-
155
- list.jsp
156
-
157
- <%@ page contentType="text/html; charset=UTF-8"%>
158
-
159
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
160
-
161
- <!DOCTYPE html>
162
-
163
- <html>
164
-
165
- <head>
166
-
167
- <meta charset="UTF-8">
168
-
169
- <link href="css/style.css" rel="stylesheet" type="text/css" />
170
-
171
- <title>書籍一覧</title>
172
-
173
- </head>
174
-
175
- <body>
176
-
177
- <%@ include file="../header.jsp"%>
178
-
179
- <div id="main">
180
-
181
-
182
-
183
- <h2>書籍一覧画面</h2>
184
-
185
-
186
-
187
- <form action="<%= request.getContextPath() %>/selectname" method="post">
188
-
189
- 書籍名:<input type="text" name="bookName"/>
190
-
191
- <input type="submit" value="検索"/><br/>
192
-
193
- </form>
194
-
195
-
196
-
197
- <form action="<%= request.getContextPath() %>/selectgenre" method="post">
198
-
199
- ジャンル名:
200
-
201
- <select name="genreId">
202
-
203
- <option value="1">文学</option>
204
-
205
- <option value="2">経済</option>
206
-
207
- <option value="3">技術</option>
208
-
209
- </select>
210
-
211
- <input type="submit" value="検索"/><br/>
212
-
213
- </form>
214
-
215
- <table border ="1">
216
-
217
- <tr>
218
-
219
- <th>書籍ID</th>
220
-
221
- <th>書籍名</th>
222
-
223
- <th>著者</th>
224
-
225
- <th>発行日</th>
226
-
227
- <th>在庫</th>
228
-
229
- <th>ジャンル名</th>
230
-
231
- </tr>
232
-
233
-
234
-
235
- <c:forEach var="book" items="${bookList }">
236
-
237
- <tr>
238
-
239
- <th>${book,book_id}</th>
240
-
241
- <th>${book,book_name}</th>
242
-
243
- <th>${book,author}</th>
244
-
245
- <th>${book,publication_date}</th>
246
-
247
- <th>${book,stock}</th>
248
-
249
- <th>${book,genre_Id}</th>
250
-
251
- </c:forEach>
252
-
253
- </table>
254
-
255
- </div>
256
-
257
- </body>
258
-
259
- </html>
260
-
261
- ```
262
-
263
- ```
264
-
265
- BookDAO.java
266
-
267
- package jp.co.sss.book.dao;
268
-
269
-
270
-
271
- import java.sql.Connection;
272
-
273
- import java.sql.PreparedStatement;
274
-
275
- import java.sql.ResultSet;
276
-
277
- import java.sql.SQLException;
278
-
279
- import java.util.ArrayList;
280
-
281
- import java.util.List;
282
-
283
-
284
-
285
- import jp.co.sss.book.bean.Book;
286
-
287
- import jp.co.sss.book.bean.Genre;
288
-
289
-
290
-
291
-
292
-
293
- public class BookDAO {
294
-
295
-
296
-
297
- //全件
298
-
299
- public static List<Book> findBookAll() throws SQLException{
300
-
301
- List<Book> bookList = new ArrayList<>();
302
-
303
-
304
-
305
- Connection con = DBManager.getConnection();
306
-
307
- PreparedStatement ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g ON b.genre_id = g.genre_id");
308
-
309
-
310
-
311
- ResultSet rs = ps.executeQuery();
312
-
313
-
314
-
315
- while (rs.next()) {
316
-
317
- Book book = new Book();
318
-
319
- book.setBookId(rs.getInt("book_id"));
320
-
321
- book.setBookName(rs.getString("book_name"));
322
-
323
- book.setAuthor(rs.getString("author"));
324
-
325
- book.setPublicatoindate(rs.getString("publicatoin_date"));
326
-
327
- book.setStock(rs.getInt("stock"));
328
-
329
- Genre genre = new Genre();
330
-
331
- genre.setGenreName(rs.getString("genre_name"));
332
-
333
- book.setGenre(genre);
334
-
335
-
336
-
337
- bookList.add(book);
338
-
339
-
340
-
341
- }
342
-
343
- DBManager.close(ps, con);
344
-
345
- return bookList;
346
-
347
- }
348
-
349
-
350
-
351
- //書籍名
352
-
353
- public static List<Book> findByBookName(String bookName) throws SQLException{
354
-
355
- Connection con = null;
356
-
357
- PreparedStatement ps = null;
358
-
359
- List<Book> bookList = new ArrayList<>();
360
-
361
-
362
-
363
- con = DBManager.getConnection();
364
-
365
- ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g"
366
-
367
- + " ON b.genre_id = g.genre_id WHERE genre_name LIKE ?");
368
-
369
- ps.setString(1,bookName);
370
-
371
- ResultSet rs = ps.executeQuery();
372
-
373
-
374
-
375
- while (rs.next()){
376
-
377
- Book book = new Book();
378
-
379
- book.setBookId(rs.getInt("book_id"));
380
-
381
- book.setBookName(rs.getString("book_name"));
382
-
383
- book.setAuthor(rs.getString("author"));
384
-
385
- book.setPublicatoindate(rs.getString("publicatoin_date"));
386
-
387
- book.setStock(rs.getInt("stock"));
388
-
389
- Genre genre = new Genre();
390
-
391
- genre.setGenreName(rs.getString("genre_name"));
392
-
393
- book.setGenre(genre);
394
-
395
-
396
-
397
- bookList.add(book);
398
-
399
- }
400
-
401
-
402
-
403
- DBManager.close(ps,con);
404
-
405
-
406
-
407
- return bookList;
408
-
409
- }
410
-
411
- //ジャンルID
412
-
413
- public static List<Book> findByGenreId(int genreId) throws SQLException{
414
-
415
- Connection con = null;
416
-
417
- PreparedStatement ps = null;
418
-
419
- List<Book> bookList = new ArrayList<>();
420
-
421
-
422
-
423
- con = DBManager.getConnection();
424
-
425
- ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g"
426
-
427
- + " ON b.genre_id = g.genre_id WHERE genre_id LIKE ?");
428
-
429
- ps.setInt(1,genreId);
430
-
431
- ResultSet rs = ps.executeQuery();
432
-
433
-
434
-
435
- while (rs.next()){
436
-
437
- Book book = new Book();
438
-
439
- book.setBookId(rs.getInt("book_id"));
440
-
441
- book.setBookName(rs.getString("book_name"));
442
-
443
- book.setAuthor(rs.getString("author"));
444
-
445
- book.setPublicatoindate(rs.getString("publicatoin_date"));
446
-
447
- book.setStock(rs.getInt("stock"));
448
-
449
- Genre genre = new Genre();
450
-
451
- genre.setGenreName(rs.getString("genre_name"));
452
-
453
- book.setGenre(genre);
454
-
455
-
456
-
457
- bookList.add(book);
458
-
459
- }
460
-
461
- DBManager.close(ps,con);
462
-
463
-
464
-
465
- return bookList;
466
-
467
- }
468
-
469
-
470
-
471
- }
472
-
473
-
474
-
475
-
476
-
477
- ```

1

脱字

2020/06/11 11:51

投稿

c111000
c111000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,9 @@
60
60
 
61
61
  ```
62
62
 
63
+ ```
64
+
63
- ```Login.java
65
+ Login.java
64
66
 
65
67
  package jp.co.sss.book.servlet;
66
68
 
@@ -148,7 +150,9 @@
148
150
 
149
151
  ```
150
152
 
153
+ ```
154
+
151
- ```list.jsp
155
+ list.jsp
152
156
 
153
157
  <%@ page contentType="text/html; charset=UTF-8"%>
154
158
 
@@ -256,7 +260,9 @@
256
260
 
257
261
  ```
258
262
 
263
+ ```
264
+
259
- ```BookDAO.java
265
+ BookDAO.java
260
266
 
261
267
  package jp.co.sss.book.dao;
262
268