Hmmm... Groovy is juicy!
Monday, 15. January 2007, 14:26:07
If scripting languages were fruits, Groovy would be the juiciest!
import groovy.sql.Sql
def mysql = Sql.newInstance('jdbc:mysql:///mydb', 'myuser', 'mypass')
mysql.execute 'delete from language'
def file = new File('Languages.txt')
file.eachLine{ line ->
def comps = line.split(/,/);
comps = comps.collect{ comp -> comp.trim() }
mysql.execute "insert into language(abbreviation, name) values(${comps[0]}, ${comps[1]})"}
This small piece of code, reads the Language.txt line-by-line:
...
en-za, English (South Africa)
en-tt, English (Trinidad)
en-GB, English (UK)
en-US, English (US)
et, Estonian
fi, Finnish
...
Splits, each line into two tokens, namely comps[0] and comps[1], trims each token, and then inserts every two token as a row in the language table. Mmm... Juicy!
language(id bigint auto increment, abbreviation varchar(5), name varchar(100))
Yes, I know... almost every database system allows us to load such data directly with some built-in function, but how can we call that juicy ![]()









How to use Quote function: