Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login Win a copy of Arduino in Action this week in the General Computing forum! A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

connecting to an MS SQL server

rik antropov
Greenhorn

Joined: Feb 26, 2007
Posts: 7
trying to connect to ms sql server 2005 express edition from a java program.
got the sqljdbc.jar file from microsoft, and put it in the same folder as the java file. i added this jar file to the classpath and used an IDE to compile the java file. compiling was fine, but when i try to run it from the cmd line (windows XP) it gives me this:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.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.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at UserMS.main(UserMS.java:36)

if i try to use -classpath in the command like java -classpath C:\blah blah
then it throws

Exception in thread "main" java.lang.NoClassDefFoundError: UserMS

in my code, i use Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

Has anyone gotten anything like this? Any help would be appreciated.

qin_23
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443

9
The classpath needs to include the directory where your application is located, so it should probably look like:

java -classpath .;c:\path\to\mssql-driver.jar UserMS

if UserMS.class is in the current directory.

rik antropov
Greenhorn

Joined: Feb 26, 2007
Posts: 7
ok, that worked. thanks ulf.

I agree. Here's the link: http://aspose.com/file-tools

subject: connecting to an MS SQL server