数据库中的Spring mvc-下拉框选项

时间:2016-11-09 09:29:23

标签: java spring jsp spring-mvc jdbc

这是我使用spring mvc为create下拉框实现的。 这很有效。但我需要让所有孩子都属于数据库记录中的motherID="M-1"。这只给了我第一个属于M-1的孩子。

我必须修理的地方?我是春天的初学者。

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> 

控制器

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

dataAccess类

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

}

1 个答案:

答案 0 :(得分:1)

主要问题不在于spring mvc。您必须从数据库中执行正确的select语句。如果你想让所有的孩子都被M-1&#39;您可以在dataAccess类中进行以下更改。

NewFile1.txt
        Targets:
            target1
            target2
            target3
            ....... 

        Build Configurations:
            config1
            config2
            config3
            .......

        Schemes:
            schemeName1
            schemeName2 
            schemeName3
            ...........
            ...........

而且你必须改变你的控制器,如下所示。

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

和jsp:

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