漂亮的代码,精妙的实现!

禁止转载

前言

主要是为了备份日常工作或学习过程中,觉得有所感触的代码实现过程,便于后续重复使用。

正文

PLSQL

游标锁表、循环更新

1
2
3
4
5
6
CURSOR c2 IS  
SELECT aac002 FROM IEA4 WHERE CIE578 = PI_CIE578 FOR UPDATE;
...
FOR v2 IN c2 LOOP
UPDATE IEA4 SET AAC002 = nvl(pkg_fun.f_sfzhm15to18(aac002), aac002) WHERE CURRENT OF c2;
END LOOP;

MYSQL

查看编码格式

  • 查看数据库编码格式

    1
    show variables like 'character_set_database';
  • 查看数据表的编码格式

    1
    show create table <表名>;

JAVA

同步&异步httpClient请求

- 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {

/**
* 发送POST 支持http、https
*
* @param url post地址
* @param paramMap 要传递的参数封装成Map
*/
public static String sendPost(String url, Map<String, String> paramMap) {
String returnmsg = "";
try {
//封装参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
if (paramMap != null && paramMap.keySet() != null) {
for (String key : paramMap.keySet()) {
parameters.add(new BasicNameValuePair(key, paramMap.get(key)));
}
}
//创建UrlEncodedFormEntity对象
UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters, "UTF-8");
if (url.startsWith("https")) {
//returnmsg=sendPostHttps(url,formEntiry);
} else {
returnmsg = sendPostHttp(url, formEntiry);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnmsg;
}

//发送http请求
public static String sendPostHttp(String url, UrlEncodedFormEntity formEntiry) {
// 发送请求
HttpClient client = new DefaultHttpClient();
String returnmsg = "fail"; //失败
try {
//实例化HTTP POST方法
HttpPost postmethod = new HttpPost(url);
postmethod.setEntity(formEntiry);

//执行请求
HttpResponse reponse = client.execute(postmethod);
//回去返回实体
HttpEntity entity = reponse.getEntity();
returnmsg = EntityUtils.toString(entity, "UTF-8");
//System.out.println("POST返回数据:"+returnmsg);
//若返回消息有中文要进行一下解码 服务器要加码URLEncoder.encode("服务器返回中文", "UTF-8")
//System.out.println("POST返回数据--:"+URLDecoder.decode(returnmsg,"utf-8"));
} catch (Exception e) {
returnmsg = "fail";
e.printStackTrace();
} finally {
//关闭连接,释放资源
client.getConnectionManager().shutdown();
}
return returnmsg;
}

/**
* application/json
*
* @param url
* @param params
* @return
*/
public static String sendPostHttpJson(String url, String params) {
HttpClient httpClient = null;
HttpPost postMethod = null;
HttpResponse response = null;
String returnmsg = "fail"; //失败
try {
httpClient = HttpClients.createDefault();
postMethod = new HttpPost(url);//传入URL地址
postMethod.addHeader("Content-type", "application/json; charset=utf-8");
postMethod.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
response = httpClient.execute(postMethod);//获取响应
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity httpEntity = response.getEntity();
String reponseContent = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);//释放资源
if (statusCode == HttpStatus.SC_OK) {
returnmsg = reponseContent;
}
} catch (Exception e) {
e.printStackTrace();
}
return returnmsg;
}

/**
* 发送GET请求
*
* @param url
* @return
*/
public static String doGet(String url) {
String reqUrl = url;
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
HttpGet httpGet = new HttpGet(reqUrl);
String result = "fail"; //失败
try {
client = HttpClients.createDefault();
response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity httpEntity = response.getEntity();
String reponseContent = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);//释放资源
if (statusCode == HttpStatus.SC_OK) {
result = reponseContent;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

/**
* 发送异步POST请求
*
* @param url post地址
* @param paramMap 要传递的参数封装成Map
*/
public static void sendAsyncPost(String url, Map<String, String> paramMap, FutureCallback callback) {
//封装参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
if (paramMap != null && paramMap.keySet() != null) {
for (String key : paramMap.keySet()) {
parameters.add(new BasicNameValuePair(key, paramMap.get(key)));
}
}

CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault();
HttpPost httpPost = new HttpPost(url);
try {
UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters, "UTF-8");
httpPost.setEntity(formEntiry);
//start
httpAsyncClient.start();
//异步请求
httpAsyncClient.execute(httpPost, callback);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

/*try {
httpAsyncClient.close();
} catch (IOException e) {
e.printStackTrace();
}*/
}

/**
* 发送异步GET请求
*
* @param url post地址
*/
public static void doAsyncGet(String url, FutureCallback callback) {
CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault();
HttpGet httpGet = new HttpGet(url);
//start
httpAsyncClient.start();
//异步请求
httpAsyncClient.execute(httpGet, callback);

/*try {
httpAsyncClient.close();
} catch (IOException e) {
e.printStackTrace();
}*/
}
}

- 调用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 调用示例
*/
Map<String, String> reqmap = new HashMap<>();
reqmap.put("basicPropertits", jobPropertyJson.toString());
reqmap.put("collectPropertits", jobConfigJson.toString());
reqmap.put("dataStore", jobStoreJson.toString());
reqmap.put("templatetList", jobProcJson.toString());

HttpClientUtil.sendAsyncPost(clctEngineMethodCollectTemplateUrl, reqmap, new launchServiceCallBack(id));

/**
* http异步请求回调处理
*/
private class launchServiceCallBack implements FutureCallback<HttpResponse> {
private int jobId = 0;

launchServiceCallBack(int _iJobid) {
this.jobId = _iJobid;
}

/**
* 请求完成后调用该函数
*/
@Override
public void completed(HttpResponse httpResponse) {
HttpEntity entity = httpResponse.getEntity();
String rtnMsg = "{\"code\":\"fail\"}";
String code = "fail";
try {
rtnMsg = EntityUtils.toString(entity, "UTF-8");
Map<String, String> reqmap = (Map) JSONObject.fromObject(rtnMsg);
code = reqmap.get("code").toString();

if (code.equals(String.valueOf(HttpStatus.SC_OK))) {
JobProperty load = jobPropertyService.load(this.jobId);
load.setReleaseStatus(CollectionConstant.WHETHER_YES);
load.setReleaseTime(new Date());
jobPropertyService.update(load);
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 请求失败后调用该函数
*/
@Override
public void failed(Exception e) {
System.out.println("failed");
}

/**
* 请求取消后调用该函数
*/
@Override
public void cancelled() {
System.out.println("cancelled");
}
}

- 注意事项

  • 调用完成时,需及时关闭回收httpAsyncClient