如何将放在ArrayList里面的数据输出到JSP页面的表格?

如题!谢谢高手指教!
ArrayList里面的数据是从数据库中动态抓取的,且数量可能成千上万笔,因此要考虑代码的效率问题,就是数据显示到页面的时间问题
拜请高手指教!最好给个参考代码

我用javaBean 写过这样的 也页面 就是从下拉框里选择表名 然后在当前页面输出 你看看
也是动态输出的 一个lib数据库 下面有三个表 分别是 reader book borrow

servlet代码如下
package com.bean;
import java.sql.*;
public class DataBaseQuery
{
String tableName="";
String result="";
public DataBaseQuery(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(Exception e){
e.printStackTrace();
}
}
public String getTableName()
{
return tableName;
}
public void setTableName(String tableName)
{
this.tableName = tableName;
}
public String getResult()
{

if(!tableName.equals("")){
Connection con=null;
Statement stm=null;
ResultSet rs=null;
StringBuffer buffer=new StringBuffer();
try{
con=DriverManager.getConnection("jdbc:sqlserver://localhost;databasename=lib;","sa","123456");
stm=con.createStatement();
String cmd="select * from "+tableName;
rs=stm.executeQuery(cmd);
int colNum=rs.getMetaData().getColumnCount();//表格列数
buffer.append("<table border=1 cellpadding=5>");
buffer.append("<tr>");
int i=1;
while(true){
String name=rs.getMetaData().getColumnName(i).toString();
buffer.append("<td>"+name+"</td>");
i++;
if(i>colNum) break;
}
buffer.append("</tr>");
while(rs.next()){
buffer.append("<tr>");
for(i=1;i<=colNum;i++){
buffer.append("<td>"+rs.getString(i)+"</td>");
}
buffer.append("</tr>");
}
buffer.append("</table>");

}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null&&stm!=null&&con!=null){
rs.close();
stm.close();
con.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
result=new String(buffer);
}
return result;
}
public void setResult(String result)
{
this.result = result;
}

}
jsp页面如下
<%@ page language="java" import="java.util.*,com.bean.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'showTable.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="" method="post">
<select name="tableSelected">
<option value="reader">读者信息</option>
<option value="book">书籍信息</option>
<option value="borrow">借阅信息</option>
</select>
<input type="submit" name="g" value="查询"/>
</form>
<jsp:useBean id="database" class="com.bean.DataBaseQuery" scope="request"></jsp:useBean>
<jsp:setProperty name="database" property="tableName" param="tableSelected"/>
<p>您选择的表是</p>
<jsp:getProperty name="database" property="tableName"/>
<p>查询到的数据是</p>
<jsp:getProperty name="database" property="result"/>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-29
代码效率还是,输出效率,用PV做搞多进程。如果输出不想太多用系统调用wait一下
第2个回答  推荐于2017-07-23
这是写的简单的购物车程序。你在JSP的页面<body></body>标签中写如下面的代码就ok了。
代码你修改一下就可以用了。o(∩_∩)o...
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.yxq.valuebean.GoodSingle" %>
<% ArrayList goodlist=(ArrayList)session.getAttribute("goodlist"); %>
<table border="1" width="450" rules="none" cellspacing="0" cellpadding="0">
<tr height="50"><td colspan="3" align="center">提供商品如下</td></tr>
<tr align="center" height=30 bgcolor="lightgrey">
<td>名称</td>
<td>价格(元/斤)</td>
<td>购买</td>
</tr>
<% if(goodlist==null||goodlist.size()==0){ %>
<tr height="100"><td colspan="3" align="center">没有商品可显示!</td></tr>
<%}
else {
for(int i=0;i<goodlist.size();i++){
GoodSingle single=(GoodSingle)goodlist.get(i);
%>
<tr height="50" align="center">
<td><%=single.getName() %></td>
<td><%=single.getPrice() %></td>
<td><a href="doCar?action=buy&id=<%=i%>">购买</a></td>
</tr>
<%}
} %>
</table>本回答被网友采纳
第3个回答  2010-12-29
直接用循环啊
很简单的东西啊

这中间要注意类型的转换

集合添加泛型的条件,就不用考虑类型的转换问题了