0

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.

2 Answers 2

0

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

It is available here.

1
  • Jar is present in below folders (1) .m2 repository (2) .groovy/lib (3) grails-app/lib...i can see the jar in my project/properties/java Buildpath as well.. Jun 14, 2014 at 19:27
0

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

5
  • ,thanks a lot, i tried using GORM DSL and i am able to save the data in to the table. Below are the changes i did,(1) As it is an existing table, i used static mapping (GORM DSL)..(2) Used dbCreate = Update in Datasource.groovy (3) Instead of Groovy sql statements, i used below GORM statements 'def stud = new Employee()' 'stud.seqNum = 1 ' 'stud.save(flush:true,failOnError: true)' 'def findstudent = Employee.list()' Jun 23, 2014 at 13:26
  • I am still unable to understand, why groovy sql statments are not working. I will update if i get an answer... Jun 23, 2014 at 13:27
  • You can use direct sql, just create your Sql instance based on datasource. If you need to create sql instance without datasource, load it specifying the classloader like that: def driver = Class.forName(driverClassName, false, this.class.classLoader).newInstance(); def properties = new Properties(); properties.setProperty('user', username); properties.setProperty('password', password); driver.connect(dbUrl, properties); This way you specify classloader which should be used and which have access to jdbc lib Jun 23, 2014 at 20:21
  • Thanks a lot. I tried using dataSource instead of using sql.newInstance. It is working. now i am able to display records using groovy sql. i would like to share what i observed: 'def dataSource' should NOT be inside a method instead it should be at class level. Then only, it seems, groovy will inject. and ofcourse, datasource should have correct properties. Thanks again. Jun 24, 2014 at 9:21
  • @user3582387 Hi, I am also facing the same issue. Can you share the corrected code here.
    – Reshma
    Dec 13, 2017 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.