0

I want to add comma to string which is retrieved from database .

in below code i am getting employee from database whose job is teacher

here is my code

<td>
    <% 
       sql2 = "SELECT empname FROM users WHERE job = ? ";
       ps2 = connection.prepareStatement(sql2);
       ps2.setString(1, jobname);
       rs2 = ps2.executeQuery();

         while (rs2.next()){ 
              String emp= rs2.getString("empname");
     %>                        
      <%=emp%> 

    <% } %>
</td>
Developer Desk
  • 2,294
  • 8
  • 36
  • 77

3 Answers3

1

You can create on e ArrayList of empname

ArrayList<String> empList = new ArrayList<String>();
while (rs2.next())
{ 
   String emp= rs2.getString("empname");
   empList.add(emp);                  
}

Then you can use JSTL isLast()

<c:forEach items="${empList}" var="empName" varStatus="loop">
   <c:out value="${empName}" />
   <c:if test="${!loop.last}">,</c:if>
</c:forEach>    

You can use isLast() method of ResultSet but, I would recommend you not to use scriptlets in JSP
See how to avoid Java Code in JSP-Files?

For working with JSTL you just need to put jstl-1.2.jar in /WEB-INF/lib
and in JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  

Update answer to comment

it does not printing result.

Scripts are raw java embedded in the page code, and if you declare variables in your scripts, then they become local variables embedded in the page.

In contrast, JSTL works entirely with scoped attributes, either at page, request or session scope.
So, for using ArrayList empList created in Scriptlets you need to modify the code. see this answer

<%
 ArrayList<String> empList = new ArrayList<String>();
 while (rs2.next())
 { 
   String emp= rs2.getString("empname");
   empList.add(emp);                  
 }
 pageContext.setAttribute("empList", empList);//pageContext is implicit object available
%>  

then above mentioned JSTL code will work fine.

Related links

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

You can use rs2.isLast() to check if the current ResultSet is last record and do not add , in that case. This, however is costly because the JDBC driver will fetch ahead one row in order to determine whether the current row is the last row in the result set.

Uooo
  • 6,204
  • 8
  • 36
  • 63
Prashant
  • 190
  • 1
  • 9
0

This would be enough..

<% 
   sql2 = "SELECT empname FROM users WHERE job = ? ";
   ps2 = connection.prepareStatement(sql2);
   ps2.setString(1, jobname);
   rs2 = ps2.executeQuery();
   boolean b = rs2.last();
    int numberOfRecords = 0;
   if(b){
   numberOfRecords = rs.getRow();
   System.out.println(numberOfRecords);
     }
rs = stmt.executeQuery(sql);
 while (rs.next()){ 
      if(numberOfRecords>1)
      {
      String emp= rs.getString("bank")+",";

      }
      else
      {

      String emp=rs.getString("bank");

      }
      numberOfRecords--;
 %>                        
  <%=emp%> 

<% } %>
Bharath R
  • 1,491
  • 1
  • 11
  • 23