练习servlet监听器的时候,使用jdbc连接数据库,总是报错,各位老师,我该怎么解决

数据库连接除了问题,却不知道具体哪里的问题

监听器部分的代码

package com.zh.listener;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        String jdbcDriver = "com.mysql.jdbc.Driver";
        String dbURL = "jdbc:mysql://127.0.0.1/test";
        String dbUser = "root";
        String password = "root";
        try {
            Class.forName(jdbcDriver);
            Connection connection = DriverManager.
                    getConnection(dbURL, dbUser, password);
            ServletContext servletContext = event.getServletContext();
            servletContext.setAttribute("mycon", connection);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("Servlet has contextDestroyed...");
    }

}

servlet部分的代码

package com.zh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetDataServlet extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Override![图片说明](https://img-ask.csdn.net/upload/201906/19/1560915422_774394.png)

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try {
            ServletContext servletContext = getServletContext();
            Connection con = (Connection)servletContext.
                    getAttribute("mycon");
            if (con==null) {
                System.out.println("数据库连接异常");
            }
            String sql = "select * from emplpyees";
            PreparedStatement statement = con.prepareStatement(sql,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                out.print("<br>"+resultSet.getString(0)
                        +" "+resultSet.getString(1)
                        +" "+resultSet.getString(2));
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.close();
    }
}

web.xml部分代码

  <servlet>
    <servlet-name>getDataServlet</servlet-name>
    <servlet-class>com.zh.servlet.GetDataServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>getDataServlet</servlet-name>
    <url-pattern>/getDataServlet</url-pattern>
  </servlet-mapping>

图片说明

错误信息

2019-06-19 11:20:53.657:INFO:oejs.Server:jetty-8.1.14.v20131031
2019-06-19 11:20:54.041:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
数据库连接异常
java.lang.NullPointerException
    at com.zh.servlet.GetDataServlet.doGet(GetDataServlet.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
    at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Thread.java:745)

数据库账号密码都是root 这个我试过了 没有错

求助各位大神了 帮帮忙!!!

1个回答

第一好歹错误消息发出来。
第二看你的代码,链接数据库的字符串不对吧,端口号都没有,mysql 是3306

String dbURL = "jdbc:mysql://127.0.0.1/test";
改成
String dbURL = "jdbc:mysql://127.0.0.1:3306/test";

GetDataServlet.java:34 第34行 空指针异常,你debug调试下,第34应该没获取到数据。

javawebxx
东哥编程 我也正好遇到了这个问题,楼主解决了吗
12 个月之前 回复
qq_23126581
已退出 回复qq_39634344: GetDataServlet.java:34 第34行 空指针异常,你debug调试下,第34应该没获取到数据。
一年多之前 回复
qq_39634344
qq_39634344 错误信息发出来了 在最下面 加上端口号还是不行 还是报之前的错误
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