专栏首页Jerry的SAP技术分享Could not load JDBC driver class [com.mysql.jdbc.Driver]

Could not load JDBC driver class [com.mysql.jdbc.Driver]

我使用Spring JDBC编程时,遇到一个错误消息:Could not load JDBC driver class [com.mysql.jdbc.Driver]

完整的错误为:

Jul 27, 2020 12:22:26 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b80350b: startup date [Mon Jul 27 12:22:26 CST 2020]; root of context hierarchy
Jul 27, 2020 12:22:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Jul 27, 2020 12:22:26 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [Beans.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]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [Beans.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:1650)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1357)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.sap.MainApp.main(MainApp.java:10)
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:123)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1646)
	... 13 more

解决方法:

下载MySQL Connector/J(the official JDBC driver for MySQL). https://dev.mysql.com/downloads/connector/j/

将下载的mysql-connector-java.jar添加到Java build path后问题解决:

使用mySQL client命令行可以看到之前用Java JDBC插入的数据库条目:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Spring 基于设值函数的依赖注入

    为什么Spring IOC容器,如此智能地知道调用TextEditor的setSpellChecker方法呢?

    Jerry Wang
  • Spring 基于注解(annotation)的配置之@Required注解

    从 Spring 2.5 开始就可以使用注解来配置依赖注入。注解连线在默认情况下在 Spring 容器中不打开。因此,在可以使用基于注解的连线之前,我们将需要在...

    Jerry Wang
  • Spring 自动装配模式之构造函数装配方式

    这种模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置...

    Jerry Wang
  • spring+quartz集成启动报错 原

    尚浩宇
  • Failed to instantiate org.mybatis.spring.SqlSessionTemplate Constructor threw exception

    数据库JDBC链接失败,检查下用户名和密码是否正确,我这里是因为密码copy时候copy错了,导致的。

    MickyInvQ
  • Spring 容器 17 个常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点:

    江南一点雨
  • 你清楚这几个 Spring 常用注解吗?

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点:

    一个优秀的废人
  • Spring 基于设值函数的依赖注入

    为什么Spring IOC容器,如此智能地知道调用TextEditor的setSpellChecker方法呢?

    Jerry Wang
  • Spring实战——通过Java代码装配bean

      上篇说的是无需半行xml配置完成bean的自动化注入。这篇仍然不要任何xml配置,通过Java代码也能达到同样的效果。   这么说,是要把上篇的料拿出来再煮...

    JackieZheng
  • nested exception is java.lang.IllegalStateException: refreshAfterWrite requires

    已解决 nested exception is java.lang.IllegalStateException: refreshAfterWrite requi...

    谙忆

扫码关注云+社区

领取腾讯云代金券