使用servlet与sql建立Java连接

时间:2016-11-05 07:32:59

标签: java mysql database-connection

这与NullPointerException不同,因为问题是它遇到sql连接时。

当我从另一个类使用checkUser()时,我在PreparedStatement获取java.lang.NullPointerException pstmt = connect.prepareStatement(sql);

我已经检查过我的sql server运行正常,配置是正确的,getConnection()代码是有意义的。我真的很困惑为什么它不会连接到数据库并抛出指针异常。

我可以就如何使其发挥作用提出一些建议吗?

public class UserVerification {

  private static Connection connect = null;
  private static String host="localhost";
  private static String database="database";
  private static String username="root";
  private static String password="xxxx";


  public static Connection getConnection(){

   if(connect ==null){
            try{
             Class.forName("com.mysql.jdbc.Driver");
              String conn_string="jdbc:mysql://"+host+"/"+database;
              connect = DriverManager.getConnection(conn_string,username,password);
              return connect;
            }catch(Exception ex){

                ex.printStackTrace();
            }
        }else{
            return connect;
        }
      return connect;
    }

  public User checkUser(String user,String password){

      String sql="SELECT * FROM OFFICER  WHERE OUSERNAME='?' AND PASSWORDHASH=?";
      User u=null;
        try( Connection connect = getConnection(); 
             PreparedStatement pstmt = connect.prepareStatement(sql);
            ){  
             pstmt.setString(1,user);
             pstmt.setString(2,HashGenerator.getMD5Hash(password));
             try (ResultSet rs = pstmt.executeQuery();){
               while(rs.next()){             
                  String uname=rs.getString("USERNAME");        
                  String pass=rs.getString("PASSWORDHASH");     
                  u = new User(uname,pass);
                  break;
              }
             }
        }catch(SQLException ex){
            ex.printStackTrace();   
        }
        return u;
  }
}

StackTrace我从上面的代码中得到了getConnection()ex.printStactTrace();

org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [LoginServlet] in context with path [/project] threw exception
java.lang.NullPointerException
at UserVerification.checkUser(UserVerification.java:40)
at LoginServlet.doPost(LoginServlet.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:-1)

在getConnection中,您声明一个具有相同名称连接的new.Connection对象并返回它。因此永远不会填充静态字段连接。删除该声明并为静态连接字段分配。此外,在捕获几行之后,您将返回null并且不打印堆栈跟踪。尝试打印,看看会发生什么