SqlExecutor.java
3.23 KB
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
package com.sitech.ismp.check.mbean;
import java.util.Date;
import java.util.HashMap;
import com.sitech.ismp.check.util.SqlCollThread;
import com.sitech.ismp.messageObject.cc.CommandParameters;
import com.sitech.ismp.messageObject.cc.CommandResults;
public class SqlExecutor implements SqlExecutorMBean {
public static String Exec_Flag_Success = "1";
public static String Exec_Flag_Failure = "-1";
public CommandResults executeSqls(CommandParameters cps) {
HashMap _customProp = cps.getCUSTOMPROP();
String _taskid = cps.getTASK_ID();
int _type = cps.getTYPE();
// String _content = "";
// String _dbdriver = "";
String _dburl = "";
// String _dbusername = "";
// String _dbpassword = "";
Date _begintime = null;
Date _endtime = null;
String _execresult = "";
String _execlog = "";
String _execflag = "";
String _failurereasion = "";
_execlog = "指令执行器准备开始执行SQL\n";
CommandResults crs = new CommandResults();
_execlog += "准备连接数据库" + _dburl + ",并执行SQL语句\n";
SqlCollThread sct = new SqlCollThread(cps);
// init(cps);
try {
_begintime = new Date();
_execresult = sct.run();
_endtime = new Date();
if (_execresult.indexOf("执行SQL语句失败") == -1) {
_execlog += "执行SQL结束\n";
_execflag = Exec_Flag_Success;
} else {
_execlog += "执行SQL结束\n";
_execflag = Exec_Flag_Failure;
_failurereasion = _execresult;
}
} catch (Exception e) {
_endtime = new Date();
e.printStackTrace();
_execlog += "执行SQL结束\n";
_execflag = Exec_Flag_Failure;
_failurereasion = e.getMessage();
_execresult = e.getMessage();
} finally {
crs.setTASK_ID(_taskid);
crs.setCUSTOMPROP(_customProp);
crs.setTYPE(_type);
crs.setBEGIN_TIME(_begintime);
crs.setEND_TIME(_endtime);
crs.setEXEC_RESULT(_execresult);
crs.setEXEC_LOG(_execlog);
crs.setEXEC_FLAG(_execflag);
crs.setFAILURE_REASION(_failurereasion);
crs.setCUSTOMPROP(_customProp);
}
return crs;
}
// public String init(CommandParameters cps) {
// _taskid = cps.getTASK_ID();
// _type = cps.getTYPE();
// _content = cps.getCONTENT();
// _dburl = cps.getDB_URL();
// _customProp = cps.getCUSTOMPROP();
// if (String.valueOf(_type) != null && _type == 1) {
// // SHELL指令方式,此类处理的是SQL/存储过程指令方式
// } else if (String.valueOf(_type) != null && (_type == 2 || _type == 3)) {
// // SQL/存储过程指令方
// _execlog += "准备连接数据库" + _dburl + ",并执行SQL语句\n";
// sct = new SqlCollThread(cps);
// }
// return "SUCCESS";
// }
/**
* @param args
*/
public static void main(String[] args) {
SqlExecutor se = new SqlExecutor();
CommandParameters cps = new CommandParameters();
cps.setTASK_ID("TaskID-mooker");
cps.setTYPE(2);
// cps.setCONTENT("select * from tb_cde_kpi where kpi_id like 'PM-00-01-005-0%' order by kpi_id");
cps.setCONTENT("select 111 as aa from dual");
cps.setDB_DRIVER("oracle.jdbc.driver.OracleDriver");
cps.setDB_URL("jdbc:oracle:thin:@172.21.1.9:1533:bnms");
cps.setDB_USERNAME("bnms1");
cps.setDB_PASSWORD("bnms1");
CommandResults cr = se.executeSqls(cps);
System.out.println(cr.getEXEC_RESULT());
System.out.println(cr.getTASK_ID());
}
}