0

I have created a dynamic web application with a full guide in internet which is here. I used sql and tomcat 7 so i made the required chances which are here(these might be the problem though i dont know);

SQLServerConnUtils_SQLJDBC

String hostName = "localhost";
String sqlInstanceName = "CURSEOFDARKNESS";
String database = "Hearthstone";
String integrated = "integratedSecurity=true";

return getSQLServerConnection_SQLJDBC(hostName, sqlInstanceName,
        database, integrated);
}




private static Connection getSQLServerConnection_SQLJDBC(String hostName,
        String sqlInstanceName, String database, String integrated) throws ClassNotFoundException, SQLException {


       
 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 
 //i think problem is here but i dont know
String connectionURL = "jdbc:sqlserver://" + hostName + ":1433"
        + ";instanceName=" + sqlInstanceName + ";databaseName="
        + database + ";" + integrated;


Connection conn = DriverManager.getConnection(connectionURL);
return conn;

I am using sqljdbc42 as my jdbc. server is running .TCP/IP are enabled in SCM.

here is the console outcome:

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)

And i also think there might be a problem with jdbcfilter(it says in console )

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);
      }

  }

if you have any idea please let me know. edit:Sory for not mentioning before i am working on tomcat 7 server

Community
  • 1
  • 1
Tenbatsu
  • 1
  • 5
  • You are specifying both a port number and an instance name. You should only supply one of those. Also, a named instance is unlikely to be listening on port 1433. – Gord Thompson Sep 01 '17 at 14:53
  • Yes I believe port 3307 is the default. – Carl Shiles Sep 01 '17 at 15:00
  • If you only have one SQL instance, then it usually communicates on port 1433. Ensure your firewall allows this. If you have more than one instance, you should probably be assigning a dynamic port number. Go into SCM > Protocols for DATABASE > Right click 'TCP/IP' and go to the IP Addresses tab. Scroll to bottom and set TCP Dynamic Port to 0. Remove 1433 if set. – greenTree Sep 01 '17 at 15:11
  • Also, of course, make sure your SQL Server ([DATABASE]) service and the SQL Server Browser service are both running. Also check that in SCM that 'Named Pipes' are enabled. – greenTree Sep 01 '17 at 15:16
  • 1
    @CarlShiles - Port 3307 is not a default port number for Microsoft SQL Server. 3306 is the default port number for MySQL. – Gord Thompson Sep 01 '17 at 15:20
  • @greenTree re: *"If you only have one SQL instance, then it usually communicates on port 1433."* - That is not necessarily true. There are lots of SQL Server Express installs out there with a single instance (named SQLEXPRESS by default) that are **not** listening on port 1433. – Gord Thompson Sep 01 '17 at 15:23
  • @greenTree re: *"Also check that in SCM that 'Named Pipes' are enabled."* - AFAIK, mssql-jdbc cannot communicate via named pipes, just TCP/IP. – Gord Thompson Sep 01 '17 at 15:26
  • thx for all answers i am trying them 1 by 1 i will let you know which works – Tenbatsu Sep 01 '17 at 15:42
  • ` String connectionURL = "jdbc:sqlserver://" + hostName + ":1433" + ";databaseName=" + database + ";" + integrated; ` this didnt work @Gord Thompson , my firewall is not open, dynamic port is 0 @greenTree – Tenbatsu Sep 01 '17 at 15:53
  • @Tenbatsu re: *"dynamic port is 0"* - Scroll to the bottom of the list and see if a "TCP Dynamic Ports" value is specified for the "IPAll" adapter. That's normally where the dynamic port value is specified. (It's usually zero for all of the other adapters.) – Gord Thompson Sep 01 '17 at 16:07
  • @GordThompson yes in "IPAII" adapter TCP Dynamic Ports is 55208 should i change it to 0? edit: after i changed and restarted it automaticaly changed into another value like 49294 – Tenbatsu Sep 01 '17 at 19:49
  • @Tenbatsu - No, just use 55208 instead of 1433 for the port number in your connection string, and do not specify an instance name. – Gord Thompson Sep 01 '17 at 19:51
  • @GordThompson new error WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:9394d7f9-29a8-4a26-b4f0-8fb6081b144d – Tenbatsu Sep 01 '17 at 19:55
  • @GordThompson i am new to java so i forgot to tell you that i am using tomcat 7 as server. – Tenbatsu Sep 01 '17 at 20:46

0 Answers0