Hi All,

I'm trying to use jdbc connection with sqlServer 2000. I'm using j2sdk1.4.2. And i have service pack2.

I've installed the .jar files mssqlserver.jar, msutil.jar, msbase.jar.

I have these jar files in E:\j2sdk1.4.2_04\lib.

I'm trying to connect with the following program.

import java.*;
import java.io.*;

public class Connect{
     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= "master";
     private final String userName = "Z";
     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 Connect(){}
     
     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     private java.sql.Connection getConnection(){
          try{
			//  Driver d = (Driver)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
              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();
       }
}

when i compile i'm not getting any error...
but when i run it .. i'm getting this error...

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Connect.getConnection(Connect.java:28)
at Connect.displayDbProperties(Connect.java:46)
at Connect.main(Connect.java:82)
Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
Error: No active Connection

I'd be greatful if anyone can help me come out of this :sad:

If so, then not correctly, or you misspelled the class or package name.

A ClassNotFoundException will only occur in one of three instances:

1. The Class is not anywhere on the CLASSPATH
2. The Class name is misspelled (which, essentially, falls back to #1)
3. The package "path" is wrong/misspelled (which also, essentially, falls back to #1)

Edit: Also, do not rely on the System CLASSPATH environment variable, containers won't use it. If running from a container, then include the library either in the common/lib or shared/lib (or equivalent depending on the server) or the WEB-INF/lib directory, or include a reference to it in the Manifest file when running from a jar (using -jar) or use the -cp command line option. (When running from an IDE, add it as an external library/jar in the project properties.)

Hi,

I have my class path setup and i'm still getting this errror.
I am using eclips and jsp.

here is a paste of my class path:
C:\Program Files\Java\jre6\lib;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;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;.

and my install directory:
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib

I did copy them into:
C:\tomcat\webapps\Reporting\WebContent\WEB-INF\lib

my java direcories are:
C:\Program Files\Java\jdk
C:\Program Files\Java\jre6

where Reporting is the project name.

any advice would be great.
thanks.

p.s. I'm on a vista laptop.

I have writen a simple test using the java compiler and I get the this result.

import java.util.*;
import java.text.*;
import java.sql.*;
import java.io.*;
import java.*;


class test
{
public static void main(String args[]){
System.out.print("Start");
try
{



Connection conn = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://DESKTOP;databasename=cssd_reporting_tool","sa","05330921");
Statement command = conn.createStatement();
String fieldSelectStatement = "SELECT * FROM tblUsers where UserName='nxc018'";
ResultSet rs = command.executeQuery(fieldSelectStatement);
rs.next();
System.out.print("OK");
System.out.print(rs.getString(2));


conn.close();
}
catch(SQLException sqle)
{
System.out.print(sqle.getMessage());
System.out.print("Sql error");


}catch(ClassNotFoundException cnfe){
System.out.print(cnfe.getMessage());
System.out.print("class not found error");


}
System.out.print("finnish");


}
}



C:\>java test
Start[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.Sql errorfinnish
C:\>

Edited 3 Years Ago by happygeek: fixed formatting

That is not the same error.

For that one, make sure that your SQL Server is setup to respond to TCP IP connection attempts (per default, it is, AFAIK, not).

i know.
thanks
but i need to figure out the first one.
I believe it has something to do with Eclips.

Reread the last sentence (the one in parenthesis) of reply #4.

Also, reread the part of that same post that says not to rely on the system CLASSPATH environment variable as containers (of which all IDEs are) will not use it.

I did, and I put the jar files in the Web-info directory but it didn't work.
that's why i stated i was using eclips.
I was hoping someone knew if there was something i can set in there that would help.

there are many web-inf folders in the tomcat directory for some reason.

thanks

Are you running from tomcat? If so, then place it in the common/lib or shared/lib (I can't remember which it is). Depending on how you configure your datasource web-inf/lib may be loaded from a different (and later in the chain) classloader than the datasource.

If you are running from Eclipse, currently, than, as I said in that last sentence, add it as an external library/jar to the project properties/preferences in Eclipse.

Once again, the only thing that causes that error, is that the class is not on the classpath for the classloader concerned.

There are three classloaders in every application. The bootstrap classloader (which loads rt.jar and the security packages), the system classloader (which loads the core java extension packages), and the application classloader (which loads the rest of the classpath). In web containers, each application also gets a fourth classloader for the classes contained within the application directory.

Now, depending on how you configured your datasource, it may be being loaded from the application classloader (which would mean that you need the jar in (common/shared)/lib) or the web applications classloader, which would then allow you to place the jar in the web-inf/lib directory. If you are actually running from tomcat, then it appears as though you need it in (common/shared)/lib (and I don't know why you haven't tried that already when it is mentioned in reply #4).

And see above if currently running from eclipse.

Thanks

That fixed the class not found error.
I missed that some how. i was looking for the web-inf directory.
I'm new to java. I was an ASP.net dev and not use to doing all the work lol.
java is great but I'm not good at building the I.U.
It's so much easer in visual studio.
and yeah i'm using tomcat.

Thanks again.
now I just need to figure out why sql 2000 isn't letting me in ofer my network.

Great forum.
I hope it's not blocked at work.

I have resolved this problem now.

Thanks

Although every time I make a call to the SQL server it take over 30 seconds ro get a responce. At work the pages load in one second.
5 records max


Any tips?

Could be any number of things. Add a few "log" time statements to make a preliminary determiniation of where it's "bottlenecking".

Hi All,

I'm trying to use jdbc connection with sqlServer 2000. I'm using j2sdk1.4.2. And i have service pack2.

I've installed the .jar files mssqlserver.jar, msutil.jar, msbase.jar.

I have these jar files in E:\j2sdk1.4.2_04\lib.

I'm trying to connect with the following program.

import java.*;
import java.io.*;

public class Connect{
     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= "master";
     private final String userName = "Z";
     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 Connect(){}
     
     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     private java.sql.Connection getConnection(){
          try{
			//  Driver d = (Driver)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
              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();
       }
}

when i compile i'm not getting any error...
but when i run it .. i'm getting this error...

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Connect.getConnection(Connect.java:28)
at Connect.displayDbProperties(Connect.java:46)
at Connect.main(Connect.java:82)
Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
Error: No active Connection

I'd be greatful if anyone can help me come out of this :sad:

Instead of using msbase.jar.... files add sqljdbc.jar and slightly change your driver name as com.microsoft. sqlserver .jdbc.SQLServerDriver Surely it will work

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:-

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Driver;
import java.sql.ResultSet;
public class Example
{
    public static void main(String args[])
    {
        Connection con=null;
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=microleasing;"; 
            con=DriverManager.getConnection(url,"sa","*********");
            PreparedStatement ps1=con.prepareStatement("select * from login_details");
            ResultSet rs1=ps1.executeQuery();
            while(rs1.next())
            {
                System.out.println("result set is not empty");
            }
        }
        catch(Exception e)
        {
            System.out.println("Generated exception is "+e);
        }
    }   
}

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.

Friend, your debugging informaiton is:

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

May be the woking code below will hints you:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance()

Edited 3 Years Ago by parsifal

parsifal: this question was posted 5 years ago. no use of trying to solve it now. either it has been solved quite a while ago, or the OP stopped looking.

This article has been dead for over six months. Start a new discussion instead.