方法1:将数据集遍历 连接成一个字符串
String str = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("names");
String sex = rs.getString("sex");
String blood = rs.getString("blood");
String hobby = rs.getString("hobby");
long time = Long.valueOf(rs.getString("time"));
String date = df.format(time);
String pic = rs.getString("pic");
pic = pic.replace("\\", "/");
// 连接成一个字符串
str += "<tr>";
str += "<td>"+id+"</td>";
str += "<td>"+name+"</td>";
str += "<td>"+sex+"</td>";
str += "<td>"+blood+"</td>";
str += "<td>"+hobby+"</td>";
str += "<td>"+date+"</td>";
str += "<td>"+pic+"</td>";
str += "</tr>";
}
request.setAttribute("stu_list", str); //将字符串发送到JSPJSP端:
<table>
<tr>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>血型</td>
<td>爱好</td>
<td>发布时间</td>
<td>图片</td>
</tr>
${stu_list}
</table>方法2: 遍历数据集先到数组,再将数组到集合
String str = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<String[]> result = new ArrayList<String[]>();
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("names");
String sex = rs.getString("sex");
String blood = rs.getString("blood");
String hobby = rs.getString("hobby");
long time = Long.valueOf(rs.getString("time"));
String date = df.format(time);
String pic = rs.getString("pic");
pic = pic.replace("\\", "/");
// 连接成一个字符串
//将信息添加到数组中, 再将数组添加到List集合中
String[] row = new String[7];
row[0] = String.valueOf(id);
row[1] = name;
row[2] = sex;
row[3] = blood;
row[4] = hobby;
row[5] = date;
row[6] = pic;
result.add(row);
}
request.setAttribute("result",result); //将List集合发送到JSPJSP端:
<table>
<tr>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>血型</td>
<td>爱好</td>
<td>发布时间</td>
<td>图片</td>
</tr>
<%
List<String[]> result = (List<String[]>)request.getAttribute("result");
for(String[] row:result){ %>
<tr>
<td><%out.println(row[0]); %></td>
<td><%out.println(row[1]); %></td>
<td><%out.println(row[2]); %></td>
<td><%out.println(row[3]); %></td>
<td><%out.println(row[4]); %></td>
<td><%out.println(row[5]); %></td>
<td><%out.println(row[6]); %></td>
</tr>
<%} %>
</table>方法3: 遍历数据先到Map集合,再将May添加到List
String str = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Map<String,Object>> result1 = new ArrayList<Map<String,Object>>();
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("names");
String sex = rs.getString("sex");
String blood = rs.getString("blood");
String hobby = rs.getString("hobby");
long time = Long.valueOf(rs.getString("time"));
String date = df.format(time);
String pic = rs.getString("pic");
pic = pic.replace("\\", "/");
// 连接成一个字符串
Map<String,Object> map = new HashMap<>();
map.put("id",id);
map.put("name",name);
map.put("sex",sex);
map.put("blood",blood);
map.put("hobby",hobby);
map.put("date",date);
map.put("pic",pic);
result1.add(map);
}
request.setAttribute("result1",result1); //将List集合发送到JSPJSP端:
<table>
<tr>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>血型</td>
<td>爱好</td>
<td>发布时间</td>
<td>图片</td>
</tr>
<%
List<Map<String,Object>> result1 = (List<Map<String,Object>>)request.getAttribute("result1");
for(Map<String,Object>row:result1){ %>
<tr>
<td><%out.println(row.get("id")); %></td>
<td><%out.println(row.get("name")); %></td>
<td><%out.println(row.get("sex")); %></td>
<td><%out.println(row.get("blood")); %></td>
<td><%out.println(row.get("hobby")); %></td>
<td><%out.println(row.get("date")); %></td>
<td><%out.println(row.get("pic")); %></td>
</tr>
<% } %>
</table>