Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a database table in the JSP page

Tags:

java

mysql

jsp

I am trying to display my user table on to a table in my JSP page. But the data is not shown when I run the JSP page.

I have a mySQL schema called "eyetracker" and a table called "user". Appreciate your help.. If possible , I want to retrieve mySQL data by using servlet and display it in a JSP page...

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>

<form method="post">

<table border="2">
   <tr>
        <td>user ID</td>
        <td>Birthday</td>
        <td>Gender</td>
        <td>First Name</td>
        <td>Last Name</td>
   </tr>
   <%
   try
   {
       Class.forName("com.mysql.jdbc.Driver");
       String url="jdbc:mysql://localhost:3306/eyetracker";
       String username="root";
       String password="root";
       String query="select * from eyetracker";
       Connection conn=DriverManager.getConnection(url, username, password);
       Statement stmt=conn.createStatement();
       ResultSet rs=stmt.executeQuery(query);
       while(rs.next())
       {
   %>
           <tr><td><%rs.getInt("userID"); %></td></tr>
           <tr><td><%rs.getDate("dob"); %></td></tr>
           <tr><td><%rs.getString("gender"); %></td></tr>
           <tr><td><%rs.getString("firstName"); %></td></tr>
           <tr><td><%rs.getString("lastName"); %></td></tr>

   <%
       }
   %>
   </table>
   <%
        rs.close();
        stmt.close();
        conn.close();
   }
   catch(Exception e)
   {
        e.printStackTrace();
   }
   %>
</form>`
like image 965
linnndaaa Avatar asked Sep 28 '22 10:09

linnndaaa


1 Answers

update your small code like following,

%>

       <tr><td><%out.println(rs.getInt("userID")); %></td></tr>
       <tr><td><%out.println(rs.getDate("dob")); %></td></tr>
       <tr><td><%out.println(rs.getString("gender")); %></td></tr>
       <tr><td><%out.println(rs.getString("firstName")); %></td></tr>
       <tr><td><%out.println(rs.getString("lastName")); %></td></tr>
       <tr><td><%=rs.getInt("userID"); %></td></tr>
       <tr><td><%=rs.getDate("dob"); %></td></tr>
       <tr><td><%=rs.getString("gender"); %></td></tr>
       <tr><td><%=rs.getString("firstName"); %></td></tr>
       <tr><td><%=rs.getString("lastName"); %></td></tr>

<%

like image 76
Brijesh Dobariya Avatar answered Oct 13 '22 03:10

Brijesh Dobariya