Feeds

Groovy way to MySQL

Get your Groove on

High performance access to file storage

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

High performance access to file storage

More from The Register

next story
Android engineer: We DIDN'T copy Apple OR follow Samsung's orders
Veep testifies for Samsung during Apple patent trial
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Windows XP still has 27 per cent market share on its deathbed
Windows 7 making some gains on XP Death Day
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
Windows 8.1, which you probably haven't upgraded to yet, ALREADY OBSOLETE
Pre-Update versions of new Windows version will no longer support patches
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
prev story

Whitepapers

Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
Five 3D headsets to be won!
We were so impressed by the Durovis Dive headset we’ve asked the company to give some away to Reg readers.
HP ArcSight ESM solution helps Finansbank
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.