没有为jdbc找到合适的驱动程序:mysql:// localhost

时间:2014-06-14 19:19:33

标签: mysql grails jdbc

我是Groovy&的新手Grails,面临这个问题。我一直在尝试将mysql连接到GGTS面临以下问题。

GGTS 3.5.1 Grails 2.3.7 Mysql 5.6.19

  1. 我在Buildconfig.groovy中添加了依赖项,在.m2存储库中有jar 2.Tried在grails-app / lib中添加jar 3.Tried添加@grab,但面临错误,"没有系统类加载器' 4.我能够在java / eclipse中使用相同的url并且能够连接。
  2. 控制台中显示的错误说明

    |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
    

    我的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'
             }
    

    控制器

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

    请建议。感谢您的支持。

2 个答案:

答案 0 :(得分:0)

该错误明确告诉您需要将mysql JDBC连接器JAR添加到类路径中。

可以使用here

答案 1 :(得分:0)

可能不同的类加载器加载jar并且在控制器中使用不同的类。正如这里提出的:Grails sql queries从注入的dataSource而不是Sql.newInstance()创建Sql实例

你仍然应该考虑定义一个Domain类并使用GORM访问数据库。

希望这有帮助, droggo