dynamic sql

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

dynamic sql

nozomi
I want to write a dynamic sql insert statement:
so I define a statement how looks like this:

def sql = Sql.newInstance("jdbc:postgresql://localhost:5432/groovy", "user","pass", "org.postgresql.Driver")

String[] fields = ["data1","data2","data3"]
def dataDB = "field1,field2,field3"
def valuess = "?,?,?"
def statement= "insert into data ("+dataDB+") values ("+valuess+")"
sql.execute(statement,fields)

When I run the script I become folloing error message:
Exception thrown: groovy.lang.MissingMethodException: No signature of method: groovy.sql.Sql.execute() is applicable for argument types: (java.lang.String, [Ljava.lang.String;) values: {"insert into data (field1,field2,field3) values (?,?,?)", ["data1", "data2", "data3"]}

what made I wrong?

rgds nozomi
Reply | Threaded
Open this post in threaded view
|

Re: dynamic sql

tugwilson

On 20 Dec 2006, at 10:01, nozomi wrote:

>
> I want to write a dynamic sql insert statement:
> so I define a statement how looks like this:
>
> def sql = Sql.newInstance("jdbc:postgresql://localhost:5432/groovy",
> "user","pass", "org.postgresql.Driver")
>
> String[] fields = ["data1","data2","data3"]
> def dataDB = "field1,field2,field3"
> def valuess = "?,?,?"
> def statement= "insert into data ("+dataDB+") values ("+valuess+")"
> sql.execute(statement,fields)
>
> When I run the script I become folloing error message:
> Exception thrown: groovy.lang.MissingMethodException: No signature of
> method: groovy.sql.Sql.execute() is applicable for argument types:
> (java.lang.String, [Ljava.lang.String;) values: {"insert into data
> (field1,field2,field3) values (?,?,?)", ["data1", "data2", "data3"]}
>
> what made I wrong?

Execite takes a Sting and a List - you gave it a String and an array  
of Strings

change the declaration of fields from String[] to def

Please post any further problems on the usr list the jsr list is for  
discussions on language standardisation.

John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email