Error creating bean ... failed to load JDBC driver class [oracle.jdbc.driver.OracleDriver]

I wrote code to run a query using JDBC on Spring, but I get an exception (see below)

Here is my context.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@Mohsen-PC:1521:mydb"/> <property name="username" value="system"/> <property name="password" value="123"/> </bean> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"> <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/> </bean> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> </beans> 

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 that 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) 

EDIT: Cut the remainder of the stack trace, as the above exception is sufficient to cover the problem.

What's going on here?

+4
source share
2 answers

It looks like you are missing the oracle jdbc driver in your classpath. Download the jar file from this path and add it to your classpath.

EDITED
Spring jdbc is a template layer that runs on top of the original jdbc layer. It just provides us with some useful methods to facilitate access to the database. This layer internally requires that the jdbc layer work so that every database that you want to connect to this database driver must also be enabled, in your case you need to enable the Oracle driver.

+12
source

It looks like the jar file (provided by Oracle) with the JDBC driver is not in your class path.

Note the portion of the stack trace that states: "IllegalStateException: Failed to load the JDBC driver class [oracle.jdbc.driver.OracleDriver"

+1
source

All Articles