Reputation:
I am trying to get a toString()
method working so I can list everything from the database. But I can't seem to find what I'm doing wrong. I am using Groovy in a script console, which does not let me use println
. All I can do is collect all output and return as a string
return getUserId()
@ToString
class Logs{
String created = ""
String summary =""
String category =""
String searchField =""
}
def getUserId(){
def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver
def props = new Properties()
props.setProperty("user", "USERNAME")
props.setProperty("password", "PASSWORD")
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory")
props.setProperty("ssl", "true")
def conn = driver.connect("jdbc:postgresql://Database:port/GRP", props)
def sql = new Sql(conn)
try {
def logs = new Logs(created: 'created', summary: 'summary', category: 'category', searchField: 'searchField')
String query = "SELECT * from audit_log"
PreparedStatement statement = conn.prepareStatement(query)
ResultSet result = statement.executeQuery()
while(result.next()){
String created1 = result.getString("created")
logs.created = created1
String summary1 = result.getString("summary")
logs.summary = summary1
String category1 = result.getString("category")
logs.category = catergory1
String searchField1 = result.getString("search_field")
logs.searchField = searchField1
}
return (logs.toString())
} finally {
sql.close()
conn.close()
}
}
groovy.lang.MissingPropertyException: No such property: created for class: Script321 at Script321.getUserId(Script321.groovy:100) at Script321.run(Script321.groovy:76)
Line 100 is the: def logs = new Logs((created)+"-" +(summary)+", " +(category)+", " +(searchField))
Line 76 is the: return getUserID()
Upvotes: 0
Views: 361
Reputation: 9895
The error is occurring because you're trying to define the class Logs
within the method getUserId()
. You can fix it by defining the class outside of the method.
@ToString
class Logs {
String created = ""
String summary =""
String category =""
String searchField =""
}
def getUserId(){
def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver
def props = new Properties()
props.setProperty("user", "USERNAME")
props.setProperty("password", "PASSWORD")
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory")
props.setProperty("ssl", "true")
def conn = driver.connect("jdbc:postgresql://SERVER:PORT/DATABASE", props)
def sql = new Sql(conn)
try {
def logs = new Logs("[GRP2.0] " +(created)+"-" +(summary)+", " +(category)+", " +(searchField))
String query = "SELECT * from audit_log"
PreparedStatement statement = conn.prepareStatement(query)
ResultSet result = statement.executeQuery()
while(result.next()){
String created1 = result.getString("created")
logs.created = created1
String summary1 = result.getString("summary")
logs.summary = summary1
String category1 = result.getString("category")
logs.category = catergory1
String searchField1 = result.getString("search_field")
logs.searchField = searchField1
}
return (logs.toString())
} finally {
sql.close()
conn.close()
}
}
Upvotes: 0