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

标签 java spring jsp spring-mvc jdbc

这是我使用 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/

相关文章:

java - 多线程/并行化for循环JAVA的每一项

java - Spring Boot 在 jar 签名后不读取组件

java - Eclipse:工作区中项目的更改未对导入项目的文件夹进行更改

java - Spring Batch - 两个不同的批处理可以共享同一个元数据数据源吗?

java - Wildfly "Lazy-Inits"过滤器,我可以更改吗?

java - 如何使用 spring 管理的事务性 EntityManager 执行自定义 SQL 查询

java - 如何使用 session ID 创建 session 对象?

java - Select :multiple in Spring? 使用什么类型

java - 将整数添加到乘法表

jsp - 在 JSP 中转换和格式化日期