【问题标题】:No suitable driver found for jdbc:mysql from tomcat 8.0从 tomcat 8.0 找不到适合 jdbc:mysql 的驱动程序
【发布时间】:2015-04-08 11:36:57
【问题描述】:

我正在尝试在 tomcat 8.0 中应用程序,但出现错误

org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException: No suitable driver found for jdbc:mysql://ip:3306/DURGA_DEV?characterEncoding=UTF-8

at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:737) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]

我正在使用

  1. Tomcat 8.0.21
  2. 春季 4.1.6
  3. Mysql 连接器:mysql-connector-java-5.1.27.jar

我的 context.xml 文件如下所示。

context.xml

<?xml version="1.0" encoding="UTF-8"?>



<beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:p="http://www.springframework.org/schema/p"
                xmlns:util="http://www.springframework.org/schema/util"
                xmlns:tx="http://www.springframework.org/schema/tx" 
                xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <bean id="datasourceProperties" 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:META-INF/config/datasource.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
  </bean>
  <bean id="batchUpdateDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName">
            <value>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</value>
        </property>

        <property name="url">
            <value>${jdbc.components.url.DURGA}</value>
        </property>

        <property name="username">
            <value>${jdbc.components.userName.DURGA}</value>
        </property>

        <property name="password">
            <value>${jdbc.components.password.DURGA}</value>
        </property>

 </bean>

  <bean id="batchJDBCTemplate" class="com.nri.durga.maf.batch.MafBatchJdbcTemplate">
    <constructor-arg
        type="org.springframework.jdbc.datasource.DriverManagerDataSource"
        ref="batchUpdateDataSource" />
 </bean>
  <bean id="GLOBAL" 
        class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        p:URL="${jdbc.components.url.GLOBAL}"
        p:user="${jdbc.components.userName.GLOBAL}"
        p:password="${jdbc.components.password.GLOBAL}">        
  </bean>
  <bean id="DURGA" 
        class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        p:URL="${jdbc.components.url.DURGA}"
        p:user="${jdbc.components.userName.DURGA}"
        p:password="${jdbc.components.password.DURGA}">        
  </bean> 

  <bean id="global-tm" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="global-em" />
    <property name="dataSource" ref="GLOBAL" />
  </bean>
  <bean id="durga-tm" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="durga-em" />
    <property name="dataSource" ref="DURGA" />
  </bean>  
  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false" />
    <property name="generateDdl" value="false" />
    <property name="databasePlatform" value="${jpa.hibernate.dialectClass}" />
  </bean>

  <bean id="global-em" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="com.nrift.finch.*.model, com.nrift.finch.*.domain" 
        p:dataSource-ref="GLOBAL"
        p:jpaVendorAdapter-ref="jpaVendorAdapter" />
  <bean id="durga-em" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="com.nri.durga.*.model, com.nri.durga.*.domain" 
        p:dataSource-ref="DURGA"
        p:jpaVendorAdapter-ref="jpaVendorAdapter" /> 
</beans>

datasource.properties

jdbc.components.all=GLOBAL
# Setting for GLOBAL component
jdbc.components.url.GLOBAL=jdbc:mysql://ip:3306/DURGA_DEV
jdbc.components.userName.GLOBAL=usr
jdbc.components.password.GLOBAL=pswd
jdbc.components.minLimit.GLOBAL=1
jdbc.components.maxLimit.GLOBAL=200
jdbc.components.initialLimit.GLOBAL=1
jdbc.components.queryTimeout.GLOBAL=0

# Setting for DURGA component
jdbc.components.url.DURGA=jdbc:mysql://ip:3306/DURGA_DEV
jdbc.components.userName.DURGA=usr
jdbc.components.password.DURGA=pswd
jdbc.components.minLimit.DURGA=1
jdbc.components.maxLimit.DURGA=200
jdbc.components.initialLimit.DURGA=1
jdbc.components.queryTimeout.DURGA=0

# Datasource properties Mysql
jdbc.database.driverClass=com.mysql.jdbc.Driver
jpa.hibernate.dialectClass=org.hibernate.dialect.MySQL5Dialect

【问题讨论】:

  • 您能否包含此数据源的 context.xml sn-p 以及您尝试打开数据库连接的代码
  • @jay 我已经附加了 context.xml 和 datasource 属性。如果我降级 tomcat 7.0.6 和 Spring 3.2,我的应用程序可以正常工作
  • 我将开始消除查看错误所在的可能性。 1)你可以从运行tomcat的服务器连接吗?尝试在 java 应用程序之外手动连接。 2)是否正确读取了属性文件?尝试在 context.xml 中硬编码连接值。 3)tomcat是否尝试连接?检查数据库日志。等等……

标签: mysql spring-jdbc tomcat8


【解决方案1】:

这意味着您的 mysql 驱动程序 jar 不在类路径中。确保它在“$CATALINA_HOME/lib”中

【讨论】:

  • 我已将 mysql 驱动程序放在 $CATALINA_HOME/lib 和上下文 lib 文件夹中,但没有结果。发生了同样的错误。
【解决方案2】:

在部署我的应用程序并从 Eclipse 启动 Tomcat 时,我遇到了类似的问题。 在构建战争并将其手动复制到它正在工作的 webapps 文件夹之后。

我的设置与这个问题中描述的非常相似:

java.sql.SQLException: No suitable driver

Driver 类位于 Tomcat 的 lib 文件夹中,但当从 Eclipse 部署到 wtpwebapps 时,我的 config.xml 似乎没有加载。

【讨论】:

    猜你喜欢
    • 2017-10-17
    • 2016-11-28
    • 2015-10-02
    • 2014-08-05
    • 1970-01-01
    • 2018-12-13
    • 2015-09-22
    • 2019-10-16
    • 2016-10-01
    相关资源
    最近更新 更多