html - 需要帮助使用 HTML 和 JSP 从 Mysql 数据库检索数据

标签 html mysql jsp

所以我试图将 MySQL 数据库中的数据获取到 HTML 表格中。该数据库包含电影信息,并有 5 列,其中包含电影 ID、标题等标题。我已经连接到数据库并检索了数据,但我真的不知道如何将结果格式化为 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> &nbsp </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>

最佳答案

只需更改执行循环的位置即可。

<%@ 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> &nbsp </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>

关于html - 需要帮助使用 HTML 和 JSP 从 Mysql 数据库检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53510681/

相关文章:

php - 如何在php编码中设置默认值

php - 显示特定字段的所有结果以及带有内连接的其余结果

php - 我的 sql 文件上传需要 "if/else"语句

java - 如何在 servlet 中使用 jsp 值?

javascript - 如何在javascript函数onChange中传递两个参数

PHP 变量超出 HTML 链接 href 字符串?

mysql - 设置可序列化会使 2 个 select 语句成为原子吗? (两次选择之间的表格没有变化)

javascript - 创建水平菜单 Web 应用程序

java - 不处理 Apache tile 定义中的 EL 表达式

html - CSS 是否应该因协议(protocol)而异(HTTP 与 HTTPS)?