GeekLondon.com Help icon Syndication Feed icon 

Cannot create JDBC driver of class '' for connect URL 'null'

So you're using Tomcat and you can't get JNDI set up right. When you try to retrieve the DataSource, you keep getting this error message: Cannot create JDBC driver of class '' for connect URL 'null'

While it is possible that you've got some more exotic problem here, you're probably in luck.


Driver problems

First check to see if the rest of the error message gives you further clues. For example, if you see something like this:

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null',
cause: java.sql.SQLException: No suitable driver

Then you are usually looking at a simple classpath problem - put the appropriate driver JAR file into the Tomcat common/lib directory. You also need to make sure that you've called the Class.forName() method with the class name of the appropriate driver (actually this is no longer necessary with JDBC4 compliant drivers).

Otherwise check to make sure that the connection URL you have provided is correct - the DriverManager determines which Driver to load on the basis of this, so if you meant to configure a URL of jdbc:mysql://localhost/MYDB and actually provided one of jdbc:nysql://localhost/MYDB the application will not be able to find the non-existant nySQL driver even if the mySQL driver is in all the right places.


JNDI problems

If not, then there is still a very good chance that the problem is extremely simple. You are probably using Tomcat 5.5 and your data source is configured via XML using the Tomcat 5.0 or 4.x syntax.

That is, your configuration probably looks something like this:


<Resource name="jdbc/MYDS" auth="Container" type="javax.sql.DataSource"/>
   <ResourceParams name="jdbc/HCD">
      <parameter>
         <name>username</name>
         <value>myuser</value>
      </parameter>
      <parameter>
         <name>password</name>
         <value>mypass</value>
      </parameter>
      <parameter>
         <name>driverClassName</name>
         <value>com.mysql.jdbc.Driver</value>
      </parameter>
      <parameter>
         <name>url</name>
         <value>jdbc:mysql://localhost/MYDB</value>
      </parameter>
   </ResourceParams>

</Context>

And it should look something like this:

<Resource
   name="jdbc/MYDS"
   auth="Container"
   type="javax.sql.DataSource"
   username="myuser"
   password="mypass"
   driverClassName="com.mysql.jdbc.Driver"
   url="jdbc:mysql://localhost/MYDB"
/>

Admittedly this is easier to read, and it's probably easier to parse (in order to call the appropriate set methods on the driver by reflection), or just makes the config parsing more consistent - but it's a shame that it's caught so many people out, because configuration of your datasource by JNDI is the Right Thing To Do and this may have discouraged some novices.

Incidentally, we can't really blame the Tomcat guys, as they do highlight (literally) the problem with the phrase Please note that JNDI resource configuration has changed somewhat between Tomcat 5.0.x and Tomcat 5.5.x, but unfortunately it doesn't also appear in the old configuration documents, and I think people tend to assume that the 5.5 configuration won't have changed from the 5.0 configuration.

So, anyway, if you're using another driver, or need to configure some other parameters not shown above, remove the ResourceParams element and its contents. Instead add all of the parameters previously configured there as attributes of the Resource - i.e.

<parameter>
   <name>foo</name>
   <value>bar</value>
</parameter>

in the ResourceParams element becomes

<Resource ... foo="bar" ... />

in the Resource element.

Posted at Feb 22, 2007 5:35:29 PM, and last updated Feb 25, 2007 5:03:26 PM
Section separator

Add Comments