Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-10-22
9
Medium Priority
?
16,954 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

927 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