java.sql.SQLException:访问数据库时没有合适的驱动程序

时间:2012-07-01 17:50:06

标签: spring hibernate connection-pooling tomcat7 c3p0

我已经在Tomcat 7上创建了一个基本的Spring 3.1 - Hibernate 4 - Mysql 5.5 Web应用程序。 我通过spring + C3PO汇集连接。

更新:我也发布了我的代码。

My Spring应用程序上下文:

<!-- Context -->
<context:component-scan base-package="com.yl.tomcat" />

<!-- AOP -->
<aop:aspectj-autoproxy />

<!-- Properties -->
<bean id="applicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:applicationProperties.properties" />
</bean>

<!-- Data source -->
<!-- Have spring manage connection management along with connection pooling -->
<bean id="dataSourceGlobal" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${driverClass}" />
    <property name="jdbcUrl" value="${jdbcUrl" />
    <property name="user" value="${user}" />
    <property name="password" value="${password}" />

    <!-- C3P0 Connection pool properties -->
    <property name="minPoolSize" value="${c3p0.min_pool_size}" />
    <property name="maxPoolSize" value="${c3p0.max_pool_size}" />
    <property name="unreturnedConnectionTimeout" value="${c3p0.timeout}" />
    <property name="idleConnectionTestPeriod" value="${c3p0.idle_test_period}"/>
    <property name="maxStatements" value="${c3p0.max_statements}"/>
    <property name="automaticTestTable" value="${c3p0.automatic_test_table}"/>
</bean>

<!-- JPA -->
<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="myPU" />
    <property name="dataSource" ref="dataSourceGlobal" />
</bean>

<!-- In order to enable EntityManager injection -->
<bean id="persistenceAnnotation"
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    <property name="persistenceUnits">
        <map>
            <entry key="myPU" value="persistence/myPU" />
        </map>
    </property>
</bean>

<!-- Transactions -->
<tx:annotation-driven />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSourceGlobal" />
</bean>

正如您所看到的,我使用了一个C3PO数据源进行池化,由spring容器管理。

我的属性文件(从spring容器引用):

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/yl
user=root
password=pass

c3p0.min_pool_size=5
c3p0.max_pool_size=20
c3p0.timeout=5000
c3p0.idle_test_period=100
c3p0.max_statements=50
c3p0.automatic_test_table=test_table

