001: package org.dbbrowser;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.io.BufferedWriter;
006: import java.io.FileWriter;
007: import java.sql.Connection;
008: import java.sql.DriverManager;
009: import java.sql.ResultSet;
010: import java.sql.Statement;
011: import java.text.DateFormat;
012: import java.text.SimpleDateFormat;
013: import java.util.Date;
014:
015: import javax.swing.BoxLayout;
016: import javax.swing.JButton;
017: import javax.swing.JFrame;
018:
019: public class OpenConnectionsSummary extends JFrame implements
020: ActionListener, Runnable {
021: private JButton start = null;
022: private JButton stop = null;
023: private boolean stopped = false;
024:
025: public static void main(String[] args) {
026: System.out.println("Starting application...");
027: (new OpenConnectionsSummary()).show();
028: }
029:
030: public OpenConnectionsSummary() {
031: super ("Open connections summary");
032: this .setSize(600, 400);
033: this .setLocationRelativeTo(null);
034:
035: start = new JButton("Start");
036: start.addActionListener(this );
037: stop = new JButton("Stop");
038: stop.addActionListener(this );
039: stop.setEnabled(false);
040:
041: this .setResizable(false);
042: this .getContentPane().setLayout(
043: new BoxLayout(this .getContentPane(), BoxLayout.X_AXIS));
044: this .getContentPane().add(start);
045: this .getContentPane().add(stop);
046:
047: this .setDefaultCloseOperation(EXIT_ON_CLOSE);
048:
049: }
050:
051: public void actionPerformed(ActionEvent e) {
052: System.out.println("Pressed " + e.getActionCommand());
053:
054: if ("Start".equals(e.getActionCommand())) {
055: stopped = false;
056: (new Thread(this )).start();
057: start.setEnabled(false);
058: stop.setEnabled(true);
059: }
060:
061: if ("Stop".equals(e.getActionCommand())) {
062: stopped = true;
063: start.setEnabled(true);
064: stop.setEnabled(false);
065: }
066: }
067:
068: public void run() {
069: try {
070: System.out.println("Pinging...");
071: String sql = "select status, count(schemaname) as open_connections from v$session where schemaname='XCSSYS' group by status order by status";
072: DateFormat format = new SimpleDateFormat("HH:mm:ss");
073: Class.forName("oracle.jdbc.OracleDriver");
074:
075: //Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:migrate", "system", "qwerty");
076: Connection connection = DriverManager.getConnection(
077: "jdbc:oracle:thin:@192.168.223.87:1521:xcpt",
078: "xcssys", "portal");
079:
080: FileWriter fw = new FileWriter("open-connections.csv", true);
081: BufferedWriter writer = new BufferedWriter(fw);
082:
083: while (!stopped) {
084: //Run the sql
085: Statement statement = connection.createStatement();
086: ResultSet rs = statement.executeQuery(sql);
087: int totalOpenConnections = 0;
088:
089: String data = "";
090: while (rs.next()) {
091: data = data + rs.getString("status");
092: data = data + ", ";
093: int connections = rs.getInt("open_connections");
094: data = data + connections;
095: data = data + ", ";
096: totalOpenConnections = totalOpenConnections
097: + connections;
098: }
099: data = data + "TOTAL, " + totalOpenConnections + ", ";
100:
101: Date now = new Date();
102: data = data + format.format(now);
103: System.out.println(data);
104: writer.write(data + "\n");
105:
106: rs.close();
107: statement.close();
108: writer.flush();
109:
110: Thread.sleep(1000);
111: }
112:
113: connection.close();
114: writer.close();
115: fw.close();
116: } catch (Exception exc) {
117: System.out.println("*** Exception ***");
118: System.out.println(exc.getMessage());
119: }
120: System.out.println("End");
121: }
122: }
|