Date Created: Fri 21-Jan-2011

Related Document Categories:



    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.

Middleware Mentor - Steven Charles Robinson

About Me

Steve Robinson has been working in IT for over 15 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware consulting. Steve comes from both an administration and development background.

Before moving to JEE, Steve was an accomplished developer and consultant for both IBM Lotus Notes and Microsoft .NET Technologies.

Follow Steve as @stevencrobinson on twitter.

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

WebSphere Categories

Oracle WebLogic Categories

JBoss Categories

Other Categories