練習問題 解答
13.9 練習問題 解答
問1
➢ PracticeDao.java[html] package ch13exercise; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class PracticeDao { //接続用の情報をフィールドに定数として定義 private static String RDB_DRIVE = “org.mariadb.jdbc.Driver”; private static String URL = “jdbc:mariadb://localhost/practice_web_db”; private static String USER = “root”; private static String PASS = “root123”; //データベース接続を行うメソッド public static Connection getConnection(){ try{ Class.forName(RDB_DRIVE); Connection con = DriverManager.getConnection(URL, USER, PASS); return con; }catch(Exception e){ throw new IllegalStateException(e); } } //データベースから全てのデータの検索を行うメソッド public ArrayList<PracticeInfo> selectAll(){ //変数宣言 Connection con = null; Statement smt = null; //return用オブジェクトの生成 ArrayList<PracticeInfo> list = new ArrayList<PracticeInfo>(); //SQL文 String sql = “SELECT * FROM practice_table”; try{ con = getConnection(); smt = con.createStatement(); //SQLをDBへ発行 ResultSet rs = smt.executeQuery(sql); //検索結果を配列に格納 while(rs.next()){ PracticeInfo info =new PracticeInfo(); info.setId(rs.getString(“id”)); info.setName(rs.getString(“name”)); info.setPrice(rs.getInt(“price”)); info.setAuthor(rs.getString(“author”)); info.setComment(rs.getString(“comment”)); list.add(info); } }catch(Exception e){ throw new IllegalStateException(e); }finally{ //リソースの開放 if(smt != null){ try{smt.close();}catch(SQLException ignore){} } if(con != null){ try{con.close();}catch(SQLException ignore){} } } return list; } //データベースから指定された1件のデータの検索を行うメソッド public PracticeInfo selectById(String id){ //変数宣言 Connection con = null; Statement smt = null; //return用オブジェクトを宣言 PracticeInfo info =new PracticeInfo(); //SQL文 String sql = “SELECT * FROM practice_table WHERE id = ‘” + id + “‘”; try{ con = getConnection(); smt = con.createStatement(); //SQLをDBへ発行 ResultSet rs = smt.executeQuery(sql); //取得した結果をreturn用オブジェクトに格納する if(rs.next()){ info.setId(rs.getString(“id”)); info.setName(rs.getString(“name”)); info.setPrice(rs.getInt(“price”)); info.setAuthor(rs.getString(“author”)); info.setComment(rs.getString(“comment”)); } }catch(Exception e){ throw new IllegalStateException(e); }finally{ //リソースの開放 if(smt != null){ try{smt.close();}catch(SQLException ignore){} } if(con != null){ try{con.close();}catch(SQLException ignore){} } } return info; } } [/html] ➢ PracticeInfo.java
[html] package ch13exercise; public class PracticeInfo { private String id; //IDデータ格納用変数 private String name; //名前データ格納用変数 private int price; //価格データ格納用変数 private String author; //著者データ格納用変数 private String comment; //コメントデータ格納用変数 public PracticeInfo() { this.id = “”; this.name = “”; this.price = 0; this.author = “”; this.comment = “”; } //変数idのアクセサメソッド public String getId() { return id; } public void setId(String id) { this.id = id; } //変数nameのアクセサメソッド public String getName() { return name; } public void setName(String name) { this.name = name; } //変数priceのアクセサメソッド public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } //変数authorのアクセサメソッド public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } //変数commentのアクセサメソッド public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } } [/html] ➢ Practice1301Servlet.java
[html] package ch13exercise; import java.io.IOException; import java.util.ArrayList; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @WebServlet(“/Practice1301Servlet”) public class Practice1301Servlet extends HttpServlet{ public void doGet(HttpServletRequest request ,HttpServletResponse response) throws ServletException ,IOException{ String error = “”; try{ //配列宣言 ArrayList<PracticeInfo> list = new ArrayList<PracticeInfo>(); //DAOオブジェクト宣言 PracticeDao objDao = new PracticeDao(); //全検索メソッドを呼び出し list = objDao.selectAll(); //検索結果を持ってpractice1301.jspへフォワード request.setAttribute(“list”, list); }catch (IllegalStateException e) { error =”DB接続エラーの為、一覧表示はできませんでした。”; }catch(Exception e){ error =”予期せぬエラーが発生しました。<br>”+e; }finally{ request.setAttribute(“error”, error); request.getRequestDispatcher(“/view/ch13exercise/practice1301.jsp”).forward(request, response); } } } [/html] ➢ practice1301.jsp
[html] <%@page contentType=”text/html; charset=UTF-8″%> <%@page import=”java.util.ArrayList,ch13exercise.PracticeInfo”%> <% ArrayList<PracticeInfo> list = (ArrayList<PracticeInfo>)request.getAttribute(“list”); String error = (String)request.getAttribute(“error”); %> <html> <head> <title>practice1301</title> </head> <body> <div style=”text-align:center”> <h2 style=”text-align:center”>一覧画面</h2> <hr style=”height:3; background-color:#0000ff” /> <br> <%= error %> <br> <table style=”border:1px solid; margin:0 auto”> <tr> <th style=”background-color:#6666FF; width:100″>ID</th> <th style=”background-color:#6666FF; width:200″>名前</th> </tr> <% if(list != null){ for(int i=0;i<list.size();i++){ %> <tr> <td style=”text-align:center; width:100″> <A href=”<%=request.getContextPath() %>/Practice1302Servlet?id=<%=list.get(i).getId()%>”> <%= list.get(i).getId() %></A> </td> <td style=”text-align:center; width:100″><%= list.get(i).getName() %></td> </tr> <% } } %> </table> <br> </div> </body> </html> [/html] ➢ Practice1302Servlet.java
[html] package ch13exercise; import java.io.IOException; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @WebServlet(“/Practice1302Servlet”) public class Practice1302Servlet extends HttpServlet{ public void doGet(HttpServletRequest request ,HttpServletResponse response) throws ServletException ,IOException{ String error = “”; try{ //パラメータの取得 String id = request.getParameter(“id”); //DTOオブジェクト宣言 PracticeInfo info = new PracticeInfo(); //DAOオブジェクト宣言 PracticeDao objDao = new PracticeDao(); //1件検索メソッドを呼び出し info = objDao.selectById(id); //検索結果を持ってpractice1302.jspへフォワード request.setAttribute(“info”, info); }catch (IllegalStateException e) { error =”DB接続エラーの為、一覧表示はできませんでした。”; }catch(Exception e){ error =”予期せぬエラーが発生しました。<br>”+e; }finally{ request.setAttribute(“error”, error); request.getRequestDispatcher(“/view/ch13exercise/practice1302.jsp”).forward(request, response); } } } [/html] ➢ practice1302.jsp
[html] <%@page contentType=”text/html; charset=UTF-8″%> <%@page import=”ch13exercise.PracticeInfo”%> <% PracticeInfo info = (PracticeInfo)request.getAttribute(“info”); String error = (String)request.getAttribute(“error”); %> <html> <head> <title>practice1302</title> </head> <body> <div style=”align:center”> <h2 style=”align:center”>詳細画面</h2> <hr style=”height:3; background-color:#0000ff” /> <br> <%= error %> <br> <table style=”border: 1px solid; margin:0 auto”> <% if(info != null){ %> <tr> <th style=”background-color:#6666FF; width:100″>ID</th> <td style=”align:center; width:200″><%= info.getId() %></td> </tr> <tr> <th style=”background-color:#6666FF; width:100″>名前</th> <td style=”align:center; width:200″><%= info.getName() %></td> </tr> <tr> <th style=”background-color:#6666FF; width:100″>価格</th> <td style=”align:center; width:200″><%= info.getPrice() %></td> </tr> <tr> <th style=”background-color:#6666FF; width:100″>著者</th> <td style=”align:center; width:200″><%= info.getAuthor() %></td> </tr> <tr> <th style=”background-color:#6666FF; width:100; height:50″>コメント</th> <td style=”align:center; width:200; height:50″><%= info.getComment() %></td> </tr> <% } %> </table> <br> </div> </body> </html> [/html]