简体   繁体   中英

How to display a database table in the JSP page

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

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>

Notice the = in the above code.

That is missing in your code, therefore the code is just extracting the values but not inserting 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>

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>

<%

Expression tag will be used for displaying value in JSP page. Expression tag convert into Java statement. But you are using Scriptlet tags allow you to embed any valid Java source code in JSP server pages.

It result is coming from database, then it will be display properly.

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

if you don't have the jdbc driver in the classpath the error goes into the application server output since you have a line with e.printStackTrace(); . try to change:

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

With:

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

to see if you have some problem with the jdbc driver

不仅Expression标签转换为Java,而且Full JSP文件转换为Servlet,因此使用任何一个Expression或Scriptlet标签。

Great solution, tried the same on my table but

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM