简体   繁体   中英

Using Spring and Hibernate — Unknown database

I have an application in which I'm using Spring and Hibernate. I'm getting the following error message, even though the database in question exists.

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database 'cpc"'

This is the relevant portion of jcbulboard-servlet.xml

<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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
        p:username="${jdbc.username}" p:password="${jdbc.password}">
    </bean>
</beans>

This is jdbc.properties, which is in the correct place.

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost/cpc"
jdbc.username=root
jdbc.password=password

Here is the relevant portion of hibernate.cfg.xml.

    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost/cpc</property>
<!-- There's more in this file, but this is all that's relevant -->
        </session-factory>
    </hibernate-configuration>

This is not a Spring or Hibernate issue, but instead is a driver issue. Your config is correct, but make sure that your connection url is correct. There are two things that jump out at me:

  1. make sure that the port is correct (you have no port specified)
  2. make sure that you actually created the database cpc (CREATE DATABASE cpc)'
  3. make sure you have permissions to actually see the database (various GRANT < blah > on cpc.* to < user >)

A trailing double quotation in jdbc.databaseurl may be cause of your problem. Try removing it.

jdbc.databaseurl=jdbc:mysql://localhost/cpc

instead of

jdbc.databaseurl=jdbc:mysql://localhost/cpc"

Hope this helps.

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