简体   繁体   中英

Spring + Hibernate + Maven: jCannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/test'

I have my mysql-connector jar in:

1.) Tomcat Server lib

2.) Build Path as an External JAR. * Removed from Maven dependency, although it remains with the same error.

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql//localhost:3306/test
jdbc.username=root
jdbc.password=****

web.xml adding it with a snippet:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

And my complete servlet-context.xml

<context:annotation-config />
<context:component-scan base-package="org.springsecudev.controller" />
<context:component-scan base-package="org.springsecudev.model" />
<context:component-scan base-package="org.springsecudev.service" />
<context:component-scan base-package="org.springsecudev.dao" />

<beans:bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties">
</beans:bean>

<beans:bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}">
</beans:bean>

<beans:bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="configLocation">
        <beans:value>classpath:hibernate.cfg.xml</beans:value>
    </beans:property>
    <beans:property name="configurationClass">
        <beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">${jdbc.dialect}</beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>   

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"></beans:property>
</beans:bean>

<interceptors>
    <beans:bean id="webContentInterceptor" 
        class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <beans:property name="cacheSeconds" value="0"/>
        <beans:property name="useExpiresHeader" value="true"/>
        <beans:property name="useCacheControlHeader" value="true"/>
        <beans:property name="useCacheControlNoStore" value="true"/>
    </beans:bean>
</interceptors>

<beans:bean id="encoder"  
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <beans:constructor-arg value="12"></beans:constructor-arg>
</beans:bean>

I also added the context.xml in META-INF to be sure I tried this one as well with

<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource  name="jdbc/test" 
        username="root" 
        password="root" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/test"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        initialSize="1"/>

But still no luck, it also has the same error being:

INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@39c45c72: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,propertyConfigurer,dataSource,sessionFactory,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view. InternalResourceViewResolver#0,transactionManager,org.springframework.web.servlet.handler.MappedInterceptor#0,encoder,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@31e0a986 WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null ERROR: org.hibernate.util.JDBCExceptionReporter - Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/test' WARN : org.hibernate.cfg.SettingsFactory - Could not obtain connection metadata java.sql.SQLException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/test' at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)....... [ shortened ] Caused by: java.sql.SQLException: No suitable driver at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151) ... 40 more

I've been trying to find out what could be the problem wherein I have copied the jar in build path and server lib.

seems like you're missing the colon in file jdbc.properties for the database URL.

jdbc:mysql//localhost:3306/test

I think it should be

jdbc:mysql://localhost:3306/test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM