jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Cannot load JDBC driver for mysql in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Cannot load JDBC driver for mysql in Servlet" Watch "Cannot load JDBC driver for mysql in Servlet" New topic

Cannot load JDBC driver for mysql in Servlet

Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66

I am attempting to write a simple Servlet. When I hit a submit button,
Tomcat tells me that it cannot find my Mysql driver.

The actual message is:

could not load JDBC driver ...ClassNotFoundException:com.mysql.jdbc.Driver

I have an actual driver (full path)

The full path and file for my web.xml is shown below

Full Path- C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\coins\WEB-INF\web.xml is:

web.xml file-
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

<!-- Define the controller servlet -->
<servlet-name>Ch3 Beer</servlet-name>
<!-- END: Define the null servlet -->

<!-- The Struts controller mapping -->
<servlet-name>Ch3 Beer</servlet-name>
<!-- END: The Struts controller mapping -->
<description>DB Connection</description>


My context.xml file has it's full path and contents listed below:

Full path:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\context.xml

The file is:
<?xml version="1.0" encoding="UTF-8"?>
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
<!-- The contents of this file will be loaded for each web application -->

<!-- Default set of monitored resources -->

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<Manager pathname="" />
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="mallory1" driverClassName="com.mysql.jdbc.Driver"


My classpath for the JDBC driver is:

I can get data from the database using non Servlet programs.
Any insight would be greatly appreciated!

Thank you,

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Usually the Driver would need to be included in the runtime classpath for the web application context, which involves putting it in the <context>/WEB-INF/lib directory


Since you're using a DataSource, which is managed by the container, you need to give the container access to th Driver. In your case you should place the JAR in the <TOMCAT_HOME>/common/lib directory and then restart
Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66

Thank you! This got me beyond this problem.

Best Wishes,

Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445

Hi David,

A quick question. Even in case there is a DataSource managed by the container wont it load the jar files in the application's WEB-INF\lib folder at the time of application startup? So my question is whether it is mandatory to have the sql driver jar in the container's lib folder in that case?

I agree. Here's the link: http://aspose.com/file-tools
subject: Cannot load JDBC driver for mysql in Servlet
It's not a secret anymore!