简体   繁体   中英

No suitable driver found for jdbc:mysql://localhost

I am new to Groovy & Grails, facing this issue. I have been trying to connect mysql to GGTS facing below issue.

GGTS 3.5.1 Grails 2.3.7 Mysql 5.6.19

  1. I added dependencies in Buildconfig.groovy, had jar in .m2 repository 2.Tried adding jar in grails-app/lib 3.Tried to add @grab, but faced error, "no system classloader' 4.I am able to use the same url in java/eclipse and able to connect.

Error description displayed in console

|Environment set to development
.................................
|Packaging Grails application
........................................
|Running Grails application
|Server running. Browse to http://localhost:8080/Hello
....Error 
|
2014-06-15 00:28:44,903 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver  - SQLException occurred when processing request: [GET] /Hello/book/index
No suitable driver found for jdbc:mysql://localhost/ot. Stacktrace follows:
Message: No suitable driver found for jdbc:mysql://localhost/ot
    Line | Method
->>  596 | getConnection in java.sql.DriverManager
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    215 | getConnection in     ''
|     12 | index . . . . in hello.BookController
|    200 | doFilter      in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter . .  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker     in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run           in java.lang.Thread

my datasource.groovy

dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'

}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
//    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}

// environment specific settings
environments {
    development {
        dataSource {
            username = "root"
            password = "lenovo"
            dbCreate = "update"
            url = "jdbc:mysql://localhost/ot"

        }
    }

Buildconfig.groovy

dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime',     'test' or 'provided' scopes e.g.
            runtime 'mysql:mysql-connector-java:5.1.30'
          // runtime 'org.postgresql:postgresql:9.3-1100-jdbc41'
         }

controller

def index() {

conn = Sql.newInstance("jdbc:mysql://localhost/ot",
                      "root",
                      "lenovo",
                      "com.mysql.jdbc.Driver")
                    //  "org.gjt.mm.mysql.Driver")

conn.rows('select NAME, ROLLNO from student').each{ 
    println "${it.NAME} ${it.ROLLNO}"

Please suggest. Thanks for your support in advance.

The error is explicitly telling you that you need to add the mysql JDBC connector JAR to your classpath.

It is available here .

Probably different classloader loads jars and different is used in controller. As proposed here: Grails sql queries create you Sql instance from injected dataSource instead of Sql.newInstance()

Still you should consider defining a Domain class and access the database with GORM.

hope this helps, droggo

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