首先附上JSP代碼
<%@ page contentType="text/html; charset=utf-8" %>
<%@page import="java.sql.*" %>
<html><body>
<p>標記點1</p>
<%
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
out.print("沒有MySQL驅動");
}
String url="jdbc:mysql://127.0.0.1/warehouse";
String user="root";
String password="";
con=DriverManager.getConnection(url, user, password);
sql=con.createStatement();
rs=sql.executeQuery("select * from product;");
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>產品號</th>");
out.print("<th width=100>名稱</th>");
out.print("<th width=100>生產日期</th>");
out.print("<th width=100>價格</th>");
out.print("</tr>");
while(rs.next()){
out.print("<tr>");
out.print("<td width=100>"+rs.getString(1)+"</td>");
out.print("<td width=100>"+rs.getString(2)+"</td>");
out.print("<td width=100>"+rs.getString(3)+"</td>");
out.print("<td width=100>"+rs.getString(4)+"</td>");
out.print("</tr>");
}
rs.close();
out.print("</tale>");
out.print("<p>標記點2</p>");
rs=sql.executeQuery("select * from students;");
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>學號</th>");
out.print("<th width=100>姓名</th>");
out.print("<th width=100>專業代號</th>");
out.print("<th width=100>性別</th>");
out.print("</tr>");
while(rs.next()){
out.print("<tr>");
out.print("<td width=100>"+rs.getString(1)+"</td>");
out.print("<td width=100>"+rs.getString(2)+"</td>");
out.print("<td width=100>"+rs.getString(3)+"</td>");
out.print("<td width=100>"+rs.getString(4)+"</td>");
out.print("</tr>");
}
rs.close();
out.print("</tale>");
%>
<p>標記點3</p>
</body></html>
編譯運行後好奇怪啊。頁面顯示的順序不對。
本應是標記點2在兩個表格**中間**顯示,標記點3第二個表格**下方**顯示。為什麼會顯示這樣呢?
我的代碼有問題嗎?有問題的話,哪裡錯了,如何改正?沒問題的話,可以解釋下為什麼嗎?謝謝。
你table的閉合標簽錯誤
</table>你寫的是</tale>