KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > mysql > ProcessListTask


1 package net.sf.jasperreports.mysql;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.ResultSet JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.sql.Statement JavaDoc;
7
8 /**
9  * Extends task to run an update on process list information from
10  * an sql databse.
11  */

12
13 public class ProcessListTask extends Task
14 {
15
16     /**
17      * sets the databases source and desitanation in Task.
18      *
19      * @param source - the database to read from.
20      * @param destination - the database to write to.
21      */

22     public ProcessListTask(Database source, Database destination)
23     {
24         super(source, destination);
25     }
26
27     /**
28      * gets information from SHOW PROCESS LIST in source and dumps it into destination.
29      * <p/>
30      * columns retrieved:
31      * report_time TIMESTAMP(12), //the time at which this function was called.
32      * user VARCHAR(32),
33      * host VARCHAR(32),
34      * database_name VARCHAR(32),
35      * time DATETIME,
36      * info VARCHAR(32)
37      */

38     public void run()
39     {
40         //System.out.print("updating process_list...");
41
try
42         {
43             String JavaDoc currentSource = parent.name;
44
45             //connect
46
Connection JavaDoc userConn = parent.getConnection();
47             Connection JavaDoc repConn = destination.getConnection();
48
49             //get the process list data
50
Statement JavaDoc get_processList = userConn.createStatement();
51             ResultSet JavaDoc ProcessList = get_processList.executeQuery(
52                     "SHOW PROCESSLIST;");
53
54             //process list columns:
55
// source VARCHAR(32),
56
// report_time DATETIME,
57
// user VARCHAR(32),
58
// host VARCHAR(32),
59
// database_name VARCHAR(32),
60
// time DATETIME,
61
// info VARCHAR(32)
62
StringBuffer JavaDoc query = new StringBuffer JavaDoc(256);
63             Statement JavaDoc update = repConn.createStatement();
64             while (ProcessList.next())
65             {
66                 query.append("INSERT INTO process_list VALUES ('");
67                 query.append(currentSource);
68                 query.append("', NOW(), '");
69                 query.append(ProcessList.getString("User"));
70                 query.append("', '");
71                 query.append(ProcessList.getString("Host"));
72                 query.append("', '");
73                 query.append(ProcessList.getString("db"));
74                 query.append("', ");
75                 query.append(ProcessList.getInt("Time"));
76                 query.append(");");
77                 update.executeUpdate(query.toString());
78                 query.setLength(0);
79             }
80             update.close();
81             ProcessList.close();
82             get_processList.close();
83         } catch (SQLException JavaDoc e)
84         {
85             e.printStackTrace();
86             System.err.println("Exception: " + e.getMessage());
87             System.err.println("The processlist task did not complete.");
88             System.exit(1);
89         }
90         //System.out.println("Process List Task executed");
91
}
92
93 }
94
Popular Tags