Java Web项目JSP显示数据库学生总数为null问题求助
问题:JSP页面显示学生总数为null(数据库已有3条数据)

我希望JSP页面显示数据库中的学生总数,但数据库内已有3条学生数据,页面却显示null。
以下是我编写的代码:
我的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); } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; } }
我的数据库连接类
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>
排查与解决方法
1. 确保请求通过Servlet访问
直接访问JSP页面时,request中不会有totalCount属性,自然显示null。必须访问http://你的域名/项目名/Dashboard_LecturerServlet,让Servlet处理请求后转发到JSP。
2. 检查数据库连接有效性
在DashboardLecturerDAO的getTotalStudentCount方法中添加打印语句,确认数据库连接是否成功:
connection = new MySqlConnection().getConnection(); System.out.println("数据库连接状态:" + (connection != null ? "成功" : "失败"));
如果连接失败,检查:
- MySQL服务是否启动
jdbcURL中的数据库名project是否存在- 用户名
root和密码是否正确
3. 验证SQL查询结果
在获取总数后添加打印,确认查询是否正确:
if (resultSet.next()) { totalCount = resultSet.getInt("total"); System.out.println("查询到的学生总数:" + totalCount); }
如果打印结果不是3,检查:
student表名是否拼写正确(MySQL在区分大小写的系统中表名大小写敏感)- 表中是否确实有3条有效数据(排除被逻辑删除的记录)
4. JSP代码优化(可选)
用EL表达式替代脚本代码,更简洁且避免空指针显示问题:
<p>${totalCount}</p> <p>Total Students</p>
内容的提问来源于stack exchange,提问作者Faris Zack
相关产品推荐
相关产品推荐

