简体   繁体   English

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

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

I am working with JSP and Ajax for the first time. 我是第一次使用JSPAjax 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. 我试图从数据库中获取一列数据,并使用Ajax调用将其填充到我的JSP页面的下拉列表中。 I don't want to refresh the page so that is the reason, I am making aN Ajax call. 我不想刷新页面,所以这就是原因,我正在拨打a Ajax。

Here is my jsfiddle which has Process button and as soon as I click Process button, it will show an empty drop down list as of now. 这是我的jsfiddle ,它具有“处理”按钮,单击“处理”按钮后,它将立即显示一个空的下拉列表。 This is in my another test.jsp page. 这是在我的另一个test.jsp页面中。

I have a table as account and 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. 单击“处理”按钮后,我需要使用Ajax在POSTGRESQL数据库上执行上述SQL查询。 And whatever users, I am getting back from the database, I need to populate those USERS in my drop down list as shown in my above jsfiddle. 无论用户是什么,我都从数据库中恢复过来,我需要在下拉列表中填充这些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 USERS data which I got from the database in my drop down list in the test.jsp page? 现在的问题是,如何在test.jsp页面的下拉列表中填充从数据库获得的所有USERS数据? 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? 意思是,我需要以某种方式在“处理”按钮单击上调用此JSP,然后传递我们获得的所有用户数据,然后动态填充下拉列表?

Suppose if I am getting 10 USERS from the database, then the drop down list should have 10 users in it. 假设如果我从数据库中获得10个USERS,则下拉列表中应该有10个用户。

Is this possible to do? 这可能吗?

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.jsp page through ajax call. 现在,您可以通过ajax调用将json数据填充到test.jsp页面。

See also: 也可以看看:

Writing java code is jsp is very bad habit ,use jquery and do all DB stuff in java code 用jsp编写Java代码是非常不好的习惯,使用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 
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM