3
votes

I am trying to use the MSSQL driver for JDBC. I have followed the instructions on how to add an external JAR file to the Eclipse Windows IDE as follow:

- Download the installation from here http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx (Microsoft JDBC Driver 4.0 for SQL Server)
- Extract the zip to a file location I have extracted it here (c:\MsJDBCforSqlDriver) - In Eclipse Package Explorer right click your project -> Build Path -> Configure Build Path -> Java Build Path -> Libraries Tab- Add External JAR file and then I browse to "C:\MsJDBCforSqlDriver\sqljdbc_4.0\enu\sqljdbc4.jar"
- I then view the successful reference as in image below including SQLServerDriver.class

enter image description here

When I try to execute Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") I get a ClassNotFound Exception as below:

enter image description here

I tried all suggestions I could find on Google including setting the classpath in Windows Environment Variables.

4
How are you running your application? The issue would appear to be the class path used at runtime as you have no dependency at compile time on the JDBC driver.SteveD
@SteveD - I am sure what you are asking. I am running it with the "run" button. Normal or in Debug mode. Why would the class path be any different in runtime. I am from .NET background and have to say this IDE is as confusing as hell.Pierre

4 Answers

4
votes

Under the Run -> Run Configurations menu item you should find an entry for a Java application with the same name as the class that contains your main() method.

Have a look at the classpath tab. You should find your project listed under user entries. Expand it and see if the Microsoft JDBC Jar is listed.

The class path you use for compilation need not be the same as you choose for runtime, though Eclipse usually mirrors any changes to your build path to any launch configurations.

Your spelling and case for the package and class names look correct (Java is case-sensitive on names).

And yes, learning Java and Eclipse will take effort - Eclipse is quite different from other IDEs and it takes time to get to know it.

2
votes

I found the answer here: http://code.google.com/p/android/issues/detail?id=27490 Seems like includes under Referenced Libraries is not available at runtime and I couldn't see how to change this in my version of Eclipse

" Reported by [email protected], Mar 22, 2012 Host OS: Windows 7 x64 SDK tools version: 17 Eclipse version: 3.7.2 ADT plug-in version:17.0.0v201203161636-291853 Platform targeted by your project: 2,2 Version of the platform running in the emulator: 2.2

STEPS TO REPRODUCE: 1. Create a project that uses external jars (in my case android-support-v4.jar ). 2. Added the android-support-v4.jar using Properties - Java Build Path - Add External Jar. Have the Activity extends from FragmentActivity. 3. Clean and run project

EXPECTED RESULTS: Application would run launching the FragmentActivity

OBSERVED RESULTS: Application crashes, android-support-v4.jar is placed within "References Libraries" and thus Android can't find it at runtime.

SOLUTION: Manualy create a folder /libs within your project, copy the android-support-v4.jar to that folder and the ADT will place it under 'Android Dependencies' and it works.

This is the case for all external jars added using Properties - Java Build Path - Add External Jar.

"

1
votes

Adding the JDBC driver of MS SQL Server to Eclipse:

  1. Download the JDBC driver and uncompress it
  2. Secondary click in the root of the Eclipse project and go to "Properties".
  3. Now go to the section "Java Build Path - Libreries" y then click "Add External JARs".
  4. Search the "sqljdbc.jar" file in the directory where you unpacked the downloaded driver, double click y finish pressing "ok".
0
votes

I faced the problem and then solved the issue i.e. If You are using the Tomcat as a server for your Web application, the same Exception was coming as the Driver was unavailable to the project, later I put the jar file (sqljdbc4-2.0.jar) into the WEB-INF/lib folder then it was working