---
# System prepended metadata

title: JV27 課題No.06
tags: [課題]

---

# JV27 課題No.06
###### tags: `課題`

### 安井版

:::spoiler class_bean.html
```htmlembedded=
<!--  -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>クラス名簿検索</title>
  </head>
  <body>
    <div class="">
      <div class="">
        <div class="">
          クラス名簿 - 検索
        </div>

        <form action="./servlet/class_servlet" method="POST">
          <div class="">
            <label class="">
              <input
                type="radio"
                name="SELECT"
                class=""
                value="1"
              />
              <span class=""></span>
              <span>クラス記号検索</span>
            </label>
          </div>
          <select
            name="CLASS_NO"
            class=""
          >
            <option value="AT11A192" selected>AT11A192</option>
            <option value="AT11B203">AT11B203</option>
            <option value="CG11A172">CG11A172</option>
            <option value="IT11A172">IT11A172</option>
            <option value="AT12A165">AT12A165</option>
            <option value="AT12B165">AT12B165</option>
            <option value="IH12A101">IH12A101</option>
            <option value="IW12A185">IW12A185</option>
            <option value="AT13A223">AT13A223</option>
            <option value="AP13A223">AP13A223</option>
            <option value="IH13A223">IH13A223</option>
            <option value="IW13A187">IW13A187</option>
            <option value="CD13A166">CD13A166</option>
            <option value="AP14A226">AP14A226</option>
            <option value="AT14A226">AT14A226</option>
            <option value="IH14A223">IH14A223</option>
            <option value="CT14A187">CT14A187</option>
            <option value="IW14A187">IW14A187</option>
            <option value="xx14Axx">xx14Axx</option>
          </select>
          <div class="">
            <label class="">
              <input
                type="radio"
                name="SELECT"
                class=""
                value="2"
              />
              <span class=""></span>
              <span>学年検索</span>
            </label>
          </div>
          <select
            name="GAKUNEN"
            class=""
          >
            <option value="1" selected>1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
          </select>
          <div class="">
            <a href="class_index.html" class=""
              >戻る</a
            >
            <button class="">検索</button>
          </div>
        </form>
      </div>
    </div>
  </body>
</html>
```
:::

:::spoiler class_servlet.java
```java=
// class_servlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class class_servlet extends HttpServlet {
  public void doPost (
    HttpServletRequest req,
    HttpServletResponse res )
    throws ServletException , IOException {
      req.setCharacterEncoding("UTF-8");
      res.setContentType("text/html;charset=UTF-8");

      String selectStr,gakunenStr,class_noStr;

      try {
        nhs00718.class_bean class_bean = new nhs00718.class_bean();
        selectStr = req.getParameter("SELECT");
        if (selectStr.equals("1")){
          class_noStr = req.getParameter("CLASS_NO");
          class_bean.setJyouken("class_no = '" + class_noStr + "'");
          class_bean.DBselect01();
        } else {
          gakunenStr = req.getParameter("GAKUNEN");
          class_bean.DBselect02();
          req.setAttribute("gakunenStr", gakunenStr);
        }
        
        
        
        req.setAttribute("class_bean", class_bean);
        ServletContext sc = getServletContext();
        if (selectStr.equals("1")){
        	sc.getRequestDispatcher("/class_bean1.jsp").forward(req, res);
        } else {
        	sc.getRequestDispatcher("/class_bean2.jsp").forward(req, res);
        }
      } 
      catch (Exception ex) {
        PrintWriter out;
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");
        out = res.getWriter();
        ex.printStackTrace(out);
      }
    }
}
```
:::

