需要帮助使用html和jsp从mysql数据库检索数据吗

oknrviil  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(355)

所以我试图把mysql数据库中的数据转换成html格式的表。数据库有电影信息,有5列标题,如电影ID,标题等。我已经连接到数据库并检索了数据,但我真的不知道如何将结果格式化成这样。
结果是这样的
这是我的代码,任何帮助都是感激的。

<%@ 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>
bqjvbblv

bqjvbblv1#

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

<%@ 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>

相关问题