0

I connected a SQL server and its driver to my Dynamic Web project. I created a test class (not part of the website) that successfully connects to my database.

However, when I try to connect to my database in my website (in a java servlet actually) and run it on my server/Apache Tomcat I get the exception:

class not found

I don't understand why this is happening. My server is working, and the database connection works in a regular java class. Do I have to set up something else when using a SQL server on a (Tomcat) web server?

Any help would be much appreciated!

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
SGolds
  • 119
  • 2
  • 16

2 Answers2

1

I suppose you are using a JDBC driver to connect to your SQLserver from the servlet.

Make sure you have copied the JDBC jar file into the Tomcat app you are actually running the servlet from. Ensure that you are accessing that servlet in particular from your front-end (web site) and not another servlet by mistake which does not have the JDBC classes in its class path:

Typically the jar file goes into webapps/your_app_name/WEB_INF/lib

can you share the stack trace, on which class is your code breaking?

Apollo
  • 46
  • 2
0

If you are using Maven then you may need to remove the <scope> notation. I was using eClipse, Maven, MSSql. My pom.xml shows the dependancy was grey and marked as test. I removed the line and it worked great.

Change:

    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.1.4.jre8-preview</version>
        <scope>test</scope>
    </dependency>

to:

    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.1.4.jre8-preview</version>
    </dependency>
Chris Hagn
  • 21
  • 1