Solved

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

Posted on 2003-10-22
9
16,763 Views
Last Modified: 2011-08-18
I am trying to connect to a DB in java using MSSQL server.  I have followed the steps; my CLASSPATH environment variable is currently set to:

CLASSPATH=.;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar

To verify that I have set the variable correctly I went to the command prompt and typed:

javap com.microsoft.jdbc.sqlserver.SQLServerDriver

and got:

No sourcepublic class com.microsoft.jdbc.sqlserver.SQLServerDriver extends com.microsoft.jdbc.base.BaseDriver {
    static {};
    public com.microsoft.jdbc.sqlserver.SQLServerDriver();
    public static void main(java.lang.String[]);
}

as a response which should indicate that my CLASSPATH is set up correctly.  In spite of this I am still getting a:

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
      at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:140)
      at DBConnect.getConnection(DBConnect.java:30)
      at DBConnect.displayDbProperties(DBConnect.java:48)
      at DBConnect.main(DBConnect.java:85)

I have tried copying the mssqlserver.jar, msutil.jar and msbase.jar in the lib/ directory of my JDK installation folder but that did not help either.  Suggestions anyone?  Here is my source code:

import java.*;

public class DBConnect
{
     private java.sql.Connection  con = null;
     private final String url = "jdbc:microsoft:sqlserver://";
     private final String serverName= "localhost";
     private final String portNumber = "1433";
     private final String databaseName= "avato";
     private final String userName = "sa";
     private final String password = "";

     // Informs the driver to use server a side-cursor,
     // which permits more than one active statement
     // on a connection.
     private final String selectMethod = "cursor";

     // Constructor
     public DBConnect(){}

     private String getConnectionUrl()
     {
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }

     private java.sql.Connection getConnection()
     {
          try{
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
               con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }
          return con;
      }

     /*
          Display the driver properties, database details
     */

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               con= this.getConnection();
               if(con!=null){
                    dm = con.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    }
                    rs.close();
                    rs = null;
                    closeConnection();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e)
          {
               e.printStackTrace();
          }
          dm = null;
     }

     private void closeConnection(){
       try{
               if(con!=null)
                    con.close();
               con=null;
          }catch(Exception e){
               e.printStackTrace();
          }
     }
     public static void main(String[] args) throws Exception
       {
          DBConnect myDbTest = new DBConnect();
          myDbTest.displayDbProperties();
       }
}
0
Comment
Question by:fholmx
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9598114
> CLASSPATH=.;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar

I don't think java likes having spaces on the classpath :-/

can you try:

CLASSPATH=.;c:\progra~1\Micros~1\lib\msbase.jar;c:\progra~1\Micros~1\lib\msutil.jar;c:\progra~1\Micros~1\lib\mssqlserver.jar

assuming

c:\progra~1\Micros~1

does get you to the right directory...
0
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9599359
Or you could move the jar file.

You want it in the ..\jre\lib\ext directory, not \lib.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9601087
Put the jars in *all* the lib\ext directories
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9601131
CEHJ: Which other lib\ext directories do you mean?

I can only see one other on my system and I don't think it would be an appropriate one.

Just asking ;-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:fholmx
ID: 9603930
TimYates,

I tried the path thing but I could not find the ^%$! dos equivilant directory for my SQL JDBC driver installation.  Either way I don't think the CLASSPATH was the problem as my javap command executed successfully.

I tried putting the .JAR files in the jre/lib/ext and that solved the problem.  What is astounding is that I followed microsofts instructions, line by line, but there was no mention AT ALL with regard to displacing the .JAR files into the jre/lib/ext.

Now its on to the next bug as I am getting a lovely:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

Let me know if any of you know anything about connecting to a DB using MS SQL and I'll officially post this question for some easy points.  Thanks for the help guys!
0
 

Expert Comment

by:joeabbey
ID: 12381296
I am do the exact programming to test my Ms Sql server with JDBC connection, and I am having the following errors:

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:141)
        at examples.Connect.getConnection(Connect.java:42)
        at examples.Connect.displayDbProperties(Connect.java:60)
        at examples.Connect.main(Connect.java:96)
Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
Error: No active Connection

Here is my code, I need to get working on project that involves reading from Sql Server.

package examples;

import java.sql.*;
import java.*;
import java.util.*;
import javax.sql.*;

public class Connect{
     private java.sql.Connection  con = null;
     private final String url = "jdbc:microsoft:sqlserver//";
     private final String serverName= "local";
     private final String portNumber = "1433";
     private final String databaseName= "university";
     private final String userName = "joe";
     private final String password = "abbey";
     // Informs the driver to use server a side-cursor,
     // which permits more than one active statement
     // on a connection.
     private final String selectMethod = "cursor";

     
   
     // Constructor
     public Connect(){}

     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }

     private java.sql.Connection getConnection(){
          try{
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
               con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }
          return con;
      }

     /*
          Display the driver properties, database details
     */

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               con= this.getConnection();
               if(con!=null){
                    dm = con.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    }
                    rs.close();
                    rs = null;
                    closeConnection();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e){
               e.printStackTrace();
          }
          dm=null;
     }

     private void closeConnection(){
          try{
               if(con!=null)
                    con.close();
               con=null;
          }catch(Exception e){
               e.printStackTrace();
          }
     }
     public static void main(String[] args) throws Exception
       {
          Connect myDbTest = new Connect();
          myDbTest.displayDbProperties();
       }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12381312
You are probably beset putting this in a seperate question rather than the bottom of this one :-)

Good luck!

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12381315
beset == best

:-/
0
 

Expert Comment

by:tienthinh
ID: 26093399
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now