MySQL 如何在 JSP 页面中的表上显示数据库表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18997285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:57:49  来源:igfitidea点击:

How to display a database table on to the table in the JSP page

mysqljsp

提问by ABV

  • My problem is that the table in my database is not able to be displayed at the table in my JSP page.
  • I find this code totally right but dont know y m not getting the output right.
  • Please tell me what is the problem.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                                                    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
    </body>
    <form method="post">
    
    <table border="2">
    <tr>
    <td>ID</td>
    <td>NAME</td>
    <td>SKILL</td>
    <td>ACTION</td>
    </tr>
    <%
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost/test";
    String username="root";
    String password="root";
    String query="select * from jsp1";
    Connection conn=DriverManager.getConnection(url,username,password);
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery(query);
    while(rs.next())
    {
    
    %>
        <tr><td><%rs.getInt("ID"); %></td></tr>
        <tr><td><%rs.getString("NAME"); %></td></tr>
        <tr><td><%rs.getString("SKILL"); %></td></tr>
            <%
    
    }
    %>
        </table>
        <%
        rs.close();
        stmt.close();
        conn.close();
        }
    catch(Exception e)
    {
        e.printStackTrace();
        }
    
    
    
    
    %>
    
    </form>
    </html> 
    
  • 我的问题是我的数据库中的表无法显示在我的 JSP 页面中的表中。
  • 我发现这段代码完全正确,但不知道 ym 没有得到正确的输出。
  • 请告诉我是什么问题。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                                                    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
    </body>
    <form method="post">
    
    <table border="2">
    <tr>
    <td>ID</td>
    <td>NAME</td>
    <td>SKILL</td>
    <td>ACTION</td>
    </tr>
    <%
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost/test";
    String username="root";
    String password="root";
    String query="select * from jsp1";
    Connection conn=DriverManager.getConnection(url,username,password);
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery(query);
    while(rs.next())
    {
    
    %>
        <tr><td><%rs.getInt("ID"); %></td></tr>
        <tr><td><%rs.getString("NAME"); %></td></tr>
        <tr><td><%rs.getString("SKILL"); %></td></tr>
            <%
    
    }
    %>
        </table>
        <%
        rs.close();
        stmt.close();
        conn.close();
        }
    catch(Exception e)
    {
        e.printStackTrace();
        }
    
    
    
    
    %>
    
    </form>
    </html> 
    

回答by vinhdq

The problem here is very simple. If you want to display value in JSP, you have to use <%= %> tag instead of <% %>, here is the solved code:

这里的问题很简单。如果你想在 JSP 中显示值,你必须使用 <%= %> 标签而不是 <% %>,这里是解决的代码:

<tr> <td><%=rs.getInt("ID") %></td> <td><%=rs.getString("NAME") %></td> <td><%=rs.getString("SKILL") %></td> </tr>

<tr> <td><%=rs.getInt("ID") %></td> <td><%=rs.getString("NAME") %></td> <td><%=rs.getString("SKILL") %></td> </tr>

回答by Er Harsh Rathore

you can also print the data onto your HTML/JSP document. like:-

您还可以将数据打印到 HTML/JSP 文档中。喜欢:-

<!DOCTYPE html>
<html>
<head>
    <title>Jsp Sample</title>
    <%@page import="java.sql.*;"%>
</head>
<body bgcolor=yellow>
    <%
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=(Connection)DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/forum","root","root");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from student;");
    %><table border=1 align=center style="text-align:center">
      <thead>
          <tr>
             <th>ID</th>
             <th>NAME</th>
             <th>SKILL</th>
             <th>ACTION</th>
          </tr>
      </thead>
      <tbody>
        <%while(rs.next())
        {
            %>
            <tr>
                <td><%=rs.getString("id") %></td>
                <td><%=rs.getString("name") %></td>
                <td><%=rs.getString("skill") %></td>
                <td><%=rs.getString("action") %></td>
            </tr>
            <%}%>
           </tbody>
        </table><br>
    <%}
    catch(Exception e){
        out.print(e.getMessage());%><br><%
    }
    finally{
        st.close();
        con.close();
    }
    %>
</body>
</html>

<!--executeUpdate() mainupulation and executeQuery() for retriving-->

回答by ashu kmar

跟踪 ID 跟踪
    <br>
    <%String id = request.getParameter("track_id");%>
       <%if (id.length() == 0) {%>
    <b><h1>Please Enter Tracking ID</h1></b>
    <% } else {%>
    <div class="container">
        <table border="1" class="table" >
            <thead>
                <tr class="warning" >
                    <td ><h4>Track ID</h4></td>
                    <td><h4>Source</h4></td>
                    <td><h4>Destination</h4></td>
                    <td><h4>Current Status</h4></td>

                </tr>
            </thead>
            <%
                try {
                    connection = DriverManager.getConnection(connectionUrl + database, userid, password);
                    statement = connection.createStatement();
                    String sql = "select * from track where track_id="+ id;
                    resultSet = statement.executeQuery(sql);
                    while (resultSet.next()) {
            %>
            <tr class="info">
                <td><%=resultSet.getString("track_id")%></td>
                <td><%=resultSet.getString("source")%></td>
                <td><%=resultSet.getString("destination")%></td>
                <td><%=resultSet.getString("status")%></td>
            </tr>
            <%
                    }
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            %>
        </table>

        <%}%>
</body>