Hasini Silva
Hasini Silva

Reputation: 344

Dropdown option menu repeat same option

This is what I implemented using spring-mvc. As I observed resultSet in ChildNameAccess.java works properly. As I put print statement here it print what I really need as the options. But dropdown box repeats the one result in the resultset object. Could anybody trace this and tell me what's wrong with my jsp please?

controller.java

    @RequestMapping(value="/my_children", method = RequestMethod.GET) 
public void viewMyChild(Model model) { 
    ChildNameAccess childNameDAO = new ChildNameAccess(); 

    try{
        java.util.List<Child> children = childNameDAO.getChildDataByMotherId("M-2"); 
        model.addAttribute("children",children);

        System.out.println(children);
    }
    catch (SQLException e) {
        e.printStackTrace();
    }

}

my_children.jsp

<div class="container-fluid bg-2 text-center">
<form:form method="get" >
    <div class="div_box">
        <select>
            <option value="top" >Select child</option>
                <c:forEach items="${children}" var="children">
                    <option value="" >${children.firstName} ${children.lastName}</option>
                </c:forEach>
        </select>
            <br>

    <div align ="justify">
        <button type="button" onclick="location.href='/web/mother/my_child_details'" class="btn btn-success active">View Details</button>
    </div>
        </div>
</form:form> 

ChildNameAccess.java

package com.emidwife.web.models.dataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

import com.emidwife.web.models.entities.Child;
import com.emidwife.web.models.utilities.Database;


public class ChildNameAccess {
private Database connection = new Database();
Child child = new Child();

public List<Child> getChildDataByMotherId(String motherID) throws SQLException {

    connection.openConnection();

    List<Child> children = new ArrayList<Child>();
    try{
        ResultSet resultSet = connection.getData("SELECT * FROM childdetails WHERE MotherID=\'" + motherID + "\'");
            while(resultSet.next()){
                System.out.println(resultSet.getString("FirstName"));
            child.setChildId(resultSet.getString("ChildID"));//database column -->ChildID
            child.setMotherId(resultSet.getString("MotherID"));
            child.setFirstName(resultSet.getString("FirstName"));
            child.setLastName(resultSet.getString("LastName"));
            System.out.println(children);
            children.add(child);
            System.out.println(children);
        }
    } 
    catch (SQLException e) {
        e.printStackTrace();
    }
    finally{
        connection.closeConnection();
    }

    return children;
}

}

Upvotes: 1

Views: 62

Answers (2)

lpsandaruwan
lpsandaruwan

Reputation: 818

In your ChildNameAccess.java child is defined outside resultSet iteration, try creating new Child object inside the loop every time it loops. Because in Java strings are immutable, and you actually cannot change the value of a string. Means when you do child.setFirstName(resultSet.getString("FirstName")); like wise original value of the firstName String is still in the memory and the other variables which are pointed to it haven't changed.

while(resultSet.next()){
                    Child child = new Child();
                    child.setChildId(resultSet.getString("ChildID"));//database column -->ChildID
                    child.setMotherId(resultSet.getString("MotherID"));
                    child.setFirstName(resultSet.getString("FirstName"));
                    System.out.println(resultSet.getString("FirstName"));
                    child.setLastName(resultSet.getString("LastName"));

                    children.add(child);
            }

Upvotes: 1

ScanQR
ScanQR

Reputation: 3820

You need to create child object for each resultset row. You are not creating it. I have fixed it as follows.

 package com.emidwife.web.models.dataAccess;

 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.List;
 import java.util.ArrayList;

 import com.emidwife.web.models.entities.Child;
 import com.emidwife.web.models.utilities.Database;


 public class ChildNameAccess {
 private Database connection = new Database();
  Child child = null;

public List<Child> getChildDataByMotherId(String motherID) throws SQLException {

connection.openConnection();

List<Child> children = new ArrayList<Child>();
try{
    ResultSet resultSet = connection.getData("SELECT * FROM childdetails WHERE MotherID=\'" + motherID + "\'");
        while(resultSet.next()){
            child = new Child();
            System.out.println(resultSet.getString("FirstName"));
        child.setChildId(resultSet.getString("ChildID"));//database column -->ChildID
        child.setMotherId(resultSet.getString("MotherID"));
        child.setFirstName(resultSet.getString("FirstName"));
        child.setLastName(resultSet.getString("LastName"));
        System.out.println(children);
        children.add(child);
        System.out.println(children);
    }
} 
catch (SQLException e) {
    e.printStackTrace();
}
finally{
    connection.closeConnection();
}

return children;
}}

Upvotes: 1

Related Questions