Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't connect grails application to a mySql database, Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

Tags:

mysql

grails

I already copied the mysql/j connector to the grails-app/lib folder of my grails application. and my DataSource.groovy file looks like this

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"
            }
        }
    }
}

but it gives me this error

Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
like image 407
chip Avatar asked Jul 31 '12 06:07

chip


People also ask

Could not load driver com mysql jdbc driver?

Driver occurs if there is a disparity between mysql driver class, mysql driver jar version and mysql database version. The java application attempts to load a driver class from the mysql database driver jar. If the driver class is unable to load, the exception Cannot load driver class: com.

Is JDBC deprecated?

jdbc. Driver'. This is deprecated. The new driver class is `com.

Which is now deprecated and will be removed in a future release of connector J?

Deprecation and Removal Notes The TLSv1 and TLSv1. 1 connection protocols are now deprecated.


3 Answers

In BuildConfig.groovy add

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

In fact it may be there already just commented out.

This tells grails to download mysql-connector and its dependencies.

You'll need to tell Grails which maven repositories to use (also in BuildConfig.groovy ) :

repositories {
        grailsPlugins()
        grailsHome()
        grailsCentral()
        mavenCentral()
    }
like image 83
David Genn Avatar answered Sep 22 '22 12:09

David Genn


Uncomment

runtime 'mysql:mysql-connector-java:5.1.20'

in BuildConfig.groovy.

like image 24
Victor Sergienko Avatar answered Sep 18 '22 12:09

Victor Sergienko


If you want to include jar files without using automatic dependency resolution, you need to put it in the lib directory in the project root, not grails-app/lib. The layout should look like this:

|-- 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
like image 44
ataylor Avatar answered Sep 18 '22 12:09

ataylor