2016-11-12 57 views
1

這是我使用spring-mvc實現的。正如我所觀察到的resultSetChildNameAccess.java中正常工作。當我在這裏打印聲明時,它會打印出我真正需要的選項。但下拉框重複結果集對象中的一個結果。任何人都可以跟蹤這個並告訴我我的jsp有什麼問題嗎?下拉選項菜單重複相同的選項

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; 
} 

}

+0

請提供您的數據庫值 – ScanQR

+0

我該怎麼辦?只是在這裏提到? – Hash

+0

我將需要什麼是您執行查詢時得到的結果集。另外爲什麼你的控制器方法是無效的它應該返回更新的模型對象,而不是使用現有的模型。 – ScanQR

回答

1

在你ChildNameAccess.java孩子以外的resultSet迭代定義,嘗試創建新的子對象的循環中每次循環的時間。因爲在Java中字符串是不可變的,而且你實際上不能改變字符串的值。意思是當你做child.setFirstName(resultSet.getString("FirstName")); 像明智的原始值的firstName字符串仍然在內存中,其他指向它的變量沒有改變。

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); 
      } 
+0

謝謝我沒有得到迄今爲止發生的事情。你的解釋清楚。 – Hash

1

您需要爲每個結果集行創建子對象。你不是在創造它。我已經修復它如下。

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; 
}} 
+0

是的,它的工作!但它重複兩次相同的名稱。這意味着它重複兩次不同的名稱(4個選項而不是兩個) – Hash

+1

這可能是因爲您沒有將新模型對象傳遞給客戶端視圖。您可以使用控制器方法更新現有模型。 – ScanQR