Link to home
Start Free TrialLog in
Avatar of fholmx
fholmx

asked on

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();
       }
}
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

> 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...
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Put the jars in *all* the lib\ext directories
Avatar of jimmack
jimmack

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 ;-)
Avatar of fholmx

ASKER

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!
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();
       }
}
You are probably beset putting this in a seperate question rather than the bottom of this one :-)

Good luck!

Tim
beset == best

:-/