• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot load JDBC driver for mysql in Servlet

 
Ken Rubin
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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)
C:\mysql-connector-java-5.0.7\mysql-connector-java-5.0.7-bin.jar

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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<!-- Define the controller servlet -->
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.CoinSelect</servlet-class>
</servlet>
<!-- END: Define the null servlet -->

<!-- The Struts controller mapping -->
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectCoin.do</url-pattern>
</servlet-mapping>
<!-- END: The Struts controller mapping -->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


</web-app>

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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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 -->
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- 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"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>


</Context>

My classpath for the JDBC driver is:
C:\mysql-connector-java-5.0.7\mysql-connector-java-5.0.7-bin.jar

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

Thank you,

Ken
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

BUT

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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

Thank you! This got me beyond this problem.

Best Wishes,

Ken
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Cheers,
Raj.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic