this.getClass().classLoader.rootLoader.addURL(new File("file.jar").toURL())
http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc41.jar
JDBC41 Postgresql Driver, Version 9.3-1102
import groovy.sql.*// Create connection to MySQL with classic JDBC DriverManager.def db = Sql.newInstance("jdbc:mysql://localhost/groovy", 'groovy', 'groovy', 'com.mysql.jdbc.Driver')// Create a new tabledb.execute 'drop table if exists languages'db.execute '''create table languages(id integer not null auto_increment,name varchar(20) not null,primary key(id))'''// Create a DataSet.def languageSet = db.dataSet("languages")languageSet.add(id: null, name: 'Groovy')languageSet.add(name: 'Java') languageSet.add(name: 'JRuby')languageSet.add(name: 'Scala')// Get data with each method.def result = []languageSet.each {result << it.name}assert 4 == result.size()assert ['Groovy', 'Java', 'JRuby', 'Scala'] == result// Use findAll and sort to define a query condition.def firstItems = languageSet.findAll { it.id < 3 }.sort { it.name }// No database acccess yet, only the query is constructed.// We can see the query with sql property and parameters with parameters property.assert 'select * from languages where id > ? order by name' == firstItems.sqlassert [3] == firstItems.parameters// We call each to really access the database.firstItems.findAll { it.name == 'Groovy' }.each { row ->assert 1 == row.idassert 'Groovy' == row.name}
沒有留言:
張貼留言