JokerAlexZhao 2017-03-27 13:47 采纳率: 0%
浏览 1330

jsp跳过输出list的for循环---能帮我重写一下循环改用foreach

我这样写,在jsp会直接跳过输出list的for循环(第二个for循环是测试是否所有for循环都跳过的,结果这个能输出,而且我用.java测试那个for循环时是可以打印的)

 //数据库实体类
package studentInformation;

public class Title {
    //数据库表的属性
    private int id;
    private String name;
    private String sex;
    private String phone;
    private String qq;

    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id=id;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getSex(){
        return sex;
    }
    public void setSex(String sex){
        this.sex=sex;
    }
    public String getPhone(){
        return phone;
    }
    public void setPhone(String phone){
        this.phone=phone;
    }
    public String getQq(){
        return qq;
    }
    public void setQq(String qq){
        this.qq=qq;
    }
    public Title(int id,String name,String sex,String phone,String qq){
        super();
        this.id=id;
        this.name=name;
        this.sex=sex;
        this.phone=phone;
        this.qq=qq;
    }

}
 //数据库操作类
package studentInformation;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import studentInformation.Title;

public class TitleDAO {
    public List<Title> readFirstTitle(){
        List<Title> list=new ArrayList<Title>();
        //数据库的驱动URL用户名密码
        String JDBC_DRIVER="com.mysql.jdbc.Driver";
        String DB_URL="jdbc:mysql://localhost:3306/student?characterEncoding=utf8&useSSL=true";
        String USER="root";
        String PASS="123456";

        Connection conn=null;
        Statement stmt=null;
        try{
            //注册JDBC驱动
            Class.forName(JDBC_DRIVER);
            //打开链接
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            //执行查询
            stmt=conn.createStatement();
            String sql="select*from information";//生成SQL语句
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                String sex=rs.getString("sex");
                String phone=rs.getString("phone");
                String qq=rs.getString("qq");
                Title t=new Title(id,name,sex,phone,qq);
                list.add(t);
            }
            //完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            //处理JDBC错误
            se.printStackTrace();
        }catch(Exception e){
            //处理Class.forName错误
            e.printStackTrace();
        }finally{
            //关闭资源
            try{
                if(stmt!=null)
                    stmt.close();
            }catch(SQLException se2){
                //do nothing
            }
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        return list;
    }
 }

 <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="studentInformation.TitleDAO,studentInformation.Title" %>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>titleList</title>
<style type="text/css">  
table {  
    border: 2px #CCCCCC solid;  
    width: 360px;  
}  

td,th {  
    height: 30px;  
    border: #CCCCCC 1px solid;  
}  
</style>  
</head>
<body>
<table border="1">
    <tr>
        <td>id</td>
        <td>name</td>
        <td>sex</td>
        <td>phone</td>
        <td>qq</td>
    </tr>
     <%
        TitleDAO dao=new TitleDAO();
        List<Title> list=dao.readFirstTitle();
        for(Title t:list)
        {
        %>
            <tr>
                <td><%=t.getId() %></td>
                <td><%=t.getName() %></td>
                <td><%=t.getSex() %></td>
                <td><%=t.getPhone() %></td>
                <td><%=t.getQq() %></td>
             </tr>
        <%}
        int j=111;
      %>      


      <%
        for(int i=0;i<5;i++){
        char a='a';
        %>
        <tr>
            <td><%=i %></td>
            <td><%=a+i %></td>
            <td>male</td>
            <td>17713562863</td>
            <td>1456395363</td>
         </tr>
        <%}
      %>                                          
</table>

</body>
</html>


  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3