前提・実現したいこと
Webページで社員登録のテーブルを一覧表示したい
社員登録のテーブルの情報を、表示させたいのに、null表示されてる状態です
どのようにすればいいかご教授いただけないでしょうか。
発生している問題・エラーメッセージ
社員登録のテーブルの情報が入らずnullが表示されます
該当のソースコード
AccountBeans
public class AccountBeans { private String user_no; //no格納用変数 private String user_name; //名前格納用変数 private String post_cd; //役職格納用変数 public String getUser_no(){ return user_no; } public void setUser_no(String user_no){ this.user_no = user_no; } public String getUser_name(){ return user_name; } public void setUser_name(String user_name){ this.user_name = user_name; } public String getPost_cd(){ return post_cd; } public void setPost_cd(String post_cd){ this.post_cd = post_cd; } }
AccountDao1
public class AccountDao1 { //接続用の情報をフィールドに定数として定義 private static String RDB_DRIVE = "org.mariadb.jdbc.Driver"; private static String URL = "jdbc:mariadb://localhost:3306/"; private static String USER = "root"; private static String PASS = "comp0121"; //データベース接続を行うメソッド public static Connection getConnection(){ try{ Class.forName(RDB_DRIVE); Connection con = DriverManager.getConnection(URL, USER, PASS); return con; }catch(Exception e){ throw new IllegalStateException(e); } } //データベースから全てのアカウント情報の検索を行うメソッド //戻り値としてArrayList<AccountInfo>型の変数を利用 public ArrayList<AccountBeans> selectAll(){ //変数宣言 Connection con = null; Statement smt = null; //return用オブジェクトの生成 ArrayList<AccountBeans> list = new ArrayList<AccountBeans>(); //SQL文 String sql = "SELECT * FROM registration.mst_user"; try{ con = getConnection(); smt = con.createStatement(); //SQLをDBへ発行 ResultSet rs = smt.executeQuery(sql); //検索結果を配列に格納 while(rs.next()){ AccountBeans accountbeans =new AccountBeans(); accountbeans.setUser_no(rs.getString("user_no")); accountbeans.setUser_name(rs.getString("user_name")); accountbeans.setPost_cd(rs.getString("post_cd")); list.add(accountbeans); } }catch(Exception e){ throw new IllegalStateException(e); }finally{ //リソースの開放 if(smt != null){ try{smt.close();}catch(SQLException ignore){} } if(con != null){ try{con.close();}catch(SQLException ignore){} } } return list; } }
SelectServlet1
public class SelectServlet1 extends HttpServlet{ public void doGet(HttpServletRequest request ,HttpServletResponse response) throws ServletException ,IOException{ String error = ""; try{ //配列宣言 ArrayList<AccountBeans> list = new ArrayList<AccountBeans>(); //オブジェクト宣言 AccountDao1 objDao = new AccountDao1(); //全検索メソッドを呼び出し list = objDao.selectAll(); //検索結果を持ってlist.jspにフォワード request.setAttribute("list", list); }catch (IllegalStateException e) { error ="DB接続エラーの為、一覧表示はできませんでした。"; }catch(Exception e){ error ="予期せぬエラーが発生しました。<br>"+e; }finally{ request.setAttribute("error", error); request.getRequestDispatcher("/example/list1.jsp").forward(request, response); } } }
list1
<%@page contentType="text/html; charset=UTF-8"%> <%@page import="java.util.ArrayList,employee_registration.AccountBeans"%> <% ArrayList<AccountBeans> list = (ArrayList<AccountBeans>)request.getAttribute("list"); String error = (String)request.getAttribute("error"); %> <html> <head> <title>社員登録</title> </head> <body> <div style="text-align:center"> <h2 style="text-align:center">社員登録</h2> <hr style="height:3; background-color:#ffdacc" /> <br> <%= error %> <table style="margin:0 auto"> <tr> <th style="background-color:#ff7f50; width:100">No</th> <th style="background-color:#ff7f50; width:100">名前</th> <th style="background-color:#ff7f50; width:100">役職</th> </tr> <% if(list != null){ for(int i=0;i<list.size();i++){ %> <tr> <td style="text-align:center; width:100"><%= list.get(i).getUser_no() %></td> <td style="text-align:center; width:250"><%= list.get(i).getUser_name() %></td> <td style="text-align:center; width:100"><%= list.get(i).getPost_cd() %></td> </tr> <% } } %> </table> <br> </div> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>example</display-name> <servlet> <servlet-name>SelectServlet1Mapping</servlet-name> <servlet-class>employee_registration.SelectServlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>SelectServlet1Mapping</servlet-name> <url-pattern>/SelectServlet1</url-pattern> </servlet-mapping> </web-app>
試したこと
補足情報(FW/ツールのバージョンなど)
Eclipse Eclipse 4.5 Mars・jdk1.8.0_241・MariaDB Connector/J 2.5.2・MariaDB 5.5.62
まだ回答がついていません
会員登録して回答してみよう