:::spoiler class_bean.java
```java=
// class_bean.java
package nhs00718;

import java.sql.*;
import java.io.*;
import java.util.*;

public class class_bean implements Serializable {

  final String URL = "jdbc:mysql://localhost/nhs00718db?useUnicode=true&characterEncoding=UTF-8";
  final String USER = "kurumi";
  final String PASSWORD = "rock3150617";
  final String DRIVER = "com.mysql.jdbc.Driver";

	Connection con = null;
	Statement stmt = null;

  int hint_flag;
  String class_no,gakuseki_no,syusseki_no,simei_1,simei_2,kana_1,kana_2,umare;
  String table = "class_table";
  String field = "*";
  String jyouken = "";
  String sort = "";
  ArrayList tbl = new ArrayList();

  public class_bean() {
    super();
  }

  // クラス記号検索の時に使用するDBselect
  public void DBselect01() {
    try {
      Class.forName(DRIVER);
      con = DriverManager.getConnection(URL,USER,PASSWORD);
      stmt = con.createStatement();
      Statement stmt = con.createStatement();
      
      StringBuffer query = new StringBuffer();
      query.append("select ");
      query.append(field);
      query.append(" from ");
      query.append(table);
      query.append(" where ");
      query.append(jyouken);
      query.append(" order by syusseki_no");
      ResultSet rs = stmt.executeQuery(query.toString());
      ResultSetMetaData rsmd = rs.getMetaData();
      System.out.println(query);

      if (rs.next() == true) {
        hint_flag = 1;
        do{
        	ArrayList row = new ArrayList();
        	for(int i=1; i<= rsmd.getColumnCount(); i++){
        		try{
        			row.add(rs.getObject(i).toString());
        		}
        		catch(Exception ex){
        			ex.printStackTrace();
        		}
        	}
        	tbl.add(row);
        }while(rs.next());
      } else {
        hint_flag = 0;
      }
      rs.close();
      stmt.close();
      con.close();
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  //　学年検索の時に使用するDBselect
  public void DBselect02() {
    try {
      Class.forName(DRIVER);
      con = DriverManager.getConnection(URL,USER,PASSWORD);
      stmt = con.createStatement();
      Statement stmt = con.createStatement();
      
      StringBuffer query = new StringBuffer();
      query.append("select ");
      query.append(field);
      query.append(" from ");
      query.append(table);
      query.append(" order by class_no,syusseki_no");
      ResultSet rs = stmt.executeQuery(query.toString());
      ResultSetMetaData rsmd = rs.getMetaData();
      System.out.println(query);

      if (rs.next() == true) {
        hint_flag = 1;
        do{
        	ArrayList row = new ArrayList();
        	for(int i=1; i<= rsmd.getColumnCount(); i++){
        		try{
        			row.add(rs.getObject(i).toString());
        		}
        		catch(Exception ex){
        			ex.printStackTrace();
        		}
        	}
        	tbl.add(row);
        }while(rs.next());
      } else {
        hint_flag = 0;
      }
      rs.close();
      stmt.close();
      con.close();
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void setJyouken(String inJyouken) {
    jyouken = inJyouken;
  }
  public int getHint_flag() {
    return hint_flag;
  }
  public ArrayList getTbl(){
	return tbl;  	
  }
}
```
:::

:::spoiler class_bean1.jsp
```java=
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" %> 
<% response.setContentType("text/html;charset=UTF-8"); %>

<html>
  <head>
    <title>名簿検索1</title>
  </head>
  <body>
    <jsp:useBean id="class_bean" scope="request" class="nhs00718.class_bean"/>
    <div class=''>
      <div class=''>
        名簿検索
      </div>
      <% if (class_bean.getHint_flag() == 1) { 
      	ArrayList tbl = class_bean.getTbl();
      	ArrayList row = (ArrayList)tbl.get(0);
      %>
      <div class=''>
        <table class=''>
          <thead>
            <tr>
              <th>
                クラス
              </th>
              <th>
                出席番号
              </th>
              <th>
                学籍番号
              </th>
              <th>
                氏名（姓）
              </th>
              <th>
                氏名（名）
              </th>
              <th>
                カナ（姓）
              </th>
              <th>
                カナ（名）
              </th>
              <th>
                生年月日
              </th>
            </tr>
          </thead>
          <tbody>
          <%
          	for(int i=0; i<tbl.size();i++){
          	row = (ArrayList)tbl.get(i);
          %>
          	<tr>
          <%
          		for(int j=0;j<row.size();j++){
          %>
              <td>
              	<%= (String)row.get(j) %>
              </td>
            <%
            		}
            %>
            	</tr>
            <%
            	}
            %>
          </tbody>
        </table>
      </div>
      <% } else { %>
        <h1 class="">該当なし</h1>
      <% } %>

      <div class=''>
          <a href='/JV27/class_bean.html'><button class=''>検索に戻る</button></a>
          <a href='/JV27/class_index.html'><button class=''>ホームに戻る</button></a>
      </div>
    </div>
  </body>
</html>
```
:::

