Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

jsp

  • 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> 
    
like image 659
ABV Avatar asked Sep 25 '13 05:09

ABV


People also ask

Can we connect to a database using JSP?

In JSP, we are using databases for storing huge types of data. We can easily connect with databases to create and manage records.


2 Answers

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>

like image 192
vinhdq Avatar answered Oct 03 '22 14:10

vinhdq


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-->
like image 30
Er. Harsh Rathore Avatar answered Oct 03 '22 14:10

Er. Harsh Rathore