Groovy / Postgres "No method signature: java.lang.String.positive ()"

Trying to write some basic PostgreSQL code using JDBC to integrate into an application written in Groovy. I wrote this Groovy code to connect to a database and then execute statements; but, I get an error that I tried to find a solution, and could not. The following is the relevant piece of Groovy code, as well as a comment about where the error occurs:

def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver) println "Sql Instance: " + sql sql.execute( " DROP TABLE IF EXISTS test;" + "CREATE TABLE test (" + "id SERIAL," + "word TEXT," + "number INTEGER," + "decimal NUMERIC," + "datetime TIMESTAMP" + ");" ) def params = ['Hello, World!', 42, 3.14159, null] sql.execute("INSERT INTO test (word, number, decimal, datetime)" + "VALUES (?,?,?,?);", params) sql.eachRow("SELECT * FROM test;") { row -> println "The row Id is: ${row.id}" // HERE?? + "The word is: ${row.word}" + "The number is: ${row.number}" + "The decimal is: ${row.decimal}" + "The date-time is: ${row.datetime}" } sql.close() 

The console log says:

  Sql Instance: groovy.sql.Sql@5aa9e4eb
     The row Id is: 1
     Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.positive () is applicable for argument types: () values: []
     Possible solutions: notify (), tokenize (), size (), size ()
     groovy.lang.MissingMethodException: No signature of method: java.lang.String.positive () is applicable for argument types: () values: []
     Possible solutions: notify (), tokenize (), size (), size ()
         at DatabaseTest $ _run_closure1.doCall (DatabaseTest.groovy: 34)
         at DatabaseTest.run (DatabaseTest.groovy: 31)
         at com.intellij.rt.execution.application.AppMain.main (AppMain.java:140)

     Process finished with exit code 1 

Any idea what I'm doing wrong?

+5
source share
2 answers

don't use scary string concatenation! Groovy has a nice replacement:

 println """The row Id is: ${row.id} The word is: ${row.word} The number is: ${row.number} The decimal is: ${row.decimal} The date-time is: ${row.datetime}""" 
+4
source

Another answer gave you a solution that I would also recommend, but it may be helpful to know the reason why this is happening.

Groovy uses excessive operator overload. This means that if you have to write your own class, you can overload the + operator to do a lot of things.

However, there is a difference between using + at the end of the line and at the beginning of the line.

At the end of the line, + considered as an addition, but before the line it is considered as positive (consider that "+ 6" is considered as "positive six").

If you have to write this, this will work better:

 println "The row Id is: ${row.id}" + "The word is: ${row.word}" + "The number is: ${row.number}" + "The decimal is: ${row.decimal}" + "The date-time is: ${row.datetime}" 

If you do this, you will get the output in one line, because you did not add new string characters, \n

 println "The row Id is: ${row.id}\n" + "The word is: ${row.word}\n" + "The number is: ${row.number}\n" + "The decimal is: ${row.decimal}\n" + "The date-time is: ${row.datetime}" 

And now everything starts to look ugly, so Groovy's multi-line String functions might come in handy, as shown in another answer.

+8
source

All Articles