:::spoiler class_bean2.jsp(オプション)
```java=
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" import="java.lang.*" import="java.io.*" %> 
<% response.setContentType("text/html;charset=UTF-8"); %>

<html>
  <head>
    <title>名簿検索2</title>
  </head>
  <body>
    <jsp:useBean id="class_bean" scope="request" class="nhs00718.class_bean"/>
    <div class=''>
      <div class=''>
        名簿検索
      </div>
      <% if (class_bean.getHint_flag() == 1) { 
      	ArrayList tbl = class_bean.getTbl();
      	ArrayList row = (ArrayList)tbl.get(0);
      	String gakunen = (String)request.getAttribute("gakunenStr");
      %>
      <div class=''>
        <table class=''>
          <thead>
            <tr>
              <th>
                クラス
              </th>
              <th>
                出席番号
              </th>
              <th>
                学籍番号
              </th>
              <th>
                氏名（姓）
              </th>
              <th>
                氏名（名）
              </th>
              <th>
                カナ（姓）
              </th>
              <th>
                カナ（名）
              </th>
              <th>
                生年月日
              </th>
            </tr>
          </thead>
          <tbody>
          <%
          	for(int i=0; i<tbl.size();i++){
          	row = (ArrayList)tbl.get(i);
          	// tmpには学籍番号(PI12A203)が入る
          	String tmp = (String)row.get(0);
          	// numは0の時レコードを表示しない/1の時レコードを表示する仕様。
          	int num = 0;
          	// numberには学籍番号の５桁目が入る。
          	String number = tmp.substring(3,4);
          	if(number.equals(gakunen)){
          		num = num + 1;
          	}
          		if(num == 1){
          %>
          	<tr>
          <%
          		for(int j=0;j<row.size();j++){
          %>
              <td>
              	<%= (String)row.get(j) %>
              </td>
            <%
            		}
            %>
            	</tr>
            <%
            	}
            	}
            %>
          </tbody>
        </table>
      </div>
      <% } else { %>
        <h1 class="">該当なし</h1>
      <% } %>

      <div class=''>
          <a href='/JV27/class_bean.html'><button class=''>検索に戻る</button></a>
          <a href='/JV27/class_index.html'><button class=''>ホームに戻る</button></a>
      </div>
    </div>
  </body>
</html>
```
:::

<br>
<br>

