ABV
ABV

Reputation: 107

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

Upvotes: 7

Views: 118876

Answers (4)

29_Saurav Padghan
29_Saurav Padghan

Reputation: 1

After few googling I found this solution.

    <sql:setDataSource var = "snapshot" driver = "com.mysql.cj.jdbc.Driver"
                  url = "jdbc:mysql://localhost:3306/edyoda"
                  user = "root"  password = "xxxxx"/>
    <sql:query dataSource = "${snapshot}" var = "result">
             SELECT * FROM exam;
    </sql:query>

    <table border = "1" width = "100%">
        <tr>
            <th>Id</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Semester</th>
            <th>Total Marks</th>
            <th>Percentage</th>
        </tr>

        <c:forEach var = "row" items = "${result.rows}">
            <tr>
                <td><c:out value = "${row.id}"/></td>
                <td><c:out value = "${row.firstName}"/></td>
                <td><c:out value = "${row.lastName}"/></td>
                <td><c:out value = "${row.semester}"/></td>
                <td><c:out value = "${row.totalMarks}"/></td>
                <td><c:out value = "${row.percentage}"/></td>
            </tr>
        </c:forEach>
    </table>

Upvotes: 0

Er. Harsh Rathore
Er. Harsh Rathore

Reputation: 798

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

<!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-->

Upvotes: 2

ashu kmar
ashu kmar

Reputation: 31

Tracking ID Track

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

Upvotes: 0

vinhdq
vinhdq

Reputation: 164

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:

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

Upvotes: 15

Related Questions