発生している問題・エラーメッセージ
試験の成績表を出力するプログラムの作成中で、以下のテーブルを使用しています。
studentテーブル
student_id(生徒ID) | student_name(生徒名) | student_furigana(生徒カナ) | school_year(学年) | school_class(クラス) |
---|
subjectテーブル
subject_id(教科ID) | subject_name(教科名) |
---|
test_resultsテーブル
student_id(外部キー) | subject_id(外部キー) | score(得点) | test_day(試験日) |
---|
下記のような成績表を出力したいと考えています。
](77080b8ae922464ee958865e1c678e8c.png)
最初の成績表はうまく出力されているのですが、
2つ目以降の成績表は上部の学年・クラス・生徒ID・名前のみしか出力されていない状態になっております。
該当のソースコード
TestResultsRegistEditServlet.java
test-results-regist.jsp(後述)の検索押下時に実行
//インポート省略 public class TestResultsRegistEditServlet extends HttpServlet{ Connection conn = null; PreparedStatement ps = null; ResultSet result = null; ResultSet result2 = null; ResultSet preTestDayResult = null; String user = "suser"; String password = "spass"; String url = "jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ request.setCharacterEncoding("UTF-8"); String mode = request.getParameter("mode"); String status = "Success!"; boolean existRegistErr = false; String student_id = request.getParameter("student_id"); String student_name = request.getParameter("student_name"); String school_year = request.getParameter("school_year"); String school_class = request.getParameter("school_class"); String test_day = request.getParameter("test_day"); String subject_id = request.getParameter("subject_id"); String score = request.getParameter("score"); //中略 try { System.out.println(mode); Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); String preTestDaySql = "select distinct test_day from test_results order by test_day"; ps = conn.prepareStatement(preTestDaySql); ps.execute(); preTestDayResult = ps.executeQuery(); request.setAttribute("preTestDayResult", preTestDayResult); StringBuffer testResultsSql = new StringBuffer(); testResultsSql.append("select distinct student.school_year, student.school_class, student.student_id, student.student_name "); testResultsSql.append("from test_results inner join student on student.student_id = test_results.student_id inner join subject on subject.subject_id = test_results.subject_id "); testResultsSql.append("where test_day like '%"); testResultsSql.append(test_day + "%'"); StringBuffer sql2 = new StringBuffer(); sql2.append("select test_results.student_id, test_results.subject_id, subject.subject_name, score, test_day from test_results "); sql2.append("inner join student on student.student_id = test_results.student_id "); sql2.append("inner join subject on subject.subject_id = test_results.subject_id "); sql2.append("where test_day like '%"); sql2.append(test_day + "%'"); if(!school_year.equals("") && !school_class.equals("")) {//学年クラスともに入力済み testResultsSql.append("and student.student_id in (select student_id from student where school_year = "); testResultsSql.append(school_year); testResultsSql.append(" and school_class = "); testResultsSql.append(school_class); testResultsSql.append(") order by student.student_id, subject.subject_id"); sql2.append("and student.student_id in (select student_id from student where school_year = "); sql2.append(school_year); sql2.append(" and school_class = "); sql2.append(school_class); sql2.append(") order by student.student_id, subject.subject_id"); } else if(school_year.equals("") || school_class.equals("")) { if(school_year.equals("") && school_class.equals("")) { testResultsSql.append(" order by student.student_id, subject.subject_id"); sql2.append(" order by student.student_id, subject.subject_id"); } else if(!school_year.equals("")) { testResultsSql.append("and student.student_id in (select student_id from student where school_year = "); testResultsSql.append(school_year); testResultsSql.append(") order by student.student_id, subject.subject_id"); sql2.append("and student.student_id in (select student_id from student where school_year = "); sql2.append(school_year); sql2.append(") order by student.student_id, subject.subject_id"); } else if(!school_class.equals("")) { testResultsSql.append("and student.student_id in (select student_id from student where school_class = "); testResultsSql.append(school_class); testResultsSql.append(") order by student.student_id, subject.subject_id"); sql2.append("and student.student_id in (select student_id from student where school_class = "); sql2.append(school_class); sql2.append(") order by student.student_id, subject.subject_id"); } } ps = conn.prepareStatement(new String(testResultsSql)); System.out.println(ps); ps.execute(); result = ps.executeQuery(); ps = conn.prepareStatement(new String(sql2)); System.out.println(ps); ps.execute(); result2 = ps.executeQuery(); request.setAttribute("result", result); request.setAttribute("result2", result2); request.getRequestDispatcher("test-results-regist.jsp").forward(request, response); } catch(Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { System.out.println("MySQLのクローズに失敗しました。"); } } //以下省略
test-results-regist.jsp
//省略 <% ResultSet preStudentResult = (ResultSet) request.getAttribute("preStudentResult"); ResultSet preSubjectResult = (ResultSet) request.getAttribute("preSubjectResult"); ResultSet preTestDayResult = (ResultSet) request.getAttribute("preTestDayResult"); ResultSet result = (ResultSet) request.getAttribute("result"); ResultSet result2 = (ResultSet) request.getAttribute("result2"); String student_id = (String) request.getAttribute("student_id"); String student_name = (String) request.getAttribute("student_name"); String test_day = (String) request.getAttribute("test_day"); String school_year = (String) request.getAttribute("school_year"); String school_class = (String) request.getAttribute("school_class"); String score = (String)request.getAttribute("score"); %> //中略 <div id="search" class="tab-pane active"> <h2>試験結果検索</h2> <div class="input-form"> <form action="testResultsRegistEdit" method="post"> <table class="input-form-table"> <tr> <td>試験日</td> <td> <select name="test_day"> <option value="">すべての試験日</option> <% if(preTestDayResult != null) { %> <% while(preTestDayResult.next()) { %> <option value="<%=preTestDayResult.getString(1) %>"><%=preTestDayResult.getString(1) %></option> <% } %> <% } %> </select> </td> </tr> <tr> <td>学年</td> <td> <%if(school_year_searched != null) {%> <input type="text" id="year" name="school_year" maxlength='10' value="<%=school_year_searched %>" placeholder="学年(半角数字)"> <%} else {%> <input type="text" id="year" name="school_year" maxlength='10' placeholder="学年(半角数字)"> <%} %> </td> </tr> <tr> <td>クラス</td> <td> <%if(school_class_searched != null) {%> <input type="text" id="class" name="school_class" maxlength='10' value="<%=school_class_searched %>" placeholder="クラス(半角数字)"> <%} else {%> <input type="text" id="class" name="school_class" maxlength='10' placeholder="クラス(半角数字)"> <%} %> </td> </tr> </table> <div class="submit-button text-center"> <input type="hidden" name="mode" value="search"> <input type="submit" value="検索" > </div> </form> </div> <% ResultSet result = (ResultSet) request.getAttribute("result"); ResultSet result2 = (ResultSet) request.getAttribute("result2"); %> <%if(result != null && result2 != null) {%> <% while(result.next()) {%> <table class="table table-bordered search-result-table"> <thead> <tr> <th>学年</th> <th>クラス</th> <th>生徒ID</th> <th>名前</th> </tr> </thead> <tbody> <tr> <td><%=result.getString(1)%></td> <td><%=result.getString(2)%></td> <td><%=result.getString(3)%></td> <td><%=result.getString(4)%></td> </tr> </tbody> <thead> <tr> <th>科目</th> <th>得点</th> <th>試験日</th> <th>更新</th> <th>削除</th> </tr> </thead> <tbody> <%while(result2.next()) {%> <%if(result2.getString(1).equals(result.getString(3))) { %> <tr> <td><%=result2.getString(3) %></td> <td><%=result2.getString(4) %></td> <td><%=result2.getString(5) %></td> <td>更新</td> <td>削除</td> </tr> <%} %> <%} %> </tbody> </table> <%} %> <%} %> </div> //以下省略
回答2件
あなたの回答
tips
プレビュー