### 河原加筆版
:::spoiler class_bean.html
```htmlembedded=

<html>
<head>
  <title>区分選択</title>
  <meta http-equiv="content-type"
   content="text/html;charset=utf-8">

   <style type="text/css">

    input[type="radio"]{
      width: 20px;
    }
    s
   </style>

</head>
<body bgcolor="#fff">
  class_bean.html
  <center>
    <caption><font size="+3" color="#00f">
      <b>＜＜クラス名簿検索＞＞</b>
    </font></caption>
    <br><br>
    <br>
    <form method="post" action="/JV27/servlet/class_servlet">
      <table border='1' bordercolor='darkblue'>
      <tr>
        <td bgcolor='darkblue'>
          <font color='white'>
			選択
          </font>
        </td>
        <td bgcolor='darkblue'>
          <font color='white'>
			検索条件
          </font>
        </td>
      </tr>
      <tr>
        <td>
          <input type="radio" name="SELECT_ID" value="1" checked>
        </td>
        <td>
		   クラス記号&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <select name="CLASS_NO" size="1">
            <option value="AT11A192" selected>AT11A192</option>
            <option value="AT11B203">AT11B203</option>
            <option value="CG11A172">CG11A172</option>
            <option value="IT11A172">IT11A172</option>
            <option value="AT12A165">AT12A165</option>
            <option value="AT12B165">AT12B165</option>
            <option value="IH12A101">IH12A101</option>
            <option value="IW12A185">IW12A185</option>
            <option value="AT13A181">AT13A181</option>
            <option value="AP13A223">AP13A223</option>
            <option value="IH13A181">IH13A181</option>
            <option value="IW13A187">IW13A187</option>
            <option value="CT13A163">CT13A163</option>
            <option value="CD13A166">CD13A166</option>
            <option value="AP14A226">AP14A226</option>
            <option value="AT14A226">AT14A226</option>
            <option value="IH14A223">IH14A223</option>
            <option value="CT14A187">CT14A187</option>
            <option value="IW14A187">IW14A187</option>
            <option value="xx14Axxx">xx14Axxx</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>
          <input type="radio" name="SELECT_ID" value="2">
        </td>
        <td>
		     学年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;
          <select name="GAKUNEN_NO" size="1">
            <option value="1" selected="">1年</option>
            <option value="2">2年</option>
            <option value="3">3年</option>
            <option value="4">4年</option>
            <option value="9">その他</option>
          </select>
        </td>
      </tr>
      </table>
      <p>
        <input type="submit" value="検索">
      </p>
      <br><br>
    </form>
  </center>
    <hr>
    <a href="class_index.html">メニューへ戻る</a>
    <br>
</body>
</html>
```
:::

:::spoiler class_servlet.java
```java=

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class class_servlet extends HttpServlet {
  public void doPost (
    HttpServletRequest req,
    HttpServletResponse res )
    throws ServletException , IOException {
      req.setCharacterEncoding("UTF-8");
      res.setContentType("text/html;charset=UTF-8");

      String selectStr,gakunenStr,class_noStr;

      try {
        nhs00402.class_bean class_bean = new nhs00402.class_bean();
        selectStr = req.getParameter("SELECT_ID");
        if (selectStr.equals("1")){
          class_noStr = req.getParameter("CLASS_NO");
          class_bean.setJyouken("class_no = '" + class_noStr + "'");
          class_bean.DBselect();
        } else {
        	gakunenStr = req.getParameter("GAKUNEN_NO");
            class_bean.setJyouken("class_no LIKE '___" + gakunenStr + "____' ");
          class_bean.DBselect();
        }
        
        
        
        req.setAttribute("class_bean", class_bean);
        ServletContext sc = getServletContext();sc.getRequestDispatcher("/kadai1/class_bean1.jsp").forward(req, res);
      } 
      catch (Exception ex) {
        PrintWriter out;
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");
        out = res.getWriter();
        ex.printStackTrace(out);
      }
    }
}
```
:::

