Code in the controller
def sql = groovy.sql.Sql.newInstance ( dbUrl, dbUser, dbPassword, dbDriver )
String[] allUser = sql.rows("select user_id from users")
allUser.each()
{
print "${it}"
}
The output in the console
{user_id=9}
{user_id=10}
{user_id=11}
{user_id=12}
{user_id=13}
{user_id=14}
How do I print it in such a way that it will display just the value? Example:
9
10
11
12
13
14
This makes it a bit more intricate than it needs to be.
Sql.rows()will return a list ofGroovyRowResultobjects, which make it easy to retrieve the fields; you have an implicit cast toString[], based on your variable declaration. You can change it as follows:or, if you prefer,
Then you can print the userid’s like so: