Created
March 24, 2010 10:30
-
-
Save marcelmaatkamp/342157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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