JSP页面输出数据库表格

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页测试数据库连接</title>
</head>
<body>
<%
//注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "root");
//创建statement
Statement stmt = conn.createStatement();
//执行查询
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
%>
<table bgcolor="#9999dd" border="1" width="300">
<tr>
<th>序号:</th>
<th>姓名:</th>
<th>年龄:</th>
</tr>
<%

//遍历结果集
while(rs.next())
{%>
<!-- <tr>
<th>序号:</th>
<th>姓名:</th>
<th>年龄:</th>
</tr> -->
<tr>
<!-- 输出结果集 -->
<td><%=rs.getString(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td>
</tr>
<%}
%>

</table>
</body>
</html>

其中标红的代码注释掉了,具体情况看运行截图:

就是那段代码加的位置不对,导致这样的结果发生

博客园密码:yrz@zx1314

原文地址:https://www.cnblogs.com/yaoruozi/p/8192933.html