【问题标题】:How to create and use ArrayList using JSTL or JSP standard actions in same page [duplicate]如何在同一页面中使用 JSTL 或 JSP 标准操作创建和使用 ArrayList [重复]
【发布时间】:2016-01-08 13:15:12
【问题描述】:

我有一个连接到数据库、获取记录、存储在数组列表中并迭代该数组列表以获取记录的脚本代码。

以下是连接和获取arraylist记录的代码

<%
    String connectionURL = "jdbc:mysql://localhost:3306/stc";
    ArrayList<String> productname = new ArrayList<String>();
    ArrayList<String> productmodel = new ArrayList<String>();
    Connection connection = null;
    ResultSet rs = null;
    try
    {
        // Load the database driver
        Class.forName("com.mysql.jdbc.Driver");
        // Get a Connection to the database
        connection = DriverManager.getConnection(connectionURL, "root", "");
        //Add the data into the database
        PreparedStatement pst = connection.prepareStatement("SELECT * FROM tbl_products");
        rs= pst.executeQuery();
        while(rs.next())
        {
            productname.add(rs.getString("txtProduct_Name"));
            productmodel.add(rs.getString("txtModel_No"));

        }

    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    %>

以下代码遍历arraylist以显示记录

<select name="productname" id="productname">
            <option>Select Name</option>
            <%
                        for(int i=0;i<productname.size();i++)
                        {
                        %>
                        <option value="<%=productname.get(i) %>"><%=productname.get(i) %></option>
                        <%
                        }
            %>
            </select>

我想通过使用 JSTL 或 JSP 标准动作来实现所有这些功能。 请帮忙

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    上面的代码可以重构为使用 JSTL。在这种情况下可能需要的 JSTL 是 Core、SQL 标记。

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
    

    对于第一个代码 sn-p 使用 sql:setDataSource , sql:query 标签连接到数据库并执行查询。

     <sql:setDataSource var="demoDB" driver="com.mysql.jdbc.Driver"
             url="jdbc:mysql://localhost/demo"
             user=""  
             password=""/>
    
    <sql:query dataSource="${demoDB}" sql=" " var="queryResult" />
    
    • sql 属性将包含您的查询。

    使用对象 queryResult 上的c:forEach 标记迭代列表。

    请参考帖:Java ArrayList using in JSTL(<c:foreach>)

    【讨论】:

    • 谢谢,这正是我想要的。你能告诉我如何将变量“demoDB”放在应用程序范围内,这样我就不必在每个页面上都建立连接而可以使用变量吗?
    【解决方案2】:

    您可以使用jstl 中的foreach 来迭代ArrayList productname,如下所示:

       <c:forEach var='parameter' items='${productname}'>  
          <c:out value="${parameter.name}" /> 
        ...
       </c:forEach><br>
    

    你对 ArrayList productmodel 做同样的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2013-08-23
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      相关资源
      最近更新 更多