-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonDB_Oracle.java
More file actions
290 lines (253 loc) · 8.6 KB
/
MonDB_Oracle.java
File metadata and controls
290 lines (253 loc) · 8.6 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package Jvakt;
/*
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.sql.*;
import java.util.Date;
import java.util.Properties;
import Jvakt.Message;
import Jvakt.SendMsg;
public class MonDB_Oracle
{
// jvakt
static String agent = null;
static String jvhost = "localhost";
static String jvport = "1956";
static int jvporti ;
static InetAddress inet;
static String config = null;
static File configF;
static boolean msgFixat = false;
// jvakt
static boolean swShow = false;
static boolean swRun = false;
static boolean swList = false;
static String host;
static String sid;
static String port;
static String user;
static String pw;
static String collectionName;
static String table;
static String where = "";
static String url;
static String stmt;
static String state = "ERR";
static String version = "monDB_Oracle ";
static int antal = 0 ;
// Format a string so that it has the specified width.
private static String format (String s, int width)
{
String formattedString;
// The string is shorter than specified width,
// so we need to pad with blanks.
if (s.length() < width) {
StringBuffer buffer = new StringBuffer (s);
for (int i = s.length(); i < width; ++i)
buffer.append (" ");
formattedString = buffer.toString();
}
// Otherwise, we need to truncate the string.
else
formattedString = s.substring (0, width);
return formattedString;
}
public static void main (String[] args)
{
version += getVersion()+".54";
// Check the input parameters.
if (args.length < 1) {
System.out.println("\n " +version);
System.out.println("\n\nThe parameters and their meaning are:\n"+
"\n-config \tThe dir of the input files. Like: \"-dir c:\\Temp\" "+
"\n-run \tTo actually update the status in Jvakt."+
"\n-host \tThe host name or IP address of the Oracle server." +
"\n-port \tThe port number of the Oracle server." +
"\n-sid \tThe sid of the Oracle database." +
"\n-user \tThe username in the Oracle database." +
"\n-pw \tThe password." +
"\n-table \tThe table to query." +
"\n-where \tThe where statement of the query." +
"\n-show \tShow the response from the server." +
"\n-list \tList the result from the query."
);
System.exit(4);
}
// reads command line arguments
for ( int i = 0; i < args.length; i++) {
if (args[i].equalsIgnoreCase("-run")) swRun = true;
if (args[i].equalsIgnoreCase("-host")) { host = args[++i]; }
if (args[i].equalsIgnoreCase("-port")) { port = args[++i]; }
if (args[i].equalsIgnoreCase("-sid")) { sid = args[++i]; }
if (args[i].equalsIgnoreCase("-user")) { user = args[++i]; }
if (args[i].equalsIgnoreCase("-pw")) { pw = args[++i]; }
if (args[i].equalsIgnoreCase("-table")) { table = args[++i]; }
if (args[i].equalsIgnoreCase("-where")) { where = args[++i]; }
if (args[i].equalsIgnoreCase("-config")) config = args[++i];
if (args[i].equalsIgnoreCase("-show")) swShow = true;
if (args[i].equalsIgnoreCase("-list")) swList = true;
}
// jvakt
if (config == null ) configF = new File("Jvakt.properties");
else configF = new File(config,"Jvakt.properties");
if (swShow && swRun) System.out.println("-config file: "+configF);
// Properties props = new Properties();
if (swRun) getProps(); // get Jvakt properties
// jvakt
Connection connection = null;
try {
// Load the Ora thin JDBS driver.
//DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Class.forName("oracle.jdbc.OracleDriver") ;
if (swShow) System.out.println("Oracle JDBC driver loaded ok.");
// Get a connection to the database. Since we do not
url = "jdbc:oracle:thin:"+user+"/"+pw+"@//"+host+":"+port+"/"+sid ;
if (swShow) System.out.println(url +"\n");
connection = DriverManager.getConnection(url);
// DatabaseMetaData dmd = connection.getMetaData ();
// Execute the query.
Statement select = connection.createStatement ();
if (swList) {
if (where.length()>1) stmt = ("SELECT * FROM " + table + " WHERE " + where);
else stmt = ("SELECT * FROM " + table );
}
else {
if (where.length()>1) stmt = ("SELECT count(*) FROM " + table + " WHERE " + where);
else stmt = ("SELECT count(*) FROM " + table );
}
state = "OK";
if (swShow) System.out.println(stmt +"\n");
ResultSet rs = select.executeQuery (stmt);
if (swShow || swList) {
// Get information about the result set. Set the column
// width to whichever is longer: the length of the label
// or the length of the data.
ResultSetMetaData rsmd = rs.getMetaData ();
int columnCount = rsmd.getColumnCount ();
String[] columnLabels = new String[columnCount];
int[] columnWidths = new int[columnCount];
for (int i = 1; i <= columnCount; ++i) {
columnLabels[i-1] = rsmd.getColumnLabel (i);
columnWidths[i-1] = Math.max (columnLabels[i-1].length(),
rsmd.getColumnDisplaySize (i));
}
// Output the column headings.
for (int i = 1; i <= columnCount; ++i) {
System.out.print (format (rsmd.getColumnLabel(i), columnWidths[i-1]));
System.out.print (" ");
}
System.out.println ();
// Output a dashed line.
// StringBuffer dashedLine;
for (int i = 1; i <= columnCount; ++i) {
for (int j = 1; j <= columnWidths[i-1]; ++j)
System.out.print ("-");
System.out.print (" ");
}
System.out.println ();
// Iterate through the rows in the result set and output
// the columns for each row.
antal = 0;
while (rs.next ()) {
for (int i = 1; i <= columnCount; ++i) {
String value = rs.getString (i);
if (rs.wasNull ())
value = "<null>";
System.out.print (format (value, columnWidths[i-1]));
System.out.print (" ");
state = "OK";
}
antal++;
System.out.println ();
}
}
}
catch (Exception e) {
if (swShow) System.out.println ("ERROR: " + e.getMessage());
state = "ERR";
}
finally {
if (state.startsWith("OK")) {
System.out.println(new Date()+" -- Connection succcessful "+host+":"+port+" "+sid);
}
else {
System.out.println(new Date()+" -- Connection failed "+host+":"+port+" "+sid);
antal = 0;
}
if (swRun) {
try {
sendJv("MonDB_Oracle_"+host+":"+port+"-"+sid , state , "R", "Checking Oracle connection to: "+host+":"+port+" "+sid);
}
catch (IOException e) {
// Ignore.
}
}
// Clean up.
try {
if (connection != null)
connection.close ();
}
catch (SQLException e) {
// Ignore.
}
}
if (swShow) System.out.println ("-- Antal: "+antal);
System.exit (antal);
}
static protected void sendJv( String ID, String STS, String type, String msg) throws IOException {
Message jmsg = new Message();
SendMsg jm = new SendMsg(jvhost, jvporti);
if (swShow) System.out.println(jm.open());
else jm.open();
jmsg.setId(ID);
jmsg.setRptsts(STS);
jmsg.setBody(msg);
jmsg.setType(type);
jmsg.setAgent(agent);
jm.sendMsg(jmsg);
if (jm.close()) {
if (swShow) System.out.println("-- Rpt Delivered --");
msgFixat = true;
}
else System.out.println("-- Rpt Failed --");
}
static void getProps() {
// jvakt
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(configF);
prop.load(input);
// get the property value and print it out
jvport = prop.getProperty("jvport");
jvhost = prop.getProperty("jvhost");
jvporti = Integer.parseInt(jvport);
if (swShow) System.out.println("getProps jvport:" + jvport + " jvhost:"+jvhost) ;
} catch (IOException ex) {
ex.printStackTrace();
}
try {
inet = InetAddress.getLocalHost();
if (swShow) System.out.println("-- Inet: "+inet);
agent = inet.toString();
}
catch (Exception e) { System.out.println(e); }
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
}