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

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();
       }
}
fholmxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
> 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
jimmackCommented:
Or you could move the jar file.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Put the jars in *all* the lib\ext directories
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

jimmackCommented:
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
fholmxAuthor Commented:
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
joeabbeyCommented:
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
TimYatesCommented:
You are probably beset putting this in a seperate question rather than the bottom of this one :-)

Good luck!

Tim
0
TimYatesCommented:
beset == best

:-/
0
tienthinhCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.