我想让jsp中显示的值成为我数据库中的学生总数。
但它显示为空,尽管我有 3 个数据在那里
这就是我所做的: 我的DAO:
import com.fyp.connection.MySqlConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DashboardLecturerDAO {
public int getTotalStudentCount() {
int totalCount = 0;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = new MySqlConnection().getConnection();
String query = "SELECT COUNT(*) AS total FROM student";
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
totalCount = resultSet.getInt("total");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return totalCount;
}
}
我的servlet:
import com.fyp.model.Dao.lecturer.DashboardLecturerDAO;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "Dashboard_LecturerServlet", urlPatterns = {"/Dashboard_LecturerServlet"})
public class Dashboard_LecturerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DashboardLecturerDAO dashboardLecturerDAO = new DashboardLecturerDAO();
int totalCount = dashboardLecturerDAO.getTotalStudentCount();
request.setAttribute("totalCount", totalCount);
request.getRequestDispatcher("/Dashboard-Lecturer.jsp").forward(request, response);
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我的连接类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySqlConnection {
private String jdbcURL = "jdbc:mysql://localhost:3306/project";
private String jdbcUsername = "root";
private String jdbcPassword = "";
public Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
}
jsp代码:
<p><%= request.getAttribute("totalCount") %></p>
<p>Total Students</p>
我无法指出您的问题。所以我建议你清理一下你的代码,以便更容易调试。
要做的第一件事是将数据库工作与用户界面(Servlet 和 JSP)工作分开。
您用“servlet-3.0”标记了您的问题,但您的导入使用了 Servlet 5.0 及更高版本规范中找到的
jakarta.*
包名称。因此,对于我这里的示例,我使用了 Jakarta EE 10. 中的 Servlet 6.0。
我在 IntelliJ 2024.1 Ultimate Edition 中结合 Tomcat 10.1.23 作为外部服务器运行了下面的示例代码。我正在 Java 22 上构建和运行。
我从最初的项目中删除了
web.xml
文件,因为如果使用诸如 @WebServlet
之类的注释,则不需要它。
我们的 servlet 很简短,只是将硬编码的
42
填充到您的属性 totalCount
中。
package work.basil.example.exservlet6;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Instant;
@WebServlet (name = "DashboardServlet", value = "/dashboard")
public class DashboardServlet extends HttpServlet
{
@Override
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("TRACE - DashboardServlet at "+ Instant.now() );
int totalCount = 42 ;
request.setAttribute("totalCount", totalCount);
request.getRequestDispatcher("/Dashboard.jsp").forward(request, response);
}
}
然后我们将请求转发到我们的 JSP。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Dashboard Example</title>
</head>
<body>
<p><%= request.getAttribute( "totalCount" ) %>
</p>
<p>Total Students</p>
</body>
</html>
瞧!我们的 JSP 使用我们通过属性传递的 42 进行渲染。
至于你项目的数据库部分,我将把它留给你作为练习。
我强烈建议您在 Stack Overflow 中搜索一些有关如何正确执行基本 JDBC 工作的示例。您会发现很多示例,包括我发布的一些完整示例应用程序。
一些简短的提示:
Connection
、PreparedStatement
、ResultSet
。DataSource
对象中的习惯。您应该使用 JDBC 驱动程序找到特定于数据库的实现。Repository
模式,定义一个具有从数据库存储和检索操作的接口,然后使用该接口的虚假实现,以及在生产中使用的真实实现。