Reputation: 526
I have a method which returns an arraylist. But the Arraylsit is displaying its values twice. The code is-
ArrayList<Double> ref_jsp=new ArrayList<Double>();
public ArrayList<Double> refernece(String name) throws SQLException, ParseException {
String first=name.substring(1,19);
String last =name.substring(24,42);
List<Double> slist = new ArrayList<Double>(map1.keySet());
String s = StringUtils.join(slist, ',');
System.out.println("comma separated string"+s);
try
{
con = getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String vs2="SQL Query"
stmt.executeQuery(vs2);
rs1 = stmt.getResultSet();
while(rs1.next())
{
ref_jsp.add((rs1.getDouble(1)));
ref_jsp.add((rs1.getDouble(2)));
ref_jsp.add((rs1.getDouble(3)));
ref_jsp.add((rs1.getDouble(4)));
ref_jsp.add((rs1.getDouble(5)));
ref_jsp.add((rs1.getDouble(6)));
ref_jsp.add(rs1.getDouble(7));
ref_jsp.add(rs1.getDouble(8));
ref_jsp.add(rs1.getDouble(9));
ref_jsp.add(rs1.getDouble(10));
ref_jsp.add((rs1.getDouble(11)));
}
}
catch( Exception e )
{
System.out.println("\nException in reference "+e);
}
return ref_jsp;
}
My Sql query is fine.I checked it.The values returned by ref_jsp are displayed twice.Please explain where I'm going wrong??My jsp code foe displaying list values is-
<%
String ref_name= request.getParameter("ref_logtime");
pageContext.setAttribute("ref_name", ref_name);
ref.FindClosestToMultiplesOfTen(ref_name);
ref.refernece(ref_name);
%>
<table width = "300px" border = "1" cellspacing="2">
<tr><c:forEach var="r" items="${ref.refernece(param.ref_logtime)}">
<td><c:out value="${r}"></c:out></td>
</c:forEach></tr>
</table>
My output is of the form-
value-1 value-2 value-3 value-4 value-1 value-2 value-3 value-4
Upvotes: 3
Views: 1614
Reputation: 241
@Zeeshan pointed the mistake correctly. If you don't want to the make the arraylist being returned as a local member of the function and as you mentioned ref_jsp is being used in another method also, make sure to clear the elements of arraylist when all your associated methods are done operating on that list. Use ref_jsp.clear() for that.
Upvotes: 0
Reputation: 12421
It looks like problem is because of the following line in your java class.
ArrayList<Double> ref_jsp = new ArrayList<Double>();
You have declared ref_jsp
as class level variable, move it inside the method public ArrayList<Double> refernece(String name)
.
It is happening because you are calling your referenece
method twice from the jsp.
<%
String ref_name= request.getParameter("ref_logtime");
pageContext.setAttribute("ref_name", ref_name);
ref.FindClosestToMultiplesOfTen(ref_name);
ref.refernece(ref_name); //******* 1st call *********
%>
<table width = "300px" border = "1" cellspacing="2">
<tr><c:forEach var="r" items="${ref.refernece(param.ref_logtime)}">//***2nd call***
<td><c:out value="${r}"></c:out></td>
</c:forEach></tr>
</table>
During the 1st call suppose you got 10 items and those got added in your arraylist, and since that arraylist is a instance level variable, during the second call you again got 10 items and that got added along with the existing 10, hence you are having duplicate items.
Upvotes: 2