如何通过MySQL在JSP Servlet中创建多个从属下拉列表

时间:2019-03-23 09:06:56

标签: java jsp dropdown

我想通过mysql在jsp servlet中创建多个相关的下拉列表,包括国家和城市。

我已经根据列出的国家/地区编写了一些代码,但是我不知道如何根据所选国家/地区名称来划分子方。我该怎么办?

这是我到目前为止编写的代码。

国家(Pojo)

public class Country{
    private int countryId;
    private String name;
    @OneToMany(mappedBy="country", cascade=CascadeType.ALL)
    private List<State> states;

    public Country(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

状态

public class State{
    private int id;
    private String name;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "countryId", nullable = false)
    private Country country;

    public State(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

CountryDao类列出国家/地区

public class CountryDao {
    String databaseURL = "jdbc:mysql://localhost:3306/bookstoredb";
    String user = "root";
    String password = "pass";

    public List<Country> list() throws SQLException {
        List<Country> listCountry = new ArrayList<>();

        try (Connection connection = DriverManager.getConnection(databaseURL, user, password)) {
            String sql = "SELECT * FROM country ORDER BY name";
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery(sql);

            while (result.next()) {
                int id = result.getInt("country_id");
                String name = result.getString("name");
                Country country = new Country(id, name);

                listCountry.add(country );
            }          

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw ex;
        }      

        return listCountry;
    }
}

StateDao

public class StateDao {
    String databaseURL = "jdbc:mysql://localhost:3306/bookstoredb";
    String user = "root";
    String password = "pass";

    public List<State> list(int countryId) throws SQLException {
        List<State> listState = new ArrayList<>();

        try (Connection connection = DriverManager.getConnection(databaseURL, user, password)) {
            String sql = "SELECT * FROM STATE WHERE countryId=?";
            PrepareStatement statement = connection.prepareStatement();
            statement.getInt(1,countryId);
            ResultSet result = statement.executeQuery();

            while (result.next()) {
                int id = result.getInt("state_id");
                String name = result.getString("name");
                State state = new State(id, name);

                listState.add(state);
            }          

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw ex;
        }      

        return listState;
    }
} 

FormServlet

@WebServlet("/FormServlet")
public class FormServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        CountryDao dao = new CountryDao();

        try {

            List<Country> listCountry= dao.list();
            request.setAttribute("listCountry", listCountry);

            RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
            dispatcher.forward(request, response);

        } catch (SQLException e) {
            e.printStackTrace();
            throw new ServletException(e);
        }
    }
}

Jsp页面

<form action="FormServlet" method="post">
        Select a Country:&nbsp;
        <select name="category">
            <c:forEach items="${listCountry}" var="country">
                <option value="${country.id}"
                    <c:if test="${country.id eq selectedcountryId}">selected="selected"</c:if>
                    >
                    ${country.name}
                </option>
            </c:forEach>
        </select>
        <br/><br/>
        <input type="submit" value="Submit" />
</form>

0 个答案:

没有答案