Share via


java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

Question

Saturday, August 18, 2012 6:43 AM

Hello Friends. i have set my environmental variable path of Microsoft JDBC Driver 4.0 as C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar and i have also used the url "com.microsoft.sqlserver.jdbc.SQLServerDriver" in my Class.forName(). But still at run time i get the exception java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver. I am using Microsoft sql server 2005 as my database and I am using Textpad4 as the platform to write the code. I have already enabled the TCP/IP protocol of MSSQLSERVER of SQL server configuration manager. Below I am giving the codes:-

1.    import java.sql.DriverManager;
2.    import java.sql.Connection;
3.    import java.sql.PreparedStatement;
4.    import java.sql.Driver;
5.    import java.sql.ResultSet;
6.    public class Example
7.    {
8.        public static void main(String args[])
9.        {
10.            Connection con=null;
11.            try
12.            {
13.                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
14.                String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=microleasing;";
15.                con=DriverManager.getConnection(url,"sa","*********");
16.                PreparedStatement ps1=con.prepareStatement("select * from login_details");
17.                ResultSet rs1=ps1.executeQuery();
18.                while(rs1.next())
19.                {
20.                    System.out.println("result set is not empty");
21.                }
22.            }
23.            catch(Exception e)
24.            {
25.                System.out.println("Generated exception is "+e);
26.            }
27.        }   
28.    }

But i get exception as:- Generated exception is java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

I will be greatful if anyone will help me come out of this sad. please please plaese help me. I am using jdk "1.6.0_16" version.

All replies (1)

Monday, August 20, 2012 3:42 AM âś…Answered

Hi Javabeginner,

The CLASSPATH variable is the search string that Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not listed in your CLASSPATH variable, you receive the following error message when you try to load the driver:
java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver

The JDBC driver is not part of the Java SDK. If you want to use it, you must set the classpath to include the sqljdbc.jar file or the sqljdbc4.jar file. If the classpath is missing an entry for sqljdbc.jar or sqljdbc4.jar, your application will throw the common "Class not found" exception.

The sqljdbc.jar file and sqljdbc4.jar file are installed in the following location:
<installation directory>\sqljdbc_<version>\language>\sqljdbc.jar
<installation directory>\sqljdbc_<version>\language>\sqljdbc4.jar

The following is an example of the CLASSPATH statement that is used for a Windows application:
CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar
The following is an example of the CLASSPATH statement that is used for a Unix/Linux application:
CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_4.0/enu/sqljdbc.jar
You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either sqljdbc.jar or sqljdbc4.jar.

For more information, please see:
http://support.microsoft.com/kb/313100
http://msdn.microsoft.com/en-us/library/ms378526.aspx

Best Regards,
Iric
Please remember to mark the replies as answers if they help and unmark them if they provide no help.