練習問題 解答
13.9 練習問題 解答
問1
➢ PracticeDao.java
6 | public class PracticeDao { |
9 | private static String RDB_DRIVE = "com.mysql.jdbc.Driver"; |
10 | private static String URL = "jdbc:mysql://localhost/practice_web_db"; |
11 | private static String USER = "root"; |
12 | private static String PASS = "root123"; |
15 | public static Connection getConnection(){ |
17 | Class.forName(RDB_DRIVE); |
18 | Connection con = DriverManager.getConnection(URL, USER, PASS); |
21 | throw new IllegalStateException(e); |
25 | //データベースから全てのデータの検索を行うメソッド |
26 | public ArrayList< PracticeInfo > selectAll(){ |
28 | Connection con = null; |
32 | ArrayList< PracticeInfo > list = new ArrayList< PracticeInfo >(); |
35 | String sql = "SELECT * FROM practice_table"; |
38 | con = getConnection(); |
39 | smt = con.createStatement(); |
42 | ResultSet rs = smt.executeQuery(sql); |
46 | PracticeInfo info =new PracticeInfo(); |
47 | info.setId(rs.getString("id")); |
48 | info.setName(rs.getString("name")); |
49 | info.setPrice(rs.getInt("price")); |
50 | info.setAuthor(rs.getString("author")); |
51 | info.setComment(rs.getString("comment")); |
56 | throw new IllegalStateException(e); |
60 | try{smt.close();}catch(SQLException ignore){} |
63 | try{con.close();}catch(SQLException ignore){} |
69 | //データベースから指定された1件のデータの検索を行うメソッド |
70 | public PracticeInfo selectById(String id){ |
72 | Connection con = null; |
76 | PracticeInfo info =new PracticeInfo(); |
79 | String sql = "SELECT * FROM practice_table WHERE id = '" + id + "'"; |
82 | con = getConnection(); |
83 | smt = con.createStatement(); |
86 | ResultSet rs = smt.executeQuery(sql); |
88 | //取得した結果をreturn用オブジェクトに格納する |
90 | info.setId(rs.getString("id")); |
91 | info.setName(rs.getString("name")); |
92 | info.setPrice(rs.getInt("price")); |
93 | info.setAuthor(rs.getString("author")); |
94 | info.setComment(rs.getString("comment")); |
97 | throw new IllegalStateException(e); |
101 | try{smt.close();}catch(SQLException ignore){} |
104 | try{con.close();}catch(SQLException ignore){} |
➢ PracticeInfo.java
3 | public class PracticeInfo { |
5 | private String id; //IDデータ格納用変数 |
6 | private String name; //名前データ格納用変数 |
7 | private int price; //価格データ格納用変数 |
8 | private String author; //著者データ格納用変数 |
9 | private String comment; //コメントデータ格納用変数 |
12 | public PracticeInfo() { |
21 | public String getId() { |
24 | public void setId(String id) { |
29 | public String getName() { |
32 | public void setName(String name) { |
37 | public int getPrice() { |
40 | public void setPrice(int price) { |
45 | public String getAuthor() { |
48 | public void setAuthor(String author) { |
53 | public String getComment() { |
56 | public void setComment(String comment) { |
57 | this.comment = comment; |
➢ Practice1301Servlet.java
6 | import javax.servlet.http.*; |
8 | public class Practice1301Servlet extends HttpServlet{ |
10 | public void doGet(HttpServletRequest request ,HttpServletResponse response) |
11 | throws ServletException ,IOException{ |
17 | ArrayList< PracticeInfo > list = new ArrayList< PracticeInfo >(); |
20 | PracticeDao objDao = new PracticeDao(); |
23 | list = objDao.selectAll(); |
25 | //検索結果を持ってpractice1301.jspへフォワード |
26 | request.setAttribute("list", list); |
28 | }catch (IllegalStateException e) { |
29 | error ="DB接続エラーの為、一覧表示はできませんでした。"; |
32 | error ="予期せぬエラーが発生しました。< br >"+e; |
35 | request.setAttribute("error", error); |
36 | request.getRequestDispatcher("/view/ch13exercise/practice1301.jsp").forward(request, response); |
➢ practice1301.jsp
1 | <%@page contentType="text/html; charset=UTF-8"%> |
2 | <%@page import="java.util.ArrayList,ch13exercise.PracticeInfo"%> |
4 | ArrayList< PracticeInfo > list = (ArrayList< PracticeInfo >)request.getAttribute("list"); |
5 | String error = (String)request.getAttribute("error"); |
9 | < title >practice1301</ title > |
12 | < div style = "text-align:center" > |
13 | < h2 style = "text-align:center" >一覧画面</ h2 > |
14 | < hr style = "height:3; background-color:#0000ff" /> |
18 | < table style = "border:1px solid; margin:0 auto" > |
20 | < th style = "background-color:#6666FF; width:100" >ID</ th > |
21 | < th style = "background-color:#6666FF; width:200" >名前</ th > |
25 | for(int i=0;i< list.size ();i++){ |
28 | < td style = "text-align:center; width:100" > |
29 | < A href="<%=request.getContextPath() %>/Practice1302Servlet?id=<%=list.get(i).getId()%>"> |
30 | <%= list.get(i).getId() %></ A > |
32 | < td style = "text-align:center; width:100" ><%= list.get(i).getName() %></ td > |
➢ Practice1302Servlet.java
5 | import javax.servlet.http.*; |
7 | public class Practice1302Servlet extends HttpServlet{ |
9 | public void doGet(HttpServletRequest request ,HttpServletResponse response) |
10 | throws ServletException ,IOException{ |
16 | String id = request.getParameter("id"); |
19 | PracticeInfo info = new PracticeInfo(); |
22 | PracticeDao objDao = new PracticeDao(); |
25 | info = objDao.selectById(id); |
27 | //検索結果を持ってpractice1302.jspへフォワード |
28 | request.setAttribute("info", info); |
30 | }catch (IllegalStateException e) { |
31 | error ="DB接続エラーの為、一覧表示はできませんでした。"; |
34 | error ="予期せぬエラーが発生しました。< br >"+e; |
37 | request.setAttribute("error", error); |
38 | request.getRequestDispatcher("/view/ch13exercise/practice1302.jsp").forward(request, response); |
➢ practice1302.jsp
1 | <%@page contentType="text/html; charset=UTF-8"%> |
2 | <%@page import="ch13exercise.PracticeInfo"%> |
4 | PracticeInfo info = (PracticeInfo)request.getAttribute("info"); |
5 | String error = (String)request.getAttribute("error"); |
9 | < title >practice1302</ title > |
12 | < div style = "align:center" > |
13 | < h2 style = "align:center" >詳細画面</ h2 > |
14 | < hr style = "height:3; background-color:#0000ff" /> |
18 | < table style = "border: 1px solid; margin:0 auto" > |
23 | < th style = "background-color:#6666FF; width:100" >ID</ th > |
24 | < td style = "align:center; width:200" ><%= info.getId() %></ td > |
27 | < th style = "background-color:#6666FF; width:100" >名前</ th > |
28 | < td style = "align:center; width:200" ><%= info.getName() %></ td > |
32 | < th style = "background-color:#6666FF; width:100" >価格</ th > |
33 | < td style = "align:center; width:200" ><%= info.getPrice() %></ td > |
36 | < th style = "background-color:#6666FF; width:100" >著者</ th > |
37 | < td style = "align:center; width:200" ><%= info.getAuthor() %></ td > |
40 | < th style = "background-color:#6666FF; width:100; height:50" >コメント</ th > |
41 | < td style = "align:center; width:200; height:50" ><%= info.getComment() %></ td > |
NEXT>> 第14章 トラブルシューティング