4

我已经编写了代码以在 Spring 上使用 JDBC 运行查询,但出现异常(见下文)

这是我的 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));
  }
}

我看到的例外是:

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)

编辑:削减堆栈跟踪的其余部分,因为上述异常足以说明问题。

这里出了什么问题?

4

3 回答 3

12

Looks like you are missing the oracle jdbc driver in your classpath. Please download the jar file from this path and add this to your classpath.

EDITED
Spring jdbc is a template layer which works on top of the raw jdbc layer. It just provides us some utility methods to make the database access easier. This layer internally needs the jdbc layer to work so which every database you want to connect to that database's driver also has to be included, in your case you need to include Oracle driver.

于 2012-06-11T11:17:15.847 回答
1

看起来带有 JDBC 驱动程序的(Oracle 提供的)jar 文件不在您的类路径中。

请注意堆栈跟踪的部分内容:“IllegalStateException:无法加载 JDBC 驱动程序类 [oracle.jdbc.driver.OracleDriver”

于 2012-06-11T11:18:28.170 回答
0

在我的情况下,问题已解决,将范围设置为runtime

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.0.0.jre8</version>
        <scope>runtime</scope>
    </dependency>

参考:我什么时候需要具有运行时范围的 Maven 依赖项

于 2018-08-30T04:46:25.003 回答