I am somewhat new to JSP/Java world so don't flame me...
I am trying to generate a report of top 10 salespeople for each region
from an AS/400 data source.
It all works nicely except when I try to compare current region with
previous region. The If statement tells me the comparison with always
not equal even when I know it is!
I'm not sure if it is the IF statement or something to do with as400
data access and the rs.getString property. See anotated code below.
(I know that this is not an example of best practices, but I am just
trying to get my first simple example JSP/JDBC on Websphere working)
Comments welcome.
marty
<HTML>
<HEAD>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.ibm.as400.access.*"%>
<%
String connectionURL = "jdbc:as400://198.1.1.1";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>
</HEAD>
<BODY>
<H1>Blinds to Go</H1>
<H2>Top 10 DCs by region</H2>
</P>
<BR>
<BR>
<BR>
</P>
<br>
<FORM METHOD=POST ACTION="dataBaseAccess3.jsp">
Fiscal Week <INPUT type="text" name="YearWeek" size="20">
<INPUT type="hidden" name="loadClass" value="true">
<INPUT type="submit"
name="submit" value="submit">
</FORM>
(example 200448)
<%
if (request.getParameter("YearWeek") != null &&
Integer.parseInt(request.getParameter("YearWeek"))>0 )
{
String YearWeek = request.getParameter("YearWeek");
try{
if (request.getParameter("loadClass") != null) {
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
connection = DriverManager.getConnection(connectionURL, "login",
"password");
}
statement = connection.createStatement();
out.println("for Year/Week:");
out.println(YearWeek);
String queryString = "SELECT ##YRWK00 Year_Week, ASRE18
AssignedRegion, ASTR18 AssignedStore, ##ASSO00 DC, min(##DASS00)
DCName, max( REGS18 ) DupRegion, sum(##SALFWK00) WeeklySales,
sum(##SAL13W00) Sales13Week, sum(##VAL13W00) Value13Week,
sum(##DIS13W00) Discount13Week, sum(RRT318) RR_13Week, sum(PN3#18)
Blinds_13Week ,max( REGS18 ) , max(ASTR18) FROM BTGDTA010.DS18RPT00
WHERE ##YRWK00 = " + YearWeek + " GROUP BY ##YRWK00, ASRE18, ASTR18,
##ASSO00 ORDER BY ##YRWK00, ASRE18, WeeklySales DESC , ASTR18,
##ASSO00 ";
float ReturnRate;
float ReturnRateRounded ;
float count = 0;
String AssignedRegionHold = new String();
String AssignedRegionCurrent = new String();
String myString = new String();
rs = statement.executeQuery(queryString);
out.println("<TABLE border = 1> ");
out.println("<TR>");
out.println("<TD>Region </TD>");
out.println("<TD>Store </TD>");
out.println("<TD>Rep# </TD>");
out.println("<TD>Name </TD>");
out.println("<TD>R&R rate (%) </TD>");
out.println("<TD>Weekly Sales </TD>");
out.println("<TD>13 Week Sales </TD>");
out.println("<TD>13 Week Value </TD>");
out.println("<TD>13 Week Discount </TD>");
out.println("<TD>13 Week Sold </TD>");
out.println("<TD>13 Week R&R </TD>");
out.println("<TD>Sold in more than one Region</TD>");
out.println("</TR");
while (rs.next())
{
if (Integer.parseInt(rs.getString("Blinds_13Week")) > 0 )
{
ReturnRate = Float.parseFloat(rs.getString("RR_13Week"))*10 /
Float.parseFloat(rs.getString("Blinds_13Week"))*10;
ReturnRateRounded = (float)Math.round(ReturnRate * 10) / 10;
}
else
{
ReturnRate = 0;
ReturnRateRounded = 0;
}
AssignedRegionCurrent = rs.getString("AssignedRegion");
// -------------------------------------------------------------------
// here I see the current and previous regions, most of the time
they
// appear equal as expected.
// -------------------------------------------------------------------
System.out.println(AssignedRegionCurrent + " = current ");
System.out.println(AssignedRegionHold + " = hold ");
// -------------------------------------------------------------------
// here I compare current region with region on previous record
// for some reason, they are always different!! ,and the
// statement is always true.
// I wonder if AssignedRegionCurrent is the same data type
// as AssignedRegionHold?
// On the AS400 they are defined as 5 characters.
// -------------------------------------------------------------------
if(AssignedRegionCurrent != AssignedRegionHold)
{
count = 0;
}
if (count < 10)
{
count = count + 1;
if ( ReturnRateRounded == 10)
{
out.println("<TR bgcolor = yellow><TD>");
}
else
{
out.println("<TR><TD>");
}
out.println(rs.getString("AssignedRegion") + " </TD> <TD>
");
out.println(AssignedRegionHold + " </TD> <TD> ");
out.println(count + " </TD> <TD> ");
out.println(rs.getString("AssignedStore") + " </TD> <TD>
");
out.println(rs.getString("DC") + " </TD> <TD> ");
out.println(rs.getString("DCName") + " </TD> <TD> ");
out.println(ReturnRateRounded + " </TD> <TD> ");
out.println(rs.getString("WeeklySales") + " </TD> <TD> ");
out.println(rs.getString("Sales13Week") + " </TD> <TD> ");
out.println(rs.getString("Value13Week") + " </TD> <TD> ");
out.println(rs.getString("Discount13Week") + " </TD> <TD> ");
out.println(rs.getString("Blinds_13Week") + " </TD> <TD> ");
out.println(rs.getString("RR_13Week") + " </TD> <TD> ");
out.println(rs.getString("DupRegion") + " </TD> <TD>
");
out.println( "</TD></TR> ");
}
AssignedRegionHold = rs.getString("AssignedRegion");
}
out.println("</TABLE>");
out.println(queryString);
out.println("\n");
statement.close();
rs = null;
}catch(Exception e)
{
out.print(e.getMessage());
e.printStackTrace();
}
};
%>
</body>
</HTML
Dieter Bender - 30 Mar 2004 07:30 GMT
Marty,
String is an object, you have to use equals() for comparisons:
if (object1 == object2)
// only true if object1 and object2 hold the same reference
use instead:
if(object1.equals(object2))
BTW: your JSP doesn't look very nice. You should not mixup HTML and Java;
use taglibs instead and put your logic in java classes.
Dieter
> I am somewhat new to JSP/Java world so don't flame me...
>
[quoted text clipped - 200 lines]
> </body>
> </HTML
marty - 30 Mar 2004 15:06 GMT
thanks for your reply!!
I'll give it a try
marty