<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