以下是我的POM依赖项:

    <properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
    <hibernate.version>4.1.4.Final</hibernate.version>
    <org.aspectj-version>1.6.9</org.aspectj-version>
    <org.slf4j-version>1.5.10</org.slf4j-version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!--Transaction Management Abstraction (depends on spring-core, spring-beans, 
        spring-aop, spring-context) Define this if you use Spring Transactions or 
        DAO Exception Hierarchy(org.springframework.transaction.*/org.springframework.dao.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!--JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
        spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!--Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and 
        iBatis.(depends on spring-core, spring-beans, spring-context, spring-tx) 
        Define this if you need ORM (org.springframework.orm.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- DB Connection -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.20</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Misc -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
</dependencies>

我的web.xml是一个标准的Spring MVC web.xml,增加了我的持久性单元定义:

<persistence-unit-ref>
    <persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
    <persistence-unit-name>myPU</persistence-unit-name>
</persistence-unit-ref>

我的DAO注入了实体经理:

@PersistenceContext
protected EntityManager entityManager;

最后,这是我的persistence.xml,它位于我的META-INF文件夹下:

<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.transaction.flush_before_completion"
            value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

尝试访问数据库时我得到java.sql.SQLException:没有合适的驱动程序:

  

java.sql.SQLException:没有合适的驱动程序       java.sql.DriverManager.getDriver(DriverManager.java:289)       com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)       com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119)       com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)       com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132)       com.mchange.v2.c3p0.impl.C3P0PooledConnectionPoolManager.initializeAutomaticTestTable(C3P0PooledConnectionPoolManager.java:772)       com.mchange.v2.c3p0.impl.C3P0PooledConnectionPoolManager.createPooledConnectionPool(C3P0PooledConnectionPoolManager.java:696)       com.mchange.v2.c3p0.impl.C3P0PooledConnectionPoolManager.getPool(C3P0PooledConnectionPoolManager.java:257)       com.mchange.v2.c3p0.impl.C3P0PooledConnectionPoolManager.getPool(C3P0PooledConnectionPoolManager.java:271)       com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)       org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)       org.hibernate.internal.AbstractSessionImpl $ NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:281)       org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)       org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)       org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.extractPhysicalConnection(ConnectionProxyHandler.java:82)       org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)       org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)       $ Proxy45.prepareStatement(未知来源)       org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ 5.doPrepare(StatementPreparerImpl.java:147)       org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166)       org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:145)       org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1711)       org.hibernate.loader.Loader.doQuery(Loader.java:828)       org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)       org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)       org.hibernate.loader.Loader.loadEntity(Loader.java:2033)       org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)       org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)       org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3719)       org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:449)       org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:418)       org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)       org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251)       org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)       org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079)       org.hibernate.internal.SessionImpl.access $ 2200(SessionImpl.java:172)       org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl.load(SessionImpl.java:2425)       org.hibernate.internal.SessionImpl.get(SessionImpl.java:975)       org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:807)       org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:781)       sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       java.lang.reflect.Method.invoke(Method.java:601)       org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)       $ Proxy44.find(未知来源)       sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       java.lang.reflect.Method.invoke(Method.java:601)       org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)       $ Proxy21.find(未知来源)       com.yl.tomcat.dal.impl.BasicJpaDao.read(BasicJpaDao.java:48)       com.yl.tomcat.app.bl.UserService.updateUserName(UserService.java:17)       com.yl.tomcat.web.HomeController.String(HomeController.java:53)       sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       java.lang.reflect.Method.invoke(Method.java:601)       org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)       org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)       org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)       org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)       org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)       org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)       org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)       org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)       org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)       org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)       javax.servlet.http.HttpServlet.service(HttpServlet.java:641)       javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

在类似的线程(thisthis)中,通过将mysql驱动程序jar放在服务器lib文件夹中来提供解决方案。 ($ CATALINA_HOME / lib中)。 目前我只在我的应用程序类路径中有jar,我想知道上面的解决方案是否是唯一可用的(我不喜欢它,它将我的应用程序耦合到我的tomcat服务器,这不是最佳的我的意见)?

无论如何 - 即使我将mysql jar复制到tomcat的lib库它也没有改变任何东西:( 我一路上都想念一下吗?

非常感谢任何帮助, 瑜珈

1 个答案:

答案 0 :(得分:9)

好的,我明白了。

如下所述: http://doc.51windows.net/mysql/?url=/mysql/ch23s03.html SQLException'No Suitable Driver'表示驱动程序不在您的CLASSPATH中,或者您的URL格式不正确。

显然我在那里错过了一个括号:

<property name="jdbcUrl" value="${jdbcUrl" />

我已经通过了许多解决方案提案,例如:

  • 将mysql jar复制到tomcat。这不是必需的,因为它在我的 classpath已经。
  • 安装不同的mysql连接器(5.1.2-bin而不是5.1.2) 这也不是必需的。
  • 降级Hibernate版本从4.1.4升级到4.1.1 - 不再被认为是 好。

所以我的网址格式不正确。 解决这个问题后,我遇到了一些其他问题。

例如,在某些时候我决定将persistence.xml位置添加到我的entitymanager定义中:

<property name="persistenceXmlLocation" value="META-INF/persistence.xml"/>

但后来得到了:

  

ContainerEntityManagerFactoryBean在war文件中找不到persistence.xml

问题是我必须在路径之前添加类路径(因为META-INF在标准maven项目格式的类路径下是自然的):

<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>

由于某种原因,我得到了:

  

java.lang.ClassNotFoundException:org.aspectj.util.PartialOrder $ PartialComparable

将aspectjweaver依赖项添加到我的pom.xml解决了这个问题:

  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.6</version>
  </dependency>

修好后我得到了:

  

错误:org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 用户'testUser'@'localhost'拒绝访问(使用密码:YES)

我甚至尝试重新安装mysql但到目前为止没有成功。我猜我会尝试为这个打开一个不同的主题。