Feeds

Groovy way to MySQL

Get your Groove on

The essential guide to IT transformation

A very traditional niche of scripting languages, (such as Perl, Python or Ruby), has been in the ad hoc manipulation of databases – from grabbing data, transforming it, performing bulk updates, right on through to full-on data migration projects that move data from one platform or RDBMS to another. Scripting languages are perfect for these types of task because they are quick to write and test, can be used interactively while the problem space is being explored, and because extraneous features – such as graphical user interface, detailed user documentation and so on – are not required.

For hardcore Java hackers, much of this kind of ad hoc activity has either entailed writing utilities libraries or, more likely, dropping down into another language – from Unix shell scripts to the aforementioned Perl, Python, Ruby et al. Now, however, Groovy exists to provide all of the benefits of a dynamically-typed scripting language that is designed to be easily accessible to Java developers and which sits on top of the Java Virtual Machine (JVM) and, as a big bonus, can make use of existing Java APIs and packages.

In the case of database access, Groovy can be used both with the standard Java JDBC mechanism (which makes use of SQL) or with its own high-level SQL-less DataSet libraries. In this two-part hands-on tutorial we'll look at how you can use Groovy to quickly and easily interface with a MySQL database sitting on a remote server somewhere.

First things first, though, and that's to install Groovy and to check that it all runs smoothly. Groovy can be downloaded here - there are install packages for various platforms, including Windows, Linux, and a platform-independent zip.

Aside from dropping the files into the right place the only other things to do are set the GROOVY_HOME environment variable (and JAVA_HOME if it doesn't already exist), and to make sure that the \groovy\bin directory is on your path.

A quick way to test is simply to open a command shell or terminal and type:

groovy -v

This should come back with the versions of Groovy and Java that you have installed.

The only other thing you need to have in place is a JDBC driver for MySQL. The jar file needs to be on the classpath; simply copying the file to the \groovy\lib is enough to banish any problems.

For the purposes of this article we'll be looking at a simple database (called pers) which contains a single table, called users, which is represented by the following grid:

user_name email user_id
'tom' 'tom@here.com' 1
'dick' 'dick@there.co.uk' 2
'harry' 'harry@harry.com' 3
'george' 'hello@hello.org' 4

Secure remote control for conventional and virtual desktops

More from The Register

next story
BBC: We're going to slip CODING into kids' TV
Pureed-carrot-in-ice cream C++ surprise
China: You, Microsoft. Office-Windows 'compatibility'. You have 20 days to explain
Told to cough up more details as antitrust probe goes deeper
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Endpoint data privacy in the cloud is easier than you think
Innovations in encryption and storage resolve issues of data privacy and key requirements for companies to look for in a solution.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Advanced data protection for your virtualized environments
Find a natural fit for optimizing protection for the often resource-constrained data protection process found in virtual environments.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.