【问题标题】:No suitable driver found for jdbc:mysql://localhost找不到适合 jdbc:mysql://localhost 的驱动程序
【发布时间】:2014-08-05 01:57:40
【问题描述】:

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

GGTS 3.5.1 圣杯 2.3.7 mysql 5.6.19

  1. 我在 Buildconfig.groovy 中添加了依赖项,在 .m2 存储库中有 jar 2.尝试在grails-app/lib中添加jar 3.尝试添加@grab,但遇到错误,“没有系统类加载器” 4.我可以在java/eclipse中使用相同的url并且可以连接。

控制台中显示的错误描述

|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

我的数据源.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}"

请提出建议。提前感谢您的支持。

【问题讨论】:

    标签: mysql grails jdbc


    【解决方案1】:

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

    它是可用的here

    【讨论】:

    • Jar 存在于以下文件夹 (1) .m2 存储库 (2) .groovy/lib (3) grails-app/lib...我可以在我的项目/properties/java 中看到 jar构建路径也是如此..
    【解决方案2】:

    可能不同的类加载器加载 jars 并且在控制器中使用不同的。正如这里建议的那样:Grails sql queries 从注入的数据源而不是 Sql.newInstance() 中创建 Sql 实例

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

    希望这会有所帮助, 卓戈

    【讨论】:

    • ,非常感谢,我尝试使用 GORM DSL,我能够将数据保存到表中。以下是我所做的更改,(1) 因为它是一个现有表,所以我使用静态映射 (GORM DSL)..(2) 在 Datasource.groovy 中使用 dbCreate = Update (3) 而不是 Groovy sql 语句,我在下面使用GORM 语句 'def stud = new Employee()' 'stud.seqNum = 1' 'stud.save(flush:true,failOnError: true)' 'def findstudent = Employee.list()'
    • 我仍然无法理解,为什么 groovy sql statments 不起作用。如果我得到答案,我会更新......
    • 您可以直接使用sql,只需根据数据源创建您的Sql实例即可。如果您需要创建没有数据源的 sql 实例,请像这样指定类加载器来加载它: def driver = Class.forName(driverClassName, false, this.class.classLoader).newInstance(); def 属性 = 新属性(); properties.setProperty('user', 用户名); properties.setProperty('密码', 密码); driver.connect(dbUrl, 属性);通过这种方式,您可以指定应该使用哪些类加载器以及哪些可以访问 jdbc lib
    • 非常感谢。我尝试使用 dataSource 而不是使用 sql.newInstance。这是工作。现在我可以使用 groovy sql 显示记录了。我想分享我观察到的内容:“def dataSource”不应该在方法内部,而应该在类级别。只有这样,groovy 才会注入。当然,数据源应该具有正确的属性。再次感谢。
    • @user3582387 嗨,我也面临同样的问题。你能在这里分享更正的代码吗?
    猜你喜欢
    • 2018-12-13
    • 2019-10-16
    • 2016-10-01
    • 2012-05-08
    • 2017-08-26
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多