Error com.microsoft.sqlserver.jdbc.SQLServerDriver not found

I am trying to connect to my SQL Server 2008 database from Java and I am having the same issue from this thread .

String userName = "xxxx"; String password = "xxxx"; String url = "jdbc:sqlserver:xxx.xxx.xxx.xxx;databaseName=asdfzxcvqwer;integratedSecurity=true"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection(url, userName, password); 

I keep getting a ClassNotFoundException whenever I try to load a driver from Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

 java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at repositories.RepositoryBase.<init>(RepositoryBase.java:22) at repositories.ProductsRepository.<init>(ProductsRepository.java:13) at api.Products.init(Products.java:31) at javax.servlet.GenericServlet.init(GenericServlet.java:160) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136) 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:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 

I made sure that the necessary jdbc and jtds libraries were added to the library, and CLASSPATH was also installed. I'm honestly not sure what went wrong here. enter image description here

Change: After the sentence, I tried to load this jdbc jar and put it in my WEB-INF / lib, and then set the CLASSPATH variable to this place. However, the same problem occurs. enter image description here

Edit2: never mind, a complete reinstall of Eclipse made it work. This is pretty unpleasant ...

+8
source share
8 answers

You do not need both jTDS and JDBC in your class path. Anyone is required. Here you only need sqljdbc.jar .

In addition, I would suggest placing sqljdbc.jar in a physical location in the /WEB-INF/lib your project, rather than adding it to the Classpath through the IDE. Then Tomcat takes care of the rest. Also try rebooting Tomcat.

You can download the Jar from: www.java2s.com/Code/JarDownload/sqlserverjdbc/sqlserverjdbc.jar.zip

EDIT:

When you connect your username and password,

You only need jdbc:sqlserver://localhost:1433;databaseName=test , skip integratedSecurity .

+7
source

You are watching sqljdbc4.2 version, for example:

 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

but for the sqljdbc4 statement should be:

 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 

I think that if you change your first version to write the correct Class.forName , your application will work.

+5
source

It worked for me as soon as I changed

 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

so that:

in POM

 <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.1.0.jre8</version> </dependency> 

and then:

 import com.microsoft.sqlserver.jdbc.SQLServerDriver; 

...

 DriverManager.registerDriver(SQLServerDriver()); Connection connection = DriverManager.getConnection(connectionUrl); 
+4
source
 public static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=dbName"; public static final String USERNAME = "xxxx"; public static final String PASSWORD = "xxxx"; /** * This method @param args command line argument */ public static void main(String[] args) { try { Connection connection; DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); connection = DriverManager.getConnection(MainDriver.URL,MainDriver.USERNAME, MainDriver.PASSWORD); String query ="select * from employee"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query); while(resultSet.next()) { System.out.print("First Name: " + resultSet.getString("first_name")); System.out.println(" Last Name: " + resultSet.getString("last_name")); } }catch(Exception ex) { ex.printStackTrace(); } } 
0
source

For me, this worked after we manually copied the sqljdbc4-2.jar file to the WEB-INF / lib folder. So please try this too.

0
source

smart idea 2019

  1. Download Microsoft JDBC driver for SQL Server

( https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017 )

  1. Unzip ("C: \ opt \ sqljdbc_7.2 \ enu \ mssql-jdbc-7.2.2.jre11.jar")
  2. Add; (File-> Project Structure-> Global Libraries)
  3. Use; (Adding Jar files to the IntellijIdea class path (see video)) add import com.microsoft.sqlserver.jdbc.SQLServerDriver; enter image description here https://youtu.be / -2hjxoRKsyk

or ub Gradle set "compile" compilation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '7.2.2.jre11'

0
source

For me, it was the wrong slowdown in dependency, so here is the right way:

for sqljdbc4 use: sqljdbc4 dependency

 <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> 

for sqljdbc42 use: sqljdbc42 dependency

 <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc42</artifactId> <version>6.0.8112</version> </dependency> 
0
source

here is your answer

 String userName = "xxxx"; String password = "xxxx"; String url = "jdbc:sqlserver:xxx.xxx.xxx.xxx;databaseName=asdfzxcvqwer;integratedSecurity=true"; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection(url, userName, password); } catch (Exception e) { e.printStackTrace(); } 
-1
source

All Articles