How to display database table on JSP page

I am trying to display my users table in a table on my JSP page. But the data is not displayed when I launch the JSP page.

I have a mySQL schema called eyetracker and a table called user. Appreciate your help. If possible, I want to get mySQL data using a servlet and display it on 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>` 
+5
source share
6 answers

update your little code as below

%>

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

<%

+2
source

In JSP, if you want to add dynamic content to the HTML DOM, you need to do this

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

Note the = in the code above.

There is no code in the code, so the code simply extracts the values, but does not paste them into the HTML DOM.

 <%@ 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 user"; 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> 
+2
source

The Expression tag will be used to display the value on the JSP page. Expression tag expression in a Java expression. But you use Scriptlet tags that allow you to embed any valid Java source code in your JSP server pages.

The result is obtained from the database, then it will be displayed correctly.

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

If you do not have a jdbc driver in the classpath, the error is displayed on the application server, since you have a line with e.printStackTrace(); . try changing:

 catch(Exception e) { e.printStackTrace(); } 

WITH

 catch(Exception e) { e.printStackTrace(); out.println("<h1> error: "+ e.getMessage()+"</h1>"); } 

to find out if you have problems with jdbc driver

0
source

Not only is the Expression tag converted to Java, but the full JSP file is converted to Servlet, so use any Expression or Scriptlet tags.

0
source

Great solution, tried the same thing on my desk, but

  <tr><td><%=rs.getString("column1") %></td <td><%=rs.getInt("column2") %></td></tr> 

prints only the first value of the first column, and then the loop stops. It works fine only for varchar columns, but not for int columns.

0
source

All Articles