【发布时间】: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 标准动作来实现所有这些功能。 请帮忙
【问题讨论】: