Two Laptop Bag*
The moose likes JDBC and the fly likes connecting to an MS SQL server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "connecting to an MS SQL server" Watch "connecting to an MS SQL server" New topic
Author

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.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 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: 41137
    
  45
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.


Ping & DNS - my free Android networking tools app
rik antropov
Greenhorn

Joined: Feb 26, 2007
Posts: 7
ok, that worked. thanks ulf.
 
 
subject: connecting to an MS SQL server
 
Similar Threads
NoClassDefinitionFound...
SQL Server 2000 Database Connection using JDBC
Creating a Class with a Static Method
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
eclipse issue(s)?