我这样写,在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>