compile groovy class using groovyc : multiplecompliationErrorException
the following is a simple class I want to compile using groovyc. it always gives me "multiplecompliationErrorException".
could some one kindly help me out?
thanks.
import groovy.sql.Sql
class TestDb{
def sql = Sql.newInstance("jdbc:postgresql://localhost:5432/mydb",
"user", "password", "org.postgresql.Driver")
// delete table if previously created
try { sql.execute("drop table if exists PERSON")
} catch(Ex开发者_C百科ception e){}
// create table sql.execute('''create table PERSON (
id integer not null primary key,
firstname varchar(20),
lastname varchar(20),
location_id integer,
location_name varchar(30) )''')
}
- you defined a class, and then put code in class body but not in a method. See this: http://groovy.codehaus.org/Scripts+and+Classes
- you commented out the sql.execute line, which should probably be there -- without it the code is not valid.
- You are swallowing an exception, i.e. doing nothing in the catch block. at least log the exception so you can get more info (this is not your compile problem, but you should fix it anyway)
@hvgotcodes is right...here is a corrected class file:
import groovy.sql.Sql
class TestDb{
def sql = Sql.newInstance("jdbc:postgresql://localhost:5432/mydb", "user", "password", "org.postgresql.Driver")
static void main( args ) {
// delete table if previously created
sql.execute("drop table if exists PERSON")
sql.execute( '''create table PERSON (
id integer not null primary key,
firstname varchar(20),
lastname varchar(20),
location_id integer,
location_name varchar(30) )''' )
}
}
精彩评论