GROOVY常用功能

来源:互联网 发布:鹊桥与淘宝联盟的使用 编辑:程序博客网 时间:2024/04/27 19:58

1、数据库连接

MYSQL

sql = Sql.newInstance("jdbc:mysql://*******:3306/vm", "USER", "PASSWORD", "com.mysql.jdbc.Driver")

ORACLE

sql = Sql.newInstance("jdbc:oracle:thin:@172.16.10.49:1521:orcl", "cms_readonly", "cms_readonly", "oracle.jdbc.driver.OracleDriver")

2、查询数据库

String req = """
select * from vmstatus
"""
sql.eachRow(req){ row ->
map60.put(row.vmid, row.sb);
}

3、发送邮件

import org.apache.commons.mail.DefaultAuthenticator;import org.apache.commons.mail.EmailAttachment;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.MultiPartEmail;
EmailAttachment attachment = new EmailAttachment();attachment.setPath("shengbo_stat_zhifu_result.txt");attachment.setDisposition(EmailAttachment.ATTACHMENT);attachment.setDescription("txt file");attachment.setName("result.txt");MultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.exmail.qq.com");email.setSmtpPort(465);email.setAuthenticator(new DefaultAuthenticator("user@163.cn","passowrd"));email.setSSLOnConnect(true);email.setFrom("ff@163.cn");email.setSubject("统计邮件");email.setMsg("见附件");email.addTo("tt@163.cn");email.attach(attachment);println email.send();
4、乱码问题

export LANG=zh_CN.UTF-8/usr/local/groovy-2.2.2/bin/groovy --encoding=UTF-8 aaa.groovy

5、一个HTTP请求的简单例子

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )import groovyx.net.http.HTTPBuilder//import groovyx.net.http.ContentType // this doesn't import ContentType//import groovyx.net.http.Method // this doesn't import Methodimport groovyx.net.http.RESTClientimport groovyx.net.http.HttpResponseDecorator// ContentType static importimport static groovyx.net.http.ContentType.*// Method static importimport static groovyx.net.http.Method.*def http = new HTTPBuilder()
http.request( 'http://a.b.com', GET, TEXT ) { req ->  uri.path = '/paymethods/paymethod/0001407'  headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4"  headers.Accept = 'application/json'   response.success = { resp, reader ->    assert resp.statusLine.statusCode == 200    println "Got response: ${resp.statusLine}"    println "Content-Type: ${resp.headers.'Content-Type'}"    println reader.text  }   response.'404' = {    println 'Not found'  }}



0 0