user3174886 user3174886 - 1 year ago 117
Groovy Question

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


def dbURL="jdbc:sqlserver://;databaseName=A"
def dbUsername="sa"
def dbPassword="password"
def dbDriver=""

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

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

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

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




Answer Source

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 -> "${row.description}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download