java web 连接数据库的问题 50C

程序突然就连不上数据库了,之前一直好好的,把登陆部分的代码贴上来,求大神给看看

jsp部分:<!DOCTYPE html>

<!--[if (gt IE 9)|!(IE)]><!--> <!--<![endif]-->



<!-- -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
    <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
    <meta name="author" content="Codrops" />
    <link rel="shortcut icon" href="../favicon.ico"> 
    <link rel="stylesheet" type="text/css" href="css/demo.css" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
</head>
<body>
    <div class="container">
        <!-- Codrops top bar -->
        <div class="codrops-top">


            <div class="clr"></div>
        </div><!--/ Codrops top bar -->
        <header>

            <nav class="codrops-demos">


            </nav>
        </header>
        <section>               
            <div id="container_demo" >
                <!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4  -->
                <a class="hiddenanchor" id="toregister"></a>
                <a class="hiddenanchor" id="tologin"></a>
                <div id="wrapper">
                    <div id="login" class="animate form">
                        <form  action="com.xx.servlet/LoginServlet" method="post"> 
                            <h1>Log in</h1> 
                            <p> 
                                <label for="username" class="uname" data-icon="u" > Your username </label>
                                <input type="text" name="username" />
                            </p>
                            <p> 
                                <label for="password" class="youpasswd" data-icon="p"> Your password </label>
                                <input type="password" name="password" />
                            </p>
                            <p class="keeplogin"> 


                            </p>
                            <p class="login button"> 
                                <input type="submit" value="Login" /> 
                            </p>
                            <p class="change_link">
                                Not a member yet ?
                                <a href="register.jsp" class="to_register">Join us</a>
                            </p>

                    </div>

                </div>
            </div>  
        </section>
    </div>
</body>

servlet部分:package com.xx.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bbs.*;
import com.mysql.jdbc.Driver;

/**

  • Servlet implementation class LoginServlet
    */
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);// TODO Auto-generated method stub }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setCharacterEncoding("utf-8");
      response.setContentType("text/html;charset=utf-8");
      request.setCharacterEncoding("utf-8");
      String name = null;String level = null;
      Connection conn = null;

      Statement stmt=null;
      ResultSet res=null;
      boolean flag=false;//通过数据库匹配标识
      String name_checked=null;//检查通过的用户名

      try {
      Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
      // TODO 自动生成的 catch 块
      e.printStackTrace();
      }

      String URL="jdbc:mysql://localhost:3306/mybbs";
      String USER="root";
      String PASSWORD="5438027";

      //1.加载驱动程序
      
          try {
              Class.forName("com.mysql.jdbc.Driver");
          } catch (ClassNotFoundException e1) {
              System.out.println("加载驱动失败1");// TODO 自动生成的 catch 块
              e1.printStackTrace();
          }
      
      //2.获得数据库的链接
      
          try {
              DriverManager.registerDriver(new Driver());
              conn=DriverManager.getConnection(URL, USER, PASSWORD);
          } catch (SQLException e) {
              System.out.println("加载失败2");// TODO 自动生成的 catch 块
              e.printStackTrace();
          }
      

      String u=request.getParameter("username"); //从上级页面获得用户名
      String p=request.getParameter("password"); //从上级页面获得密码
      String sql="SELECT * FROM user WHERE userName IN('"+u+"') "+"AND passWord IN('"+p+"')";

      System.out.println(sql);//执行上述sql语句

      try {
          res=stmt.executeQuery(sql);
      
      } catch (SQLException e) {
          System.out.println("加载失败3");// TODO 自动生成的 catch 块
          e.printStackTrace();
      }
      
      try {
          if(true==res.next())//只有查询得到结果才可进入循环
          {
              flag=true;
      
                  name_checked=res.getString("name");
                  level=res.getString("level");
      
          }
      } catch (SQLException e) {
          System.out.println("加载失败4");// TODO 自动生成的 catch 块
          e.printStackTrace();
      }
      

    if(true==flag)//登陆成功
    {

        HttpSession session=request.getSession();
    
        session.setAttribute("userlevel",level);
        session.setAttribute("name",name_checked);
    
        response.sendRedirect(request.getContextPath()+"/loginsuccess.jsp");
        User me=new User();
    
     }
     else//登陆失败
     {
    
        response.sendRedirect(request.getContextPath()+"/login.jsp");
     }
    

    }

}

报错:HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception
java.lang.NullPointerException
com.xx.servlet.LoginServlet.doPost(LoginServlet.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.82 logs.

Apache Tomcat/7.0.82

控制台:十月 26, 2017 1:10:55 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [LoginServlet] in context with path [/xxbbs] threw exception
java.lang.NullPointerException
at com.xx.servlet.LoginServlet.doPost(LoginServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)

不知道哪里出问题了,之前一直好好的,求大神帮我看看

11个回答

空指针,需要标记出99行代码,才能根据上下文判断什么问题

共11条数据 首页 2
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!