EDIT: Discovered I was trying to format a number when I should have been formatting a date. Took a lot of code and work-arounds to create said date, but worked it out.
Sorry for the clutter, delete if necessary!
I'm trying to generate a string for each month in a school year working back from July with a wildcard as a Date to compare it in MySQL. My problem is that when I try to make the string, SimpleDateFormat doesn't seem to want to format the month, so I just end up getting records from January.
Here's the appropriate code bit:
As you can see, I put in some test c:outsCode:<c:forEach var="i" begin="0" end="6" step="1">
<c:set var="theMonth" value="${7-i}"/><c:out value="month: ${theMonth}"/></br>
<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfB = new SimpleDateFormat("MM");
String yearString = sdf.format(pageContext.getAttribute("endYear"));
String monthString = sdfB.format(pageContext.getAttribute("theMonth"));
String date = new String(yearString+"-"+monthString+"-__");%><c:out value="<%=date%>"/></br></br>
Here's the output:
So SimpleDateFormat seems to be formatting properly, but it just doesn't want to put the right number in. I even hard coded the number 12 into the format method and it still came out as 1. The year formatting is working fine. I also tried just doing the following:Code:month: 7
2012-01-__
month: 6
2012-01-__
month: 5
2012-01-__
month: 4
2012-01-__
month: 3
2012-01-__
month: 2
2012-01-__
month: 1
2012-01-__
month: 12
2012-01-__
month: 11
2012-01-__
month: 10
2012-01-__
month: 9
2012-01-__
... but the month variable is an Object so it won't assign to an Int and ParseInt doesnt work either.Code:int month = pageContext.getAttribute("month");
The 'theMonth' variable seems to be passing into the Java code properly, because I tried this:
... and it gave me an output of something like '2012-5-__'. The problem with this method is that I need the '0' before the '5' or it won't match the dates in my database, which is why I need to format the month.Code:String date = new String(yearString+"-"+pageContext.getAttribute("month")+"-__");%>
I'm pretty stumped, any help would be appreciated.
And please please PLEASE don't just post saying "You shouldn't be using JSP for blah blah blah blah", it's extremely annoying and not helpful at all. </grouchy old woman>
Here's the full code in case anyone was looking for it:
Code:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<html>
<sql:setDataSource
var = "CoolSchool"
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/CoolSchool"
scope = "session"
user = "root"
password = "dbpw"
/>
<body>
<table width="100%" border=1>
<tr><td colspan=2>
<%@include file = "header.jsp" %>
</td></tr>
<tr>
<td width = "20%" valign=top>
<%@include file = "studentNav.jsp" %>
</td>
<td width = "*">
<sql:query var="getYear" dataSource="${CoolSchool}">
SELECT start_year, end_year from current_year
WHERE current = 'y'
</sql:query>
<c:forEach items="${getYear.rows}" var="row">
<c:set var="startYear" value="${row.start_year}"/>
<c:set var="endYear" value="${row.end_year}"/>
</c:forEach>
<table>
<tr>
<td><b>Date</b></td>
<td><b>Teacher Involved</b></td>
<td><b>Problem</b></td>
<td><b>Measures Taken</b></td>
<td><b>Edit</b></td>
</tr>
<c:forEach var="i" begin="0" end="6" step="1">
<c:set var="theMonth" value="${7-i}"/><c:out value="month: ${theMonth}"/></br>
<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfB = new SimpleDateFormat("MM");
String yearString = sdf.format(pageContext.getAttribute("endYear"));
String monthString = sdfB.format(pageContext.getAttribute("theMonth"));
String date = new String(yearString+"-"+monthString+"-__");%><c:out value="<%=date%>"/></br></br>
<sql:query var="studentHealth" dataSource="${CoolSchool}">
SELECT * from health_record JOIN teacher ON health_record.teacher=teacher.teacher_id
WHERE date like ?
<sql:param value="<%=date%>"/>
</sql:query>
<c:forEach items="${studentHealth.rows}" var="row">
<tr>
<td><c:out value="${row.date}"/></td>
<td><c:out value="${row.first_name} ${row.second_name}"/></td>
<td><c:out value="${row.problem}"/></td>
<td><c:out value="${row.measures_taken}"/></td>
<td><form method=post action="editHealthRecord.jsp">
<input type="hidden" name="teacher" value="${row.teacher}">
<input type="hidden" name="date" value="${row.date}">
<input type="hidden" name="problem" value="${row.problem}">
<input type="hidden" name="measuresTaken" value="${row.measures_taken}">
<input type="hidden" name="student" value="${row.student}">
<input type="hidden" name="healthID" value="${row.health_id}">
<input type="submit" value="Edit">
</form></td>
</tr>
</c:forEach>
</c:forEach>
<c:forEach var="i" begin="0" end="3" step="1">
<c:set var="theMonth" value="${12-i}"/><c:out value="month: ${theMonth}"/></br>
<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfB = new SimpleDateFormat("MM");
String yearString = sdf.format(pageContext.getAttribute("endYear"));
String monthString = sdfB.format(pageContext.getAttribute("theMonth"));
String date = new String(yearString+"-"+monthString+"-__");%><c:out value="<%=date%>"/></br></br>
<sql:query var="studentHealth" dataSource="${CoolSchool}">
SELECT * from health_record JOIN teacher ON health_record.teacher=teacher.teacher_id
WHERE date like ?
<sql:param value="<%=date%>"/>
</sql:query>
<c:forEach items="${studentHealth.rows}" var="row">
<tr>
<td><c:out value="${row.date}"/></td>
<td><c:out value="${row.first_name} ${row.second_name}"/></td>
<td><c:out value="${row.problem}"/></td>
<td><c:out value="${row.measures_taken}"/></td>
<td><form method=post action="editHealthRecord.jsp">
<input type="hidden" name="teacher" value="${row.teacher}">
<input type="hidden" name="date" value="${row.date}">
<input type="hidden" name="problem" value="${row.problem}">
<input type="hidden" name="measuresTaken" value="${row.measures_taken}">
<input type="hidden" name="student" value="${row.student}">
<input type="hidden" name="healthID" value="${row.health_id}">
<input type="submit" value="Edit">
</form></td>
</tr>
</c:forEach>
</c:forEach>
</table>
</td>
</tr>
</table>
</body>
</html>

