Java Web应用程序无法连接到SQL Server

时间:2017-09-01 14:42:50

标签: java sql-server jsp jdbc tomcat7

我创建了一个动态网络应用程序,其中包含互联网的完整指南here。我使用了sql和tomcat 7,所以我提供了所需的机会(虽然我不知道这些可能是问题所在);

SQLServerConnUtils_SQLJDBC

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at simpleWebApp.conn.SQLServerConnUtils_SQLJDBC.getSQLServerConnection_SQLJDBC(SQLServerConnUtils_SQLJDBC.java:44)
at simpleWebApp.conn.SQLServerConnUtils_SQLJDBC.getSQLServerConnection_SQLJDBC(SQLServerConnUtils_SQLJDBC.java:22)
at simpleWebApp.conn.ConnectionUtils.getConnection(ConnectionUtils.java:12)
at simpleWebApp.filter.JDBCFilter.doFilter(JDBCFilter.java:95)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Eyl 01, 2017 5:21:48 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [simpleWebApp.servlet.HomeServlet] in context with path [/SimpleWebApp] threw exception [null] with root cause
javax.servlet.ServletException
    at simpleWebApp.filter.JDBCFilter.doFilter(JDBCFilter.java:112)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

我使用sqljdbc42作为我的jdbc。服务器正在运行。在SCM中启用TCP / IP。

这是控制台结果:

import java.io.IOException;
import java.sql.Connection;
import java.util.Collection;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import simpleWebApp.conn.ConnectionUtils;
import simpleWebApp.utils.MyUtils;

@WebFilter(filterName = "jdbcFilter", urlPatterns = { "/*" })
public class JDBCFilter implements Filter {

  public JDBCFilter() {
  }

  @Override
  public void init(FilterConfig fConfig) throws ServletException {

  }

  @Override
  public void destroy() {

  }


  // Check the target of the request is a servlet?
  private boolean needJDBC(HttpServletRequest request) {
      System.out.println("JDBC Filter");
      //
      // Servlet Url-pattern: /spath/*
      //
      // => /spath
      String servletPath = request.getServletPath();
      // => /abc/mnp
      String pathInfo = request.getPathInfo();

      String urlPattern = servletPath;

      if (pathInfo != null) {
          // => /spath/*
          urlPattern = servletPath + "/*";
      }

      // Key: servletName.
      // Value: ServletRegistration
      Map<String, ? extends ServletRegistration> servletRegistrations = request.getServletContext()
              .getServletRegistrations();


      // Collection of all servlet in your webapp.
      Collection<? extends ServletRegistration> values = servletRegistrations.values();
      for (ServletRegistration sr : values) {
          Collection<String> mappings = sr.getMappings();
          if (mappings.contains(urlPattern)) {
              return true;
          }
      }
      return false;
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          throws IOException, ServletException {

      HttpServletRequest req = (HttpServletRequest) request;


      //
      // Only open connections for the special requests need
      // connection. (For example, the path to the servlet, JSP, ..)
      //
      // Avoid open connection for commons request
      // (for example: image, css, javascript,... )
      //
      if (this.needJDBC(req)) {

          System.out.println("Open Connection for: " + req.getServletPath());

          Connection conn = null;
          try {
              // Create connection
              conn = ConnectionUtils.getConnection();

              // Set Auto commit to false
              conn.setAutoCommit(false);

               // Store connection in attribute of request.
              MyUtils.storeConnection(request, conn);

              // Allow request to go forward
              // (Go to the next filter or target)
              chain.doFilter(request, response);

               // Commit change.
              conn.commit();
          } catch (Exception e) {
              e.printStackTrace();
              ConnectionUtils.rollbackQuietly(conn);
              throw new ServletException();
          } finally {
              ConnectionUtils.closeQuietly(conn);
          }
      }

      // With commons requests (images, css, html, ..)
      // No need to open the connection.        
      else {

          // Allow request to go forward
          // (Go to the next filter or target)            
          chain.doFilter(request, response);
      }

  }

我还认为jdbcfilter可能存在问题(它在控制台中说)

button

如果您有任何想法,请告诉我。 编辑:Sory在我使用tomcat 7服务器之前没有提及

0 个答案:

没有答案