groovyにおけるファイル,集合,テスト,データベースの操作例


package com.mygroovy.test;

import java.io.File;
import java.util.List;

import org.junit.Test;

import groovy.sql.Sql;

import static org.junit.Assert.*;

class SQLTest {
	public static Sql getSql(){
		String url  = "jdbc:mysql://localhost:3306/user_dev";
		String username = "root";
		String password = "519241";
		String driverClass = "org.gjt.mm.mysql.Driver";
		//     connection
		return Sql.newInstance (url, username, password, driverClass);
	} 
	@Test
	public void test(){
		Sql sql = getSql();
		//    ResultSet,row     ,        
		sql.eachRow "select * from ut_user", {row->
			println row.name
		}
		//         ,         
		def user = sql.firstRow("select * from ut_user");
		println user
		//        ,    list   
		List<User> list = sql.rows("select * from ut_user");
		//      ,it          
		list.each {
			println(it);
		}
		
		//sql.execute("insert");
		
		//sql.execute("create table book(id int primary key,name char(20))");
		//sql.execute("drop table book;");
		//         ,    
		def file = new File("C:/software/QQLicense.txt");
		file.eachLine{println it} 
		
		def str = "abc";
		str<<"d";
		println str;
		println str<<"d";
		println str+"e";
		println str-"a";
		
		//      , assert  
		assert 1.class==Integer
		println 1.01.class
		println 1.0f.class
		println 1.0d.class
		
		def arr = [21,51,14,25,84,214,3];
		//       
		println arr.sort();
		//..    1 3  ,       ,
		assert [1..3] == [[1,2,3]]
		assert [*1..3] == [1,2,3] //   *          
	}
}