今回は、「データベースアプリケーションの作成(13)―トランザクション処理―」です。
データベースには、いろいろなデータ型があります。
数値、日付のデータ型について見てみましょう。
■動画はこちら
■動画で使用しているソースコード
※量が多いので、動画より抜粋して載せています。
欲しいコードがないようでしたら、youtubeのコメント欄へどうぞ。
数値
サーブレット(Sv8.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 | 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 Sv8 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { NumTable nt = new NumTable(); nt.select(); //結果をリクエストにセット req.setAttribute("rec_list_1", nt.getRecList1()); req.setAttribute("rec_list_2", nt.getRecList2()); //結果を表示 forwardJsp("/WEB-INF/jsp/out.jsp", req, resp); } catch(Exception e) { e.printStackTrace(); } } private void forwardJsp(String jspName, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(jspName); rd.forward(req, resp); } } |
NumTable.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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package yurufuwa.prog.sample; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import javax.naming.InitialContext; import javax.sql.DataSource; public class NumTable { private ArrayList<NumRecord> recList1 = null; private ArrayList<NumRecord> recList2 = null; public void select() throws Exception { Connection conn = null; try { //DBに接続 InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); conn = ds.getConnection(); //SQL1の発行 PreparedStatement pstmt1 = conn.prepareStatement( "SELECT * FROM num_table " + "WHERE num1 = ?" ); pstmt1.setInt(1, 315); ResultSet rs1 = pstmt1.executeQuery(); recList1 = new ArrayList<NumRecord>(); while(rs1.next()) { int i1 = rs1.getInt(1); BigDecimal b2 = rs1.getBigDecimal(2); NumRecord rec = new NumRecord( Integer.toString(i1), b2.toString() ); recList1.add(rec); } rs1.close(); pstmt1.close(); //SQL2の発行 BigDecimal bd = new BigDecimal("538.23"); PreparedStatement pstmt2 = conn.prepareStatement( "SELECT * FROM num_table " + "WHERE num2 = ?" ); pstmt2.setBigDecimal(1, bd); ResultSet rs2 = pstmt2.executeQuery(); recList2 = new ArrayList<NumRecord>(); while(rs2.next()) { int i1 = rs2.getInt(1); BigDecimal b2 = rs2.getBigDecimal(2); NumRecord rec = new NumRecord( Integer.toString(i1), b2.toString() ); recList2.add(rec); } rs2.close(); pstmt2.close(); } finally { try { //接続を閉じる conn.close(); } catch(Exception e) { } } } public ArrayList<NumRecord> getRecList1() { return recList1; } public ArrayList<NumRecord> getRecList2() { return recList2; } } |
NumRecord.java
1 2 3 4 5 | package yurufuwa.prog.sample; public record NumRecord(String num1,String num2) { } |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList"%> <%@ page import="yurufuwa.prog.sample.NumRecord"%> <% ArrayList<NumRecord> recList1 = (ArrayList<NumRecord>)request.getAttribute("rec_list_1"); ArrayList<NumRecord> recList2 = (ArrayList<NumRecord>)request.getAttribute("rec_list_2"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>num_tableのデータ</title> <style>table, th, td { border-collapse: collapse; border: 1px black solid; }</style> </head> <body> <h2>SQL1の実行結果</h2> <table> <tr> <th>num1</th> <th>num2</th> </tr> <% for(NumRecord rec : recList1) { %> <tr> <td><%= rec.num1() %></td> <td><%= rec.num2() %></td> </tr> <% } %> </table> <h2>SQL2の実行結果</h2> <table> <tr> <th>num1</th> <th>num2</th> </tr> <% for(NumRecord rec : recList2) { %> <tr> <td><%= rec.num1() %></td> <td><%= rec.num2() %></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>Sv8</display-name> <servlet-name>Sv8</servlet-name> <servlet-class>yurufuwa.prog.sample.Sv8</servlet-class> </servlet> <servlet-mapping> <servlet-name>Sv8</servlet-name> <url-pattern>/sv8</url-pattern> </servlet-mapping> </web-app> |
日付
サーブレット(Sv9.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 | 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 Sv9 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { DateTable dt = new DateTable(); dt.select(); //結果をリクエストにセット req.setAttribute("rec_list", dt.getRecList()); //結果を表示 forwardJsp("/WEB-INF/jsp/out.jsp", req, resp); } catch(Exception e) { e.printStackTrace(); } } private void forwardJsp(String jspName, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(jspName); rd.forward(req, resp); } } |
DateTable.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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package yurufuwa.prog.sample; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import javax.naming.InitialContext; import javax.sql.DataSource; public class DateTable { private ArrayList<DateRecord> recList = null; public void select() throws Exception { Connection conn = null; //引数の日時(Timestamp)を作成 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Timestamp ts = new Timestamp(sdf.parse("2024/01/02 23:45:12").getTime()); try { //DBに接続 InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); conn = ds.getConnection(); //SQLの発行 PreparedStatement pstmt = conn.prepareStatement( "SELECT * FROM date_table " + "WHERE d2 = ? " ); pstmt.setTimestamp(1, ts); ResultSet rs = pstmt.executeQuery(); recList = new ArrayList<DateRecord>(); while(rs.next()) { Date d1 = rs.getDate(1); Timestamp d2 = rs.getTimestamp(2); DateRecord rec = new DateRecord( d1.toString(), d2.toString() ); recList.add(rec); } rs.close(); pstmt.close(); } finally { try { //接続を閉じる conn.close(); } catch(Exception e) { } } } public ArrayList<DateRecord> getRecList() { return recList; } } |
DateRecord.java
1 2 3 4 5 | package yurufuwa.prog.sample; public record DateRecord(String d1,String d2) { } |
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 29 30 31 32 33 34 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList"%> <%@ page import="yurufuwa.prog.sample.DateRecord"%> <% ArrayList<DateRecord> recList = (ArrayList<DateRecord>)request.getAttribute("rec_list"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>date_tableのデータ</title> <style>table, th, td { border-collapse: collapse; border: 1px black solid; }</style> </head> <body> <h2>SQLの実行結果</h2> <table> <tr> <th>d1</th> <th>d2</th> </tr> <% for(DateRecord rec : recList) { %> <tr> <td><%= rec.d1() %></td> <td><%= rec.d2() %></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>Sv9</display-name> <servlet-name>Sv9</servlet-name> <servlet-class>yurufuwa.prog.sample.Sv9</servlet-class> </servlet> <servlet-mapping> <servlet-name>Sv9</servlet-name> <url-pattern>/sv9</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> |