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)
2
  • possible duplicate of stackoverflow.com/questions/22253551/… Commented Jan 16, 2015 at 4: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 Commented Jan 16, 2015 at 11:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.