gpt4 book ai didi

java - Spring mvc-来自数据库的下拉框选项

转载 作者:行者123 更新时间:2023-12-01 09:16:39 26 4
gpt4 key购买 nike

这是我使用 spring mvc 创建下拉框实现的。这是有效的。但我需要获取数据库记录中属于 motherID="M-1" 的所有 child 。这给了我第一个属于 M-1 的 child 。

哪里需要修复?我是 spring mvc 的初学者。

my_children.jsp

<form method="post" >
<div class="div_box">
<select id="child_name" name="Child Name" >
<option value="top">Select your child</option>
<option value="">${firstName} ${lastName}</option>

</select>
<br>
<div align ="justify">
<button type="button" class="btn btn-success active">View Details</button>
</div>
</div>
</form>

Controller

@RequestMapping(value="/my_children", method = RequestMethod.GET)
public void viewMyChild(ModelMap modelMap) {
ChildNameAccess childNameDAO = new ChildNameAccess();
try{
Child child = childNameDAO.getChildDataByMotherId("M-1");


modelMap.addAttribute("firstName",child.getFirstName());
modelMap.addAttribute("lastName",child.getLastName());
}

catch(SQLException e) {
e.printStackTrace();
}
}

数据访问类

package com.emidwife.web.models.dataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;

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 Child getChildDataByMotherId(String motherID) throws SQLException {
connection.openConnection();
try{
ResultSet resultSet = connection.getData("SELECT * FROM childdetails WHERE MotherID=\'" + motherID + "\'");
resultSet.next();
//child.setChildId("1");
child.setMotherId(resultSet.getString("MotherID"));
child.setFirstName(resultSet.getString("FirstName"));
child.setLastName(resultSet.getString("LastName"));
//child.setDateOfBirth(resultSet.getDate("DOB"));

} catch (SQLException e) {
e.printStackTrace();
}
finally{
connection.closeConnection();
}

return child;
}

}

最佳答案

主要问题不在于 spring mvc。您必须从数据库执行正确的选择语句。如果您想通过“M-1”获取所有子级,您可以在 dataAccess 类中进行以下更改。

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();

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 child = new Child();
child.setMotherId(resultSet.getString("MotherID"));
child.setFirstName(resultSet.getString("FirstName"));
child.setLastName(resultSet.getString("LastName"));
children.add(child);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
connection.closeConnection();
}

return children;
}
}

而且您还必须像下面这样更改您的 Controller 。

@RequestMapping(value="/my_children", method = RequestMethod.GET)
public void viewMyChild(ModelMap modelMap) {
ChildNameAccess childNameDAO = new ChildNameAccess();
try{
List<Child> children = childNameDAO.getChildDataByMotherId("M-1");

modelMap.addAttribute("children ",children);
}

catch(SQLException e) {
e.printStackTrace();
}
}

和jsp:

<form method="post" >
<div class="div_box">
<select id="child_name" name="Child Name" >
<option value="top">Select your child</option>
<c:forEach items="${children}" var="child">
<option value="">${child.firstName} ${child.lastName}</option>
</c:forEach>
</select>
<br>
<div align ="justify">
<button type="button" class="btn btn-success active">View Details</button>
</div>
</div>
</form>

关于java - Spring mvc-来自数据库的下拉框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com