无法将grails应用程序连接到mySql数据库,无法加载JDBC驱动程序类'com.mysql.jdbc.Driver'

时间:2012-07-31 06:07:31

标签: mysql grails

我已经将mysql / j连接器复制到我的grails应用程序的grails-app / lib文件夹中。我的DataSource.groovy文件看起来像这样

dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = "password"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update',     'validate', ''
            url = "jdbc:mysql://localhost:3306/tewhareoteata3test"
            dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            pooled = true
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=true
               validationQuery="SELECT 1"
            }
        }
    }
}

但它给了我这个错误

Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

3 个答案:

答案 0 :(得分:12)

BuildConfig.groovy添加

dependencies {
        runtime 'mysql:mysql-connector-java:5.1.16'
    }

事实上它可能已经在那里被注释掉了。

这告诉grails下载mysql-connector及其依赖项。

你需要告诉Grails使用哪个maven存储库(也在BuildConfig.groovy中):

repositories {
        grailsPlugins()
        grailsHome()
        grailsCentral()
        mavenCentral()
    }

答案 1 :(得分:5)

取消注释

runtime 'mysql:mysql-connector-java:5.1.20'
BuildConfig.groovy中的

答案 2 :(得分:3)

如果要在不使用自动依赖项解析的情况下包含jar文件,则需要将其放在项目根目录中的lib目录中, not grails-app/lib。布局应如下所示:

|-- grails-app
|   |-- conf
|   |   |-- hibernate
|   |   `-- spring
|   |-- controllers
|   |-- domain
|   |-- i18n
|   |-- services
|   |-- taglib
|   |-- utils
|   `-- views
|       `-- layouts
|-- lib                  <-- jars go here
|-- scripts
|-- src
|   |-- groovy
|   `-- java
|-- target
|-- test
|   |-- integration
|   `-- unit
`-- web-app