user3174886
user3174886

Reputation: 301

How to use MSSQL Select in Groovy

I'm trying to access the table from a database. First I checked the connection of the database and it worked. Then I checked the close db and it worked. After I try to select couple of items from the table and print them using eachraw command.

I'm getting below error:

Wed Oct 05 07:26:01 EDT 2016:INFO:No signature of method: groovy.sql.Sql.eachrow() is applicable for argument types: (java.lang.String, Script26$_run_closure1) values: [SELECT Type,Description FROM A.dbo.Type, ...]
Possible solutions: eachRow(java.lang.String, groovy.lang.Closure), eachRow(groovy.lang.GString, groovy.lang.Closure), eachRow(java.lang.String, groovy.lang.Closure, groovy.lang.Closure), eachRow(java.lang.String, java.util.List, groovy.lang.Closure), eachRow(java.lang.String, java.util.Map, groovy.lang.Closure), eachRow(java.util.Map, java.lang.String, groovy.lang.Closure)

This is my code:

    import groovy.sql.Sql


    try{

    def dbURL="jdbc:sqlserver://1.1.2.1:1433;databaseName=A"
    def dbUsername="sa"
    def dbPassword="password"
    def dbDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"

    def db = Sql.newInstance(dbURL,dbUsername,dbPassword,dbDriver)

    def q1 = "SELECT Type,Description FROM A.dbo.Type"


    db.eachrow(q1){row ->
       log.info "${it.toString().Description}"
    }



   }catch (Exception e){
       log.info "Some db error"
       log.info e.getMessage()

   }finally{

   db.close()

   }

Upvotes: 3

Views: 6013

Answers (1)

quindimildev
quindimildev

Reputation: 1280

The method is eachRow, no eachrow, and if you specify a row variable name inside the closure, you have to use 'row' instead of 'it'. So you code should look like this:

db.eachRow(q1){row ->
       log.info "${row.description}"
}

Upvotes: 5

Related Questions