JDBC driver class not found while creating bean

To include a downloaded jar file in a project, place it in the lib folder and add it to the project’s build path by navigating to the jar location under Project name -> Build Path -> Add Libraries. If you are using a mule-maven project, adding the ojdbc14.jar file to the POM.xml has resulted in build failures and errors similar to those found in this Stack Overflow post.

Question:

My JDBC query code on Spring is resulting in an exception (refer to the following).

Here’s my context.xml

 
   
   
   
       
 
 
   
 
 
 
   
 

main.java

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml");
    DataSource dataSource = (DataSource) ac.getBean("dataSource");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    System.out.println(jdbcTemplate.queryForList("select EMPLOYEE_ID from EMPLOYEE", 
                                                  Long.class));
  }
}

The exception I see is:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'dataSource' defined in class path resource [context.xml]: 
  Error setting property values; nested exception is 
  org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
  PropertyAccessException 1: org.springframework.beans.MethodInvocationException: 
  Property 'driverClassName' threw exception; nested exception is 
  java.lang.IllegalStateException: Could not load JDBC driver class 
  [oracle.jdbc.driver.OracleDriver] at 
  org.springframework.beans.factory.support.
                             AbstractAutowireCapableBeanFactory.applyPropertyValues(
                                                AbstractAutowireCapableBeanFactory.java:1396)

The remaining stack trace has been removed as the previously mentioned exception provides enough information to identify the issue.

What is going wrong here?


Solution 1:

It appears that the oracle jdbc driver is not included in your classpath. You can obtain the jar file from the specified location and add it to your classpath.

Spring jdbc serves as a template layer that simplifies database access by providing utility methods. However, this layer relies on the raw jdbc layer to function, meaning that you must also include the appropriate database driver, such as the Oracle driver, to connect to the desired database.


Solution 2:


The reason you are unable to access the JDBC driver could be that the jar file provided by Oracle is missing from your classpath.

Observe the section of the stack trace which indicates an “IllegalStateException”, where the cause is the inability to perform a specific action identified as ”
load JDBC driver class
” with a corresponding identifier of ”
oracle.jdbc.driver.OracleDriver
“.


Solution 3:


The issue was resolved by assigning the scope to

runtime

.

    
        com.microsoft.sqlserver
        mssql-jdbc
        7.0.0.jre8
        runtime
    

At what point would the use of a maven dependency with runtime scope be necessary?

Frequently Asked Questions