Date Created: Fri 21-Jan-2011

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/



Tomcat SQLNestedException Cannot load JDBC driver class for MySQL


I was trying out a demo application for ZK which uses JNDI and I go this error as shown below. Solution is explained below.




Error in Tomcat / Clipse Console:

SEVERE: >>org.zkoss.zk.ui.UiException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
>>org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
>>java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
>> at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

Code

public void addCustomer(Customer customer) throws Exception {
javax.sql.DataSource ds = (javax.sql.DataSource) new InitialContext()
.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
PreparedStatement stmt = null;
try {
stmt = conn
.prepareStatement("INSERT INTO customer (name, date) values(?, ?)");

// insert what end user entered into database table
stmt.setString(1, customer.getName().toUpperCase());

stmt.setDate(2, customer.getDate());

// execute the statement
stmt.executeUpdate();
} finally { // cleanup
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
System.out.println(ex); // log and ignore
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
System.out.println(ex); // log and ignore
}
}
}

}

context.xml

<Resource driverClassName="com.mysql.jdbc.Driver"
maxActive="4" maxIdle="2" maxWait="5000" auth="Container"
name="jdbc/mysql" password="zkcustomer" type="javax.sql.DataSource"
url="jdbc:mysql://www.gonbol.com:3306/zkcustomer" username="zkcustomer"/>

web.xml

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Option to resolve


Solution:

Copy the MYSQL Connector JAR file to Tomcat.(mysql-connector-java-5.1.13-bin ws the latest one I downloaded) it is required because we are using JNDI lookups and this means the contain needs to know how to connect using the driver as the code does not implement the driver. This is correct behaviour for Enterprise thinking and applications developed for more mature Web Containers ie WebSphere, WebLogic.

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many leading brands around the world. Steve specialises in JEE, DevOps and Thought Leadership.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


  • Linked In
  • Twitter
  • About Me
  • Contact Me

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

Courses

  • WebSphere Courses

Articles

WebSphere Categories

Other Categories