GROOVY常用功能

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 Method
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator

// ContentType static import
import static groovyx.net.http.ContentType.*
// Method static import
import 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值