当前位置: 首页 > 面试题库 >

无法加载JDBC驱动程序。为什么?(spring,hibernate)

唐腾
2023-03-14
问题内容

我已经在两周前创建了一个程序(SPRING+
HIBERNATEMaven,现在它在加载JDBC驱动程序时遇到了问题(到目前为止还没有发生)。因此,我的pom.xml文件如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spring.hib</groupId>
    <artifactId>HibPav</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.27</version>
</dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-hibernate3</artifactId>
      <version>2.0.5</version>
    </dependency>

        <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
    <version>1.1</version>
    </dependency>

     <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>3.2.5.RELEASE</version>
       </dependency>

    <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>4.2.7.Final </version>
    </dependency>


    </dependencies>

</project>

看来这个文件很清楚。但是,在我尝试创建dataSourcebean 时无法加载JDBC驱动程序的主要问题是什么。

其它文件:

数据源文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>properties/database.properties</value>
    </property>
</bean>

<bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

</beans>

属性文件

jdbc.driverClassName=com.mysql.jdbc.Driver;
jdbc.url=jdbc:mysql://localhost/mydb
jdbc.username=myinfuser
jdbc.password=myinfuserpw

错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/database/datasource.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 [com.mysql.jdbc.Driver;]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.spring.dirba.Main.main(Main.java:11)
Caused by: 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 [com.mysql.jdbc.Driver;]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
    ... 16 more

任何人?

提前致谢


问题答案:

摆脱;财产的尽头

jdbc.driverClassName=com.mysql.jdbc.Driver;

Spring使用反射和该属性的值Class通过完全限定的类名加载实例。没有所谓的班级com.mysql.jdbc.Driver;。有一个叫做的类com.mysql.jdbc.Driver,因此可以使用它。

jdbc.driverClassName=com.mysql.jdbc.Driver


 类似资料:
  • 我在为Em申明德比加载JDBC驱动程序时遇到了问题。以下是我编译和运行我的程序的案例 > 案例1: 编译:E:\java\WorkReminder Run: E:\java\WorkReminder 错误: 无法加载JDBC驱动程序org . Apache . derby . JDBC . embedded driver。请检查您的类路径。Java . lang . classnotfoundex

  • 问题内容: 我在春季配置了一个netezza db。我在类路径中添加了依赖的nzjdbc.jar 春季配置: 问题答案: 将放入您的本地Maven存储库 (在该目录执行这个位于 然后像正常依赖项一样使用它: @请参阅安装第三方JAR的指南

  • 即使驱动程序类名已定义,我也会收到此错误 Java语言lang.IllegalStateException:无法加载驱动程序类:com。mysql。jdbc。组织的驱动程序。springframework。util。明确肯定state(Assert.java:392)~[spring-core-4.2.1.RELEASE.jar:4.2.1.RELEASE]位于org。springframewor

  • 我正在上使用。我试图弄明白如何使用数据导入从加载数据。但我最终无法加载JDBC驱动程序类。以下是我所做的: 把放到 1.配置: 2.配置: 在

  • 问题内容: 有人告诉我,加载JDBC驱动程序的首选方法是: 我知道,这对于从XML配置文件或用户输入中读取多个驱动程序之间的动态决策更好。我很好奇的是,调用此语句如何将指定的驱动程序加载到我们什至没有将生成的“ Class”对象存储在任何地方的环境中。JavaDocs条目说: 返回与具有给定字符串名称的类或接口关联的Class对象 在那种情况下,Java开发人员仅凭此语句如何设法促进驱动程序对象的

  • 我正在运行以下代码,但是我得到了关于Oracle类名称的错误。我已经用oracle jar文件设置了classpath环境变量,但它不起作用。有人能帮我吗?我不知道还能做什么。我真的很感谢你的帮助 这就是错误:由:java引起。sql。SQLException:无法加载JDBC驱动程序类“oracle”。jdbc。驾驶员OracleDriver's