:::spoiler class_beans.java
```java=
package nhs00402;

import java.sql.*;
import java.io.*;
import java.util.*;

public class class_bean implements Serializable {

  final String URL = "jdbc:mysql://localhost/nhs00402db?useUnicode=true&characterEncoding=UTF-8";
  final String USER = "amika";
  final String PASSWORD = "1682hoheto";
  final String DRIVER = "com.mysql.jdbc.Driver";

	Connection con = null;
	Statement stmt = null;

  int hint_flag;
  String class_no,gakuseki_no,syusseki_no,simei_1,simei_2,kana_1,kana_2,umare;
  String table = "class_table";
  String field = "*";
  String jyouken = "";
  String sort = "";
  ArrayList tbl = new ArrayList();

  public class_bean() {
    super();
  }

  // クラス記号検索の時に使用するDBselect
  public void DBselect() {
    try {
      Class.forName(DRIVER);
      con = DriverManager.getConnection(URL,USER,PASSWORD);
      stmt = con.createStatement();
      Statement stmt = con.createStatement();
      
      StringBuffer query = new StringBuffer();
      query.append("select ");
      query.append(field);
      query.append(" from ");
      query.append(table);
      query.append(" where ");
      query.append(jyouken);
      query.append(" order by class_no,syusseki_no");
      ResultSet rs = stmt.executeQuery(query.toString());
      ResultSetMetaData rsmd = rs.getMetaData();
      System.out.println(query);

      if (rs.next() == true) {
        hint_flag = 1;
        do{
        	ArrayList row = new ArrayList();
        	for(int i=1; i<= rsmd.getColumnCount(); i++){
        		try{
        			row.add(rs.getObject(i).toString());
        		}
        		catch(Exception ex){
        			ex.printStackTrace();
        		}
        	}
        	tbl.add(row);
        }while(rs.next());
      } else {
        hint_flag = 0;
      }
      rs.close();
      stmt.close();
      con.close();
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public void setJyouken(String inJyouken) {
    jyouken = inJyouken;
  }
  public int getHint_flag() {
    return hint_flag;
  }
  public ArrayList getTbl(){
	return tbl;  	
  }
}

```
:::


:::spoiler class_bean1.jsp
```java=
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" %> 
<% response.setContentType("text/html;charset=UTF-8"); %>

<html>
  <head>
    <title>名簿検索1</title>
  </head>
  <body>
    <jsp:useBean id="class_bean" scope="request" class="nhs00402.class_bean"/>
    <center>
    <div class=''>
      <div class=''>
        <font size='+3' color='#00f'>
          <b>
 		          ＜＜名簿検索＞＞
 		  </b>
 		</font>
		<br><br><br>
		<font color='deeppink'>
		     検索結果
		</font>
      </div>
      <% if (class_bean.getHint_flag() == 1) { 
      	ArrayList tbl = class_bean.getTbl();
      	ArrayList row = (ArrayList)tbl.get(0);
      %>
      <div class=''>
        <table border='1' bordercolor='darkblue'>
          <thead>
            <tr>
              <th bgcolor='darkblue'>
              	<font color='white'>
                クラス
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                出席番号
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                学籍番号
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                氏名（姓）
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                氏名（名）
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                カナ（姓）
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                カナ（名）
                </font>
              </th>
              <th bgcolor='darkblue'>
              	<font color='white'>
                生年月日
                </font>
              </th>
            </tr>
          </thread>
          <tbody>
          <%
          	for(int i=0; i<tbl.size();i++){
          	row = (ArrayList)tbl.get(i);
          %>
          	<tr>
          <%
          		for(int j=0;j<row.size();j++){
          %>
              <td>
              	<%= (String)row.get(j) %>
              </td>
            <%
            		}
            %>
            	</tr>
            <%
            	}
            %>
          </tbody>
        </table>
      </div>
      <% } else { %>
        <h1 class="">該当なし</h1>
      <% } %>
      <br><br><br>
    </div>
    </center>
    
	    <hr><a href='/JV27/kadai1/class_bean.html'>
		名簿検索に戻る
	    </a>
		<a href='/JV27/kadai1/class_index.html'>
		ホームへ戻る
	    </a>
  </body>
</html>
```
:::








### 宮原加筆版

> https://github.com/miyasan31/java-servlet-tutorial

## 雑談 (0→やまぴ 1→みや 2→やすい 3→りょうくん 4→アミカ 5→やの 6→山田)









---
<span style="color: #ff3333">aaa</span>
<span style="text-decoration: underline">aaa</span>