QA@IT

SQLで取得した値をJSPで表示(Servlet)

13972 PV

DBとSQLに挑戦中です!
SQLで取得した値をサーブレットからJSPに表示するプロジェクトを作成しております。

画面は遷移しており、DBから値も取得できてるはずなのですが、JSPで値が表示されません。
何度も調べてみて、色々ためしてみましたが、どれもうまく行かず、 、、

初歩的な質問で申し訳ございません。
どなたかご教授頂けると助かります。。。
よろしくお願いいたします。

環境は
・JSP

・Java

・Tomcat

・MYSQL

です。

サーブレット

package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Member;
import dao.KHDataAccess;
import dao.SHDataAccess;

/**

  • Servlet implementation class MemberServlet
    */
    @WebServlet("/MemberServlet")
    public class MemberServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String seni = request.getParameter("cigmm");
    
    if (seni.equals("kohjun")) {
        KHDataAccess KH = new KHDataAccess();
        List<Member> memberlist = KH.execute();
        request.setAttribute("members",memberlist);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/member002.jsp");
        dispatcher.forward(request, response);
    
    }
    else if(seni.equals("shojun")) {
        SHDataAccess SH = new SHDataAccess();
        List<Member> memberlist = SH.execute();
        request.setAttribute("members",memberlist);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/member002.jsp");
        dispatcher.forward(request, response);
    }
    

    }

    }

dao

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import bean.Member;

public class KHDataAccess {

    String user = "***";
    String passwd = "****";
    String url = "jdbc:mysql://localhost:3306/test";

    public List<Member>execute() {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    List<Member> memberList = new ArrayList<Member>();

    try{//ドライバの読み込み
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url,user,passwd);

        //SQL文実行
        stmt = con.createStatement();

        //SELECT文の実行--昇順で取得
        String sql = "SELECT * FROM `cigmemberrist` ORDER BY `Department` ASC";
        rs = stmt.executeQuery(sql);
        //ResultSetからデータの取り出し
        while(rs.next()){
            String ID = rs.getString("ID");
            String Name = rs.getString("Name");
            String Department = rs.getString("Department");
            Member member = new Member(ID,Name,Department);
            memberList.add(member);
        }

        }catch(SQLException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
            if(rs !=null)rs.close();
            if(stmt !=null)stmt.close();
            if(con !=null)con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return memberList;

}

}

================================================================================================================

bean

package bean;

public class Member {
private String ID;
private String Name;
private String Department;

    public String getId() {
        return ID;
    }
    public void setId(String ID) {
        this.ID = ID;
    }
    public String getName() {
        return Name;
    }
    public void setName(String Name) {
        this.Name = Name;
    }
    public String getDs() {
        return Department;
    }
    public void setDs(String Department) {
        this.Department = Department;
    }

    public Member(String ID, String Name, String Department) {
        this.ID = ID;
        this.Name = Name;
        this.Department = Department;
    }
}

================================================================================================================

JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="bean.Member"%>
<% Listmemberlist=(List)request.getAttribute("members"); %>
<!DOCTYPE html>




CIGmember



株式会社CIG 従業員名簿


社員ID
氏名
部署


<% for (Member member: memberlist) {%>
<%=member.getId()%>
<%=member.getName()%>
<%=member.getDs()%>
<%} %>



ウォッチ

この質問への回答やコメントをメールでお知らせします。