0

I'm trying to establish a connection between a Java servlet running on Tomcat 8.5 and a DB running on SQL Server 2012.

Here are the things I tried so far:

  1. I added to the JAVA project the connector sqljdbc42.jar,

  2. Also copied the same file to C:\Program Files (x86)\Apache Software Foundation\Tomcat 8.5\lib,

  3. Played with the connection string (within the Java code) using the following alternatives:

    final String DB_URL="jdbc:microsoft:sqlserver://DBServer\\Instance1:1433;DatabaseName=" +
              DB_Name + ";user=" + USER + ";password=" + PASS ;
    

    and:

    final String DB_URL="jdbc:sqlserver://DBServer\\Instance1:1433;DatabaseName=" + 
       DB_Name + ";user=" + USER + ";password=" + PASS ;
    

Still I'm getting the following error:

SQLException: No suitable driver found for jdbc:sqlserver://DBServer\Instance1:1433;DatabaseName=MyDB;user=Web_Client;password=PWPWPWPW [Ljava.lang.StackTraceElement;@1bd336e
 And the error stack is: 
 java.sql.SQLException: No suitable driver found for jdbc:jdbc:sqlserver://DBServer\Instance1:1433;DatabaseName=MyDB;user=Web_Client;password=PWPWPWPW at 
 java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at 
 DispatcherClass.doGet(DispatcherClass.java:72) at 
 javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at 
 javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at 
 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) at 
 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at 
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at 
 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at 
 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at 
 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at 
 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at 
 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) at 
 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at 
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at 
 org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at 
 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at 
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) at 
 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) at 
 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at 
 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) at 
 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) at 
 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 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) 

I'm not in good command of neither Java (used Eclipse to create the WAR) nor with Tomcat.

halfer
  • 18,701
  • 13
  • 79
  • 158
FDavidov
  • 3,202
  • 5
  • 19
  • 48
  • 1
    Because you don't show the relevant code this is only a guess, but have a look at http://stackoverflow.com/questions/1911253/the-infamous-java-sql-sqlexception-no-suitable-driver-found , do you have a `Class.forName` to load your driver? – fvu Mar 14 '17 at 14:02
  • Thank you @fvu for your prompt reply. Two things: First, I included the piece of code that defines the connection string .If you would need to see something more, let me know what exactly and I'll update my question with it. Second, I found in the internet that, for SQL Server, it is not required to declare the class. Still, I might have misinterpreted that comment. Please let me know what more you need to bring me to the solution of this. Thanks!!! – FDavidov Mar 14 '17 at 14:10
  • Could it just be that you use `DatabaseName` instead of `databaseName` ? See [here](https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url) – fvu Mar 14 '17 at 14:21
  • [The full list of connection properties](https://docs.microsoft.com/en-us/sql/connect/jdbc/setting-the-connection-properties) – fvu Mar 14 '17 at 14:23
  • @fvu : how is this related to database name? error clearly states that `No suitable driver found` – rkosegi Mar 14 '17 at 14:25
  • @rkosegi because errors in the url also cause that error message. See the answer I linked in my first comment. – fvu Mar 14 '17 at 14:28
  • Eventually @fvu your first comment was the correct one. As soon as I added the `Class.forName` declaration access to the DB was possible. Thanks for your suggestions!! – FDavidov Mar 15 '17 at 09:44
  • @fvu: could you add an answer to this? It appears your suggestion was the correct one. Or is this an exact duplicate of the question you linked to? – halfer Apr 06 '19 at 19:12

0 Answers0