今回は、「データベースアプリケーションの作成(8)―DELETEを使ってデータを削除する―」です。
 DELETEのSQLを使って、データを削除してみましょう。
■動画はこちら
■動画で使用しているソースコード
サーブレット(Sv4.java)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package yurufuwa.prog.sample; import java.io.IOException; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; public class Sv4 extends HttpServlet {     @Override     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {         //データ削除ページを表示         ServletContext sc = getServletContext();         RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/in.jsp");         rd.forward(req, resp);     }     @Override     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {         //入力パラメーターを取得         String kenCode = req.getParameter("txtKenCode");         //データの削除         Sakujyo t = new Sakujyo();         t.execute(kenCode);         int updateRows = t.getUpdateRows();         //結果をリクエストにセット         req.setAttribute("ken_code", kenCode);         req.setAttribute("update_rows", updateRows);         //結果を表示         ServletContext sc = getServletContext();         RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/out.jsp");         rd.forward(req, resp);     } } | 
削除処理(Sakujyo.java)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package yurufuwa.prog.sample; import java.sql.Connection; import java.sql.Statement; import javax.naming.InitialContext; import javax.sql.DataSource; public class Sakujyo {     private int updateRows = -1;     public void execute(String kenCode) {         Connection conn = null;         try {             //DBに接続             InitialContext ctx = new InitialContext();             DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");             conn = ds.getConnection();             //SQLを発行             Statement stmt = conn.createStatement();             updateRows = stmt.executeUpdate(                 "DELETE FROM todofuken "                 + "WHERE ken_code = '" + kenCode + "'"             );             stmt.close();         } catch(Exception e) {             e.printStackTrace();         } finally {             try {                 //接続を閉じる                 conn.close();             } catch(Exception e) {             }         }     }     public int getUpdateRows() {         return updateRows;     } } | 
JSP1(in.jsp)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>都道府県データ削除</title> </head> <body> <h2>削除するデータを入力してください</h2> <form action="./sv4" method="post">     <table>         <tr><td>都道府県コード</td><td><input type="text" name="txtKenCode" /></td></tr>     </table>     <input type="submit" value="削除" /> </form> </body> </html> | 
JSP2(out.jsp)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <% String kenCode = (String)request.getAttribute("ken_code"); int updateRows = (int)request.getAttribute("update_rows"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>都道府県データ削除</title> <style>table, th, td { border-collapse: collapse; border: 1px black solid; }</style> </head> <body> <h2>データの削除結果</h2> <h3>入力内容</h3> <table>     <tr><td>都道府県コード</td><td><%= kenCode %></td></tr> </table> <h3>削除の件数</h3> <%= updateRows %>件 </body> </html> | 
web.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0">   <display-name>testWeb</display-name>   <servlet>     <description></description>     <display-name>Sv4</display-name>     <servlet-name>Sv4</servlet-name>     <servlet-class>yurufuwa.prog.sample.Sv4</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>Sv4</servlet-name>     <url-pattern>/sv4</url-pattern>   </servlet-mapping> </web-app> | 
context.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0" encoding="UTF-8"?> <Context>      <Resource         name="jdbc/mysql"          auth="Container"         type="javax.sql.DataSource"         factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"         maxActive="5" maxIdle="5" initialSize="5"         username="yuruku" password="fuwatto"         driverClassName="com.mysql.cj.jdbc.Driver"         url="jdbc:mysql://localhost/yuruku"/> </Context> | 














