Repository navigation
Expand file tree
/
Copy pathHADRTwoNodes.java
More file actions
146 lines (135 loc) · 3.7 KB
/
Copy pathHADRTwoNodes.java
File metadata and controls
146 lines (135 loc) · 3.7 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
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
package auto;
import java.sql.*;
import java.util.Properties;
/**
*
* @author tomchen
*
*/
public class HADRTwoNodes {
// RMA的连接
Connection connection;
/**
*
* @param userName 用户名
* @param password 密码
* @param serverName ip或者域名
* @param port 端口
*/
HADRTwoNodes(String userName, String password, String serverName, String port) {
try {
DriverManager.registerDriver((Driver) Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance());
String connectionString = "jdbc:sybase:Tds:" + serverName + ":" + port;
Properties info = new Properties();
info.put("user", userName);
info.put("password", password);
info.put("ENCRYPT_PASSWORD", "true");
connection = DriverManager.getConnection(connectionString, info);
} catch (Exception exp) {
exp.printStackTrace();
}
}
/**
* 根据输入命令,返回执行结果
* 这儿使用的是executeQuery,注意和execute区分
* @param sqlCmd 命令
* @return
*/
public int getResultBySQLCmd(String sqlCmd) {
int numberOfItems = 0;
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sqlCmd);
ResultSetMetaData rsMetaData = rs.getMetaData();
Integer totalCols = rsMetaData.getColumnCount();
while (rs.next()) {
numberOfItems++;
for (int i = 1; i <= totalCols; i++) {
String value = rs.getString(i);
System.out.print(value + "\t\t\t");
}
System.out.println();
}
rs.close();
stmt.close();
} catch (Exception exp) {
exp.printStackTrace();
return numberOfItems;
}
return numberOfItems;
}
public int getResultOfDBTable(String useDBCmd, String tbOpCmd, String cmdType) {
int numberOfItems = 0;
try {
Statement stmt = connection.createStatement();
stmt.execute(useDBCmd);
// stmt.executeUpdate(useDBCmd);
if (cmdType.equals("NORET")) {
stmt.execute(tbOpCmd);
} else {
System.out.println();
ResultSet rs = stmt.executeQuery(tbOpCmd);
while (rs.next()) {
ResultSetMetaData rsMetaData = rs.getMetaData();
Integer totalCols = rsMetaData.getColumnCount();
numberOfItems++;
for (int i = 1; i <= totalCols; i++) {
String value = rs.getString(i);
System.out.print(value + "\t\t\t");
}
System.out.println();
}
rs.close();
}
stmt.close();
} catch (Exception exp) {
exp.printStackTrace();
return numberOfItems;
}
return numberOfItems;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
connection.close();
}
/**
* 异步命令,循环判断是否执行完毕
*/
public void waitUntilFinished() {
int counter = 0;
boolean notFinished = true;
while (counter < 10 && notFinished) {
System.out.println("#################################");
try {
Thread.sleep(3000);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("sap_status task");
ResultSetMetaData rsMetaData = rs.getMetaData();
Integer totalCols = rsMetaData.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= totalCols; i++) {
String value = rs.getString(i);
System.out.print(value + "\t\t\t");
if (i == 3 && value.trim().toLowerCase().equals("Completed".toLowerCase())) {
System.out.println("Finished!");
notFinished = false;
}
}
System.out.println();
}
rs.close();
stmt.close();
} catch (Exception exp) {
exp.printStackTrace();
throw new RuntimeException("Error:Error happened in sap_status");
}
}
// 防止异步命令一直执行,无法终止
if (counter >= 10) {
throw new RuntimeException("Error:Wait completed failed:Timeout!");
} else {
System.out.println("Task completed!");
}
}
}