如何在同一页面中使用 JSTL 或 JSP 标准操作创建和使用 ArrayList
How to create and use ArrayList using JSTL or JSP standard actions in same page
我有一个连接到数据库、获取记录、存储在数组列表中并迭代该数组列表以获取记录的 scriplet 代码。
以下是连接和获取数组列表记录的代码
<%
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();
}
}
}
%>
以下代码遍历数组列表以显示记录
<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 标准操作来实现所有这些功能。
请帮忙
您可以在 jstl
中使用 foreach
来迭代 ArrayList productname
,如下所示:
<c:forEach var='parameter' items='${productname}'>
<c:out value="${parameter.name}" />
...
</c:forEach><br>
你对 ArrayList 做同样的事情 productmodel
.
以上代码可以re-factored使用JSTL。在这种情况下可能需要的 JSTL 是核心,SQL 标记。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
对于第一个代码片段,使用 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>)
我有一个连接到数据库、获取记录、存储在数组列表中并迭代该数组列表以获取记录的 scriplet 代码。
以下是连接和获取数组列表记录的代码
<%
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();
}
}
}
%>
以下代码遍历数组列表以显示记录
<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 标准操作来实现所有这些功能。 请帮忙
您可以在 jstl
中使用 foreach
来迭代 ArrayList productname
,如下所示:
<c:forEach var='parameter' items='${productname}'>
<c:out value="${parameter.name}" />
...
</c:forEach><br>
你对 ArrayList 做同样的事情 productmodel
.
以上代码可以re-factored使用JSTL。在这种情况下可能需要的 JSTL 是核心,SQL 标记。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
对于第一个代码片段,使用 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>)