今回は、「データベースアプリケーションの作成(4)―SELECT文を使ってデータを検索―」です。
SELECT文のSQLを使って検索結果を取り出して、ブラウザでに表示してみましょう。
■動画はこちら
■動画で使用しているソースコード
サーブレット
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 | package yurufuwa.prog.sample; import java.io.IOException; import java.util.ArrayList; 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 Sv2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //都道府県名の一覧を取得 Kensaku k = new Kensaku(); k.execute(); ArrayList<String> kenCodeList = k.getKenCodeList(); ArrayList<String> kenNameList = k.getKenNameList(); ArrayList<String> yomiganaList = k.getYomiganaList(); //結果をリクエストにセットして、JSPにフォワード req.setAttribute("ken_code_list", kenCodeList); req.setAttribute("ken_name_list", kenNameList); req.setAttribute("yomigana_list", yomiganaList); ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/out.jsp"); rd.forward(req, resp); } } |
検索処理
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package yurufuwa.prog.sample; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import javax.naming.InitialContext; import javax.sql.DataSource; public class Kensaku { private ArrayList<String> kenCodeList = null; private ArrayList<String> kenNameList = null; private ArrayList<String> yomiganaList = null; public void execute() { kenCodeList = new ArrayList<String>(); kenNameList = new ArrayList<String>(); yomiganaList = new ArrayList<String>(); 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(); ResultSet rs = stmt.executeQuery("select * from todofuken"); //結果を取得 while(rs.next()) { kenCodeList.add(rs.getString(1)); kenNameList.add(rs.getString(2)); yomiganaList.add(rs.getString(3)); } rs.close(); stmt.close(); } catch(Exception e) { e.printStackTrace(); } finally { try { //接続を閉じる conn.close(); } catch(Exception e) { } } } public ArrayList<String> getKenCodeList() { return kenCodeList; } public ArrayList<String> getKenNameList() { return kenNameList; } public ArrayList<String> getYomiganaList() { return yomiganaList; } } |
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 29 30 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList" %> <% ArrayList<String> kenCodeList = (ArrayList<String>)request.getAttribute("ken_code_list"); ArrayList<String> kenNameList = (ArrayList<String>)request.getAttribute("ken_name_list"); ArrayList<String> yomiganaList = (ArrayList<String>)request.getAttribute("yomigana_list"); %> <!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> <table> <tr><th>都道府県コード</th><th>都道府県名</th><th>読み仮名</th></tr> <% for(int i=0 ; i<kenCodeList.size() ; i++) { %> <tr> <td><%= kenCodeList.get(i) %></td> <td><%= kenNameList.get(i) %></td> <td><%= yomiganaList.get(i) %></td> </tr> <% } %> </table> </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>Sv2</display-name> <servlet-name>Sv2</servlet-name> <servlet-class>yurufuwa.prog.sample.Sv2</servlet-class> </servlet> <servlet-mapping> <servlet-name>Sv2</servlet-name> <url-pattern>/sv2</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> |