19

I am trying to connect to my SQL Server 2008 database from Java and I'm having the same problem 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 the 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've made sure the necessary jdbc and jtds drivers are added to the library and CLASSPATH is set as well. I'm honestly not sure what went wrong here.enter image description here

Edit: Upon suggestion, I tried to download this jdbc jar and place it in my WEB-INF/lib, then set my CLASSPATH variable to that location. Still, the same problem is happening.enter image description here

Edit2: Never mind, completely reinstalling Eclipse made it work. This is pretty frustrating...

Community
  • 1
  • 1
TtT23
  • 6,876
  • 34
  • 103
  • 174
  • 1
    Try to include [this](http://www.java2s.com/Code/JarDownload/sqlserverjdbc/sqlserverjdbc.jar.zip) jar to your classpath – bsiamionau Mar 06 '13 at 08:07
  • 1
    Mot probably not related: but why do you have the jTDS **and** the Microsoft driver in your classpath? You only need one of them. –  Mar 06 '13 at 08:08
  • @a_horse_with_no_name I misread the tutorial. I've removed it now but still same error. – TtT23 Mar 06 '13 at 08:10
  • As you are using `;integratedSecurity=true` you also need to make sure that `sqljdbc_auth.dll` can be loaded by your class (so it needs to be on `java.library.path`) –  Mar 06 '13 at 08:10
  • @a_horse_with_no_name java.library.path? Do you mean I need to add the path containing the jar via the `Add External Class Folder` Or set the classpath as a directory path rather than the full path to the jar file? But I think my issue comes before the authentication issue as it's not even able to find the class. – TtT23 Mar 06 '13 at 08:14
  • No, not the *jar* the **DLL** - that is if you really want Windows authentication rather than SQL Server authentication. See the manual for details: http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated –  Mar 06 '13 at 08:24
  • @a_horse_with_no_name Oh no, I don't want windows authentication. That line was added by mistake and is now removed. – TtT23 Mar 06 '13 at 08:25

9 Answers9

11

You dont need both jTDS and JDBC in your classpath. Any one is required. Here you need only sqljdbc.jar.

Also, I would suggest to place sqljdbc.jar at physical location to /WEB-INF/lib directory of your project rather than adding it in the Classpath via IDE. Then Tomcat takes care the rest. And also try restarting Tomcat.

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

EDIT:

As you are supplying Username and Password when connecting,

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

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
9

You are looking at sqljdbc4.2 version like :

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

but, for sqljdbc4 version statement should be:

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

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

Ben Hoffman
  • 8,149
  • 8
  • 44
  • 71
tanpham
  • 101
  • 1
  • 1
  • 1
    Answer checks out: https://msdn.microsoft.com/en-us/library/ms378526(v=sql.110).aspx shows one way, https://support.microsoft.com/en-us/kb/313100 shows the other. – Kenster Sep 05 '15 at 21:56
7

For me, it worked once I changed

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

to:

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); 
hestellezg
  • 3,309
  • 3
  • 33
  • 37
1

intellij idea 2019

  1. Download Microsoft JDBC Driver for SQL Server

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

  1. Unpack ("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 IntellijIdea classpath (look video)) add import com.microsoft.sqlserver.jdbc.SQLServerDriver; enter image description here https://youtu.be/-2hjxoRKsyk

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

0

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();
  }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
0
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();
   }
}
Syed Tabish Ali
  • 309
  • 3
  • 5
0

For me it worked after manually copying the sqljdbc4-2.jar into WEB-INF/lib folder. So please have a try on this too.

raji
  • 401
  • 1
  • 8
  • 17
0

For me it was a wrong maven dependency deceleration, so here is the correct 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>
dorony
  • 1,008
  • 1
  • 14
  • 31
0

My code base was having

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

it was pulling an older version of mssql-jdbc

I replaced it with the below dependency and it worked.

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.4.1.jre8</version>
    </dependency>
Jobin
  • 5,610
  • 5
  • 38
  • 53