Skip navigation.

BlogtimeException

By Behrang Saeedzadeh (the 5th incarnation)

Hmmm... Groovy is juicy!

,

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 :wink:

New photos uploadedDamn! Gen Pets!?

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies