Java 如何从数据库动态填充我的 JSP 页面中的下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24074221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:00:31  来源:igfitidea点击:

How to dynamically populate the drop down list in my JSP page from the database?

javajqueryhtmlajaxjsp

提问by AKIWEB

I am working with JSPand Ajaxfor the first time. I am trying to get one column data from database and populate it in my drop down list in my JSP page using Ajax call. I don't want to refresh the page so that is the reason, I am making aN Ajax call.

我第一次JSP和我一起工作Ajax。我正在尝试从数据库中获取一列数据,并使用 Ajax 调用将其填充到我的 JSP 页面的下拉列表中。我不想刷新页面,所以这就是原因,我正在进行 Ajax 调用。

Here is my jsfiddlewhich has Process button and as soon as I click Process button, it will show an empty drop down list as of now. This is in my another test.jsppage.

这是我的jsfiddle,它有“处理”按钮,只要我单击“处理”按钮,它就会显示一个空的下拉列表。这是在我的另一test.jsp页。

I have a table as accountand I need to make this select query from the jsp -

我有一个表account,我需要从 jsp 中进行这个选择查询 -

SELECT USERS FROM ACCOUNT;

As soon as I am clicking Process button, I need to execute above SQL query on my POSTGRESQL database using Ajax. And whatever users, I am getting back from the database, I need to populate those USERSin my drop down list as shown in my above jsfiddle.

单击“处理”按钮后,我需要使用 Ajax 在我的 POSTGRESQL 数据库上执行上述 SQL 查询。无论用户是什么,我从数据库中返回,我都需要USERS在下拉列表中填充这些用户,如上面的 jsfiddle 所示。

Below is my JSP page (databasecall.jsp) in which I am making a call to my database to get all the USERS-

下面是我的 JSP 页面 (databasecall.jsp),我正在其中调用我的数据库以获取所有USERS-

<%@page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%
    response.setContentType("application/json");

    try {
        // Step 1. Load the JDBC driver
        Class.forName("org.postgresql.Driver");

        // Step 2. Create a Connection object
        Connection con = DriverManager.getConnection(
                "jdbc:postgresql://localhost/test","root", "root!");

        Statement s = con.createStatement();

        String sql ="SELECT USERS FROM ACCOUNT";
        ResultSet rs = s.executeQuery(sql);

        while (rs.next()) {
            // what to do here?
        }
        rs.close();
        s.close();
        con.close();
    } catch (Exception e3) {
        e3.printStackTrace();
    }
%>

Problem Statement:-

问题陈述:-

Now my question is, how do I populate all the USERSdata which I got from the database in my drop down list in the test.jsppage? Meaning, somehow I need to call this JSP on the Process button click and then pass all the users data which we got and then dynamically populate the drop down list?

现在我的问题是,如何USERStest.jsp页面的下拉列表中填充从数据库中获取的所有数据?意思是,我需要在 Process 按钮单击时调用这个 JSP,然后传递我们获得的所有用户数据,然后动态填充下拉列表?

Suppose if I am getting 10 USERS from the database, then the drop down list should have 10 users in it.

假设如果我从数据库中获得 10 个用户,那么下拉列表中应该有 10 个用户。

Is this possible to do?

这是可能的吗?

采纳答案by Masudul

As you get data through Ajax call so you should populate data on Servlet.

当您通过 Ajax 调用获取数据时,您应该在Servlet.

   @WebServlet("/populate")
   public class PopulateData extends HttpServlet{

      public void doGet(....){
         Class.forName("org.postgresql.Driver");
         Connection con = DriverManager.getConnection(
            "jdbc:postgresql://localhost/test","root", "root!");

        Statement s = con.createStatement();
        String sql ="SELECT USERS FROM ACCOUNT";
        ResultSet rs = s.executeQuery(sql);

        List<String> list = new ArrayList<String>();

        while (rs.next()) {
          list.add(rs.getString("USERS"));
        }
       String json = new Gson().toJson(list);
       response.getWriter().write(json);
      }
  }

Now you can populate json data to test.jsppage through ajax call.

现在您可以test.jsp通过 ajax 调用将 json 数据填充到页面。

See also:

也可以看看:

回答by Abhishek Mishra

Writing java code is jsp is very bad habit ,use jquery and do all DB stuff in java code

编写java代码是jsp是非常不好的习惯,使用jquery并在java代码中做所有DB的东西

$.ajax({
                type: "POST",
                url: "URL",
                data: "firstName=Aidy&lastName=F", // the data in form-encoded format, ie as it would appear on a querystring

                success: function (data) {
                   assign the return value which is in data to your hmtl 
                }
            });