Skip to content

Instantly share code, notes, and snippets.

@marcelmaatkamp
Created March 24, 2010 10:30
Show Gist options
  • Save marcelmaatkamp/342157 to your computer and use it in GitHub Desktop.
Save marcelmaatkamp/342157 to your computer and use it in GitHub Desktop.
#!/usr/bin/env groovy
import groovy.sql.Sql
import com.mysql.jdbc.*
/**
* A small example script on how to connect to a mysql database in a groovy script.
*
* @author: Marcel Maatkamp (m.maatkamp avec gmail dot com)
*/
@Grap(group='mysql', module='mysql-connector-java', version='5.1.12')
class MyDB {
def db = Sql.newInstance("jdbc:mysql://hostname/database", "username", "password", "com.mysql.jbc.Driver")
def getUsers() {
def results = new HashMap()
db.eachRow("select number, first, last from user", { user ->
results.put(user.number, [ number: user.number, firstname: user.first, lastname: user.last])
})
return results
}
}
def mydb = new MyDB()
mydb.getUsers().each { user
println "$user.key \t $user.value.lastname, \t $user.value.firstname"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment