所以我试图将MySQL数据库中的数据放入HTML表格中。该数据库具有电影信息,并且有5列带有标题的列,例如movieID,title等。我已经连接到数据库并检索了数据,但是我真的不知道如何格式化结果,使其看起来像this.
相反,我的结果看起来像this
这是我的代码,感谢您的帮助。
<%@ include file = "header.html" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.DriverManager" %>
<html>
<%
Class.forName("com.mysql.jdbc.Driver");
String cxnString = "jdbc:mysql://localhost:3306/alfonsom?user=root&password=bcis3680";
Connection cxn = DriverManager.getConnection(cxnString);
Statement stm = cxn.createStatement();
String sql ="select * from movie;";
ResultSet rs = stm.executeQuery(sql);
while(rs.next()){
%>
<body>
<table border="2" cellpadding="4">
<tr>
<th>   </td>
<th> Movie ID</td>
<th> Title</td>
<th> Genre</td>
<th> MPAA Rating</td>
<th> Release Date</td>
</tr>
<td><input type="radio" name="movie" value="10001">
<td><%=rs.getString("mid") %></td>
<td><%=rs.getString("title") %></td>
<td><%=rs.getString("genre") %></td>
<td><%=rs.getString("mpaa") %></td>
<td><%=rs.getString("rlsdate") %></td>
<% } %>
</table>
</body>
</html>
答案 0 :(得分:0)
只需更改执行循环的位置即可。
<%@ include file = "header.html" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.DriverManager" %>
<html>
<%
Class.forName("com.mysql.jdbc.Driver");
String cxnString = "jdbc:mysql://localhost:3306/alfonsom?user=root&password=bcis3680";
Connection cxn = DriverManager.getConnection(cxnString);
Statement stm = cxn.createStatement();
String sql ="select * from movie;";
ResultSet rs = stm.executeQuery(sql);
%>
<body>
<table border="2" cellpadding="4">
<tr>
<th>   </td>
<th> Movie ID</td>
<th> Title</td>
<th> Genre</td>
<th> MPAA Rating</td>
<th> Release Date</td>
</tr>
<%
while(rs.next()){
%>
<tr>
<td><input type="radio" name="movie" value="10001"></td>
<td><%=rs.getString("mid") %></td>
<td><%=rs.getString("title") %></td>
<td><%=rs.getString("genre") %></td>
<td><%=rs.getString("mpaa") %></td>
<td><%=rs.getString("rlsdate") %></td>
</tr>
<% } %>
</table>
</body>
</html>