練習問題 解答
13.9 練習問題 解答
問1
➢ PracticeDao.java
3 | import java.sql.Connection; |
4 | import java.sql.DriverManager; |
5 | import java.sql.ResultSet; |
6 | import java.sql.SQLException; |
7 | import java.sql.Statement; |
8 | import java.util.ArrayList; |
10 | public class PracticeDao { |
11 | //接続用の情報をフィールドに定数として定義 |
13 | private static String RDB_DRIVE = "org.mariadb.jdbc.Driver"; |
14 | private static String URL = "jdbc:mariadb://localhost/practice_web_db"; |
15 | private static String USER = "root"; |
16 | private static String PASS = "root123"; |
19 | public static Connection getConnection(){ |
21 | Class.forName(RDB_DRIVE); |
22 | Connection con = DriverManager.getConnection(URL, USER, PASS); |
25 | throw new IllegalStateException(e); |
29 | //データベースから全てのデータの検索を行うメソッド |
30 | public ArrayList< PracticeInfo > selectAll(){ |
32 | Connection con = null; |
36 | ArrayList< PracticeInfo > list = new ArrayList< PracticeInfo >(); |
39 | String sql = "SELECT * FROM practice_table"; |
42 | con = getConnection(); |
43 | smt = con.createStatement(); |
46 | ResultSet rs = smt.executeQuery(sql); |
50 | PracticeInfo info =new PracticeInfo(); |
51 | info.setId(rs.getString("id")); |
52 | info.setName(rs.getString("name")); |
53 | info.setPrice(rs.getInt("price")); |
54 | info.setAuthor(rs.getString("author")); |
55 | info.setComment(rs.getString("comment")); |
60 | throw new IllegalStateException(e); |
64 | try{smt.close();}catch(SQLException ignore){} |
67 | try{con.close();}catch(SQLException ignore){} |
73 | //データベースから指定された1件のデータの検索を行うメソッド |
74 | public PracticeInfo selectById(String id){ |
76 | Connection con = null; |
80 | PracticeInfo info =new PracticeInfo(); |
83 | String sql = "SELECT * FROM practice_table WHERE id = '" + id + "'"; |
86 | con = getConnection(); |
87 | smt = con.createStatement(); |
90 | ResultSet rs = smt.executeQuery(sql); |
92 | //取得した結果をreturn用オブジェクトに格納する |
94 | info.setId(rs.getString("id")); |
95 | info.setName(rs.getString("name")); |
96 | info.setPrice(rs.getInt("price")); |
97 | info.setAuthor(rs.getString("author")); |
98 | info.setComment(rs.getString("comment")); |
101 | throw new IllegalStateException(e); |
105 | try{smt.close();}catch(SQLException ignore){} |
108 | 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
3 | import java.io.IOException; |
4 | import java.util.ArrayList; |
6 | import jakarta.servlet.ServletException; |
7 | import jakarta.servlet.annotation.WebServlet; |
8 | import jakarta.servlet.http.HttpServlet; |
9 | import jakarta.servlet.http.HttpServletRequest; |
10 | import jakarta.servlet.http.HttpServletResponse; |
12 | @WebServlet("/Practice1301Servlet") |
13 | public class Practice1301Servlet extends HttpServlet{ |
14 | public void doGet(HttpServletRequest request ,HttpServletResponse response) |
15 | throws ServletException ,IOException{ |
21 | ArrayList< PracticeInfo > list = new ArrayList< PracticeInfo >(); |
24 | PracticeDao objDao = new PracticeDao(); |
27 | list = objDao.selectAll(); |
29 | //検索結果を持ってpractice1301.jspへフォワード |
30 | request.setAttribute("list", list); |
32 | }catch (IllegalStateException e) { |
33 | error ="DB接続エラーの為、一覧表示はできませんでした。"; |
36 | error ="予期せぬエラーが発生しました。< br >"+e; |
39 | request.setAttribute("error", error); |
40 | 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
3 | import java.io.IOException; |
5 | import jakarta.servlet.ServletException; |
6 | import jakarta.servlet.annotation.WebServlet; |
7 | import jakarta.servlet.http.HttpServlet; |
8 | import jakarta.servlet.http.HttpServletRequest; |
9 | import jakarta.servlet.http.HttpServletResponse; |
11 | @WebServlet("/Practice1302Servlet") |
12 | public class Practice1302Servlet extends HttpServlet{ |
13 | public void doGet(HttpServletRequest request ,HttpServletResponse response) |
14 | throws ServletException ,IOException{ |
20 | String id = request.getParameter("id"); |
23 | PracticeInfo info = new PracticeInfo(); |
26 | PracticeDao objDao = new PracticeDao(); |
29 | info = objDao.selectById(id); |
31 | //検索結果を持ってpractice1302.jspへフォワード |
32 | request.setAttribute("info", info); |
34 | }catch (IllegalStateException e) { |
35 | error ="DB接続エラーの為、一覧表示はできませんでした。"; |
38 | error ="予期せぬエラーが発生しました。< br >"+e; |
41 | request.setAttribute("error", error); |
42 | 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章 トラブルシューティング