BUG: “java.sql.Connection.prepareStatement(String)“ because “this.conn“ is null

问题发现时间:2022.12.10
问题描述:console台跳转到前端页面出错
我的主要代码:

@WebServlet("/allstudent")
public class allStudent extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public allStudent() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		StudentDao student1 = new StudentDao();
		List<Student> list1 = student1.getall();
		request.setAttribute("list1", list1);
		request.getRequestDispatcher("student.jsp").forward(request, response);
	}	

在这里插入图片描述

因为是跳转到前端出错,所以主要研究:

  1. servlet类有没有写错
  2. 注解路径自己在写到jsp文件有没有漏写,或者大写变小写,小写变大写
  3. 数据库驱动包是不是版本过低,过高导致不适配

然后我发现我路径确实写的有问题,student的s小写写成了大写

改正前

"${pageContext.request.contextPath}/allStudent";

改正后

"${pageContext.request.contextPath}/allstudent";

但是还是报错

HTTP状态 500 - 内部服务器错误
严重: 在路径为[/experiment04]的上下文中,servlet[com.csmz.servlet.allStudent]的Servlet.service()引发异常
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "this.conn" is null

我将数据库驱动包下载称最新版本依然不行,有点困惑,但是思路也依然是从servlet和数据库连接里找。肯定是过于粗心哪里出现了问题自己忽视了。
经过我仔细的寻找发现是数据库连接的时候我的数据库密码是root,但是我写成了password

        String url = "jdbc:mysql://localhost:3306/studentdb";
		String username = "root";
		String password = **~~"password";~~ **

改正后

        String url = "jdbc:mysql://localhost:3306/studentdb";
		String username = "root";
		String password = "root";

解决了,大家自己找的时候要注意数据库名是否正确,数据库的名字和密码是否正确。

猜你喜欢

转载自blog.csdn.net/weixin_45384457/article/details/128260925
今日推荐