Java 如何在 JSP 页面中显示数据库表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31200393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:46:53  来源:igfitidea点击:

How to display a database table in the JSP page

javamysqljsp

提问by linnndaaa

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.

我正在尝试将我的用户表显示到我的 JSP 页面中的表上。但是当我运行JSP页面时没有显示数据。

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

我有一个名为“eyetracker”的 mySQL 模式和一个名为“user”的表。感谢您的帮助.. 如果可能,我想使用 servlet 检索 mySQL 数据并将其显示在 JSP 页面中...

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

采纳答案by Brijesh Dobariya

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>

<%

<%

回答by Shrinivas Shukla

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

在 JSP 中,如果要向 HTML DOM 添加动态内容,则需要这样做

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

这在您的代码中是缺失的,因此代码只是提取值而不是将它们插入到 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>

回答by Puneet Chawla

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.

Expression 标签将用于在 JSP 页面中显示值。表达式标记转换为 Java 语句。但是您使用的 Scriptlet 标记允许您在 JSP 服务器页面中嵌入任何有效的 Java 源代码。

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>

回答by Giovanni

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:

如果类路径中没有 jdbc 驱动程序,则错误会进入应用程序服务器输出,因为您有一行e.printStackTrace();. 尝试改变:

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

看看你的jdbc驱动有没有问题

回答by Brijesh Dobariya

Not only Expression tag convert into Java, but Full JSP file convert into Servlet, So use any one Expression or Scriptlet tags.

不仅Expression标签转换成Java,而且完整的JSP文件转换成Servlet,所以使用任何一个Expression或Scriptlet标签。

回答by Efthimis Avgeris

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.

只打印第一列的第一个值,然后循环停止。它仅适用于 varchar 类型的列,但不适用于 int 类型的列。