提问者:小点点

从tomcat 8.0中找不到适合JDBC:MySQL的驱动程序


我正在尝试Tomcat8.0中的应用程序,但我得到了错误

org.springframework.JDBC.CanNotGetJdbcConnectionException:无法获取JDBC连接;嵌套异常为java.sql.sqlexception:未找到适用于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. spring 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

共2个答案

匿名用户

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

匿名用户

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

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

java.sql.sqlexception:没有合适的驱动程序

驱动程序类在Tomcat的lib文件夹中,但从Eclipse部署到wtpwebapps中时,似乎没有加载我的config.xml。