0

Goal is to connect to MS SQL Server database using Java and perform some SQL statements. Issue while finding the class, Error: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

tried both classname com.microsoft.jdbc.sqlserver.SQLServerDriver AND com.microsoft.sqlserver.jdbc.SQLServerDriver

Classpath also set in Eclipse to:

CLASSPATH C:/Program Files/Microsoft JDBC Driver 4.1 for SQL Server/sqljdbc_4.1/enu/sqljdbc.jar

also changed win7 enviornment variables to:

.;C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc.jar;C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc4.jar;C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc41.jar;

Used this Code:

package edu.umt.oop.lecture7;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class databasepro {

    public static void main(String[] args)
      {
        Connection connection = null;
        try
        {
          // the sql server driver string
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

          // the sql server url
          String url = "jdbc:microsoft:sqlserver://C-LHE-CS-68541:1433;DatabaseName=CMSA_Console";

          // get the sql server database connection
          connection = DriverManager.getConnection(url,"sa", "123456");


          System.out.println("\nSuccess");
          // now do whatever you want to do with the connection
          // ...

        }
        catch (ClassNotFoundException e)
        {
          e.printStackTrace();
          System.exit(1);
        }
        catch (SQLException e)
        {
          e.printStackTrace();
          System.exit(2);
        }
      }


}

Complete Error thrown is :

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at edu.umt.oop.lecture7.databasepro.main(databasepro.java:15)
Abubakar Riaz
  • 320
  • 8
  • 26
  • possible duplicate of http://stackoverflow.com/questions/22253551/classpath-set-but-java-lang-classnotfoundexception-com-microsoft-sqlserver-jd – Vihar Jan 16 '15 at 04:23
  • 1
    Applications rarely use the classpath environment variables. You need to add **one** and only one of the SQL Server jars (preferably `sqljdbc41.jar`) to the build path of your application (if run from Eclipse), or to the runtime class path when run standalone – Mark Rotteveel Jan 16 '15 at 11:49

1 Answers1

1

This is due to the jar file. So download jar file from below link and this to your project library in IDE e.g. Eclipse.

Download Jar

Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24