Feeds

Groovy way to MySQL

Get your Groove on

Secure remote control for conventional and virtual desktops

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

The essential guide to IT transformation

More from The Register

next story
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
Intel's Raspberry Pi rival Galileo can now run Windows
Behold the Internet of Things. Wintel Things
Linux Foundation says many Linux admins and engineers are certifiable
Floats exam program to help IT employers lock up talent
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
Linux kernel devs made to finger their dongles before contributing code
Two-factor auth enabled for Kernel.org repositories
prev story

Whitepapers

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.
Top 10 endpoint backup mistakes
Avoid the ten endpoint backup mistakes to ensure that your critical corporate data is protected and end user productivity is improved.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Rethinking backup and recovery in the modern data center
Combining intelligence, operational analytics, and automation to enable efficient, data-driven IT organizations using the HP ABR approach.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.