wood burning stoves 2.0*
The moose likes JDBC and the fly likes How to establish JDBC Connection to SQL Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to establish JDBC Connection to SQL Server" Watch "How to establish JDBC Connection to SQL Server" New topic
Author

How to establish JDBC Connection to SQL Server

Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Here is my java class, its in the directory
C:\Documents and Settings\dmortimer\My Documents\SunCert\DB

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.lang.ClassNotFoundException;

class getDBConnection {
public static void main (String [] args) {
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Class Not Found Exception:");
System.out.println(cnfe);
}
}
}

I have downloaded the Microsoft SQL Server 2000 Driver for JDBC and its in the default location of
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC

I have also set my CLASSPATH to the following
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib

My class compiles just fine, but when I run it, I get:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

As a genuine java greenhorn, I'd appreciate some advice on why this is happening and how to correct it.

THANKS!
Ivor Lithan
Greenhorn

Joined: May 30, 2004
Posts: 10
You need to move the jar files (msbase.jar, mssqlserver.jar, msutil.jar) from the lib directory of the default install location of the drivers, i.e. C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib - into your class path, somewhere along the way.
Your class compiles fine because you don't actually reference the sql driver until run time.
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Hi Ivor,

Thanks for responding...

My CLASSPATH system variable is set to:
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib

If I need to move the .jar files that appear in this directory by default, I'm not sure where I need to put them. Please can you help...

Thanks!
Ivor Lithan
Greenhorn

Joined: May 30, 2004
Posts: 10
I have also set my CLASSPATH to the following
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib


Oops I completly missed that bit. My bad.

the only thing I can think of, off the top of my head right now, is the spaces in the file path. try moving the jars somewhere where you have no spaces in the path i.e. c:\lib or something?
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Hi Ivor,

I copied the three jar files to:
C:\SQLServerDriver\lib

Then, I updated CLASSPATH system variable to:
C:\SQLServerDriver\lib;

Then, I rebooted my machine, but I am still getting the same problem. I'm running on Windows XP, with jdk v1.4.2_03 if this makes any difference...

Any suggestions gratefully received...

Thanks
Ivor Lithan
Greenhorn

Joined: May 30, 2004
Posts: 10
ok - level of desperation time - add the jars explicitly to your class path, i.e :

C:\SQLServerDriver\lib\sqlserver.jar;C:\SQLServerDriver\lib\msutil.jar and so on
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Assumimg you are running this from the command line, echo %CLASSPATH% and make sure it is what you think it is. Then make sure you have all the MSSQL driver jars in the CLASSPATH (there's a utils.jar too I think). Also make sure you are running the app with the classpath you think you are - which is easy from the command line, with the classpath argument - but can be less clear with IDE which might be using their own CLASSPATH. If all of the above is OK, then the class will be found.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
As Paul stated above be absolutely sure your classpath is set correctly.
Also your command line should be like:



Be sure to include the "." seperated by ";" somewhere in your classpath, this will tell the compiler to look in the current working directory as well as the CLASSPATH environment variable, otherwise you will get a message similar to

java.lang.NoClassDefFoundError getDBConnection


Or you can initialize your CLASSPATH with the "."




and so on.

And then all you have to do is


I hope this helps.
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Great News! It seems to be working now. To get this far, I set my ClASSPATH variable as advised and checked it was correct at the command line as follows:

echo %CLASSPATH%
.;C:\SQLServerDriver\lib\msbase.jar;C:\SQLServerDriver\lib\mssqlserver.jar;
C:\SQLServerDriver\lib\msutil.jar

Having done this, the class compiles and runs just fine. Just for my own morbid curiosity, can you briefly explain what the ".;" actually means in the classpath? What is my current working directory?

Thanks to you all for assisting me!
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Well if I remember correctly, the "." and the ".." are DOS and UNIX symbols that refer to the CURRENT and PARENT directory respectfully.

For example in DOS:
# Copying Test.java from c:\temp to current directory

is the same is


# Move up to the Parent Directory


I think you get the idea.

Craig.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Correction:


Okay I'm done with the DOS and UNIX lessons.

Back to JAVA.

Craig.
 
jQuery in Action, 2nd edition
 
subject: How to establish JDBC Connection to SQL Server