KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.jasperreports.mysql;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Statement JavaDoc;
8 import java.util.Date JavaDoc;
9
10
11 /**
12  * Extends task to run an update on table status information from
13  * an sql database.
14  */

15 public class TableStatusTask extends Task
16 {
17
18     /**
19      * sets the databases source and destination in Task.
20      *
21      * @param source - the database to read from.
22      * @param destination - the database to write to.
23      */

24     public TableStatusTask(Database source, Database destination)
25     {
26         super(source, destination);
27     }
28
29     /**
30      * gets information from SHOW TABLE STATUS in source and inserts it into destination.
31      *
32      *Columns retrieved:
33      * report_time TIMESTAMP(12), //the time at which this function was called.
34      * name VARCHAR(255),
35      * create_time DATETIME,
36      * rows BIGINT,
37          * average_row_length BIGINT,
38          * data_length BIGINT,
39          * index_length BIGINT,
40          * pdate_time DATETIME,
41          * check_time DATETIME
42      */

43     public void run()
44     {
45         //System.out.print("updating table_status...");
46
try
47         {
48             String JavaDoc currentDatabase = parent.url;
49             String JavaDoc currentSource = parent.name;
50
51             //connect
52
Connection JavaDoc userConn = parent.getConnection();
53             Connection JavaDoc repConn = destination.getConnection();
54
55             //get the database list
56
Statement JavaDoc show_databases = userConn.createStatement();
57             ResultSet JavaDoc dbs = show_databases.executeQuery("SHOW DATABASES;");
58
59             //prepare the insert statement
60
PreparedStatement JavaDoc update_tableStatus = repConn.prepareStatement("INSERT INTO table_status "
61                                                                             +
62                                                                             "VALUES (?,?,?,?,?,?,?,?,?,?,?);");
63
64             while (dbs.next())
65             {
66                 //switch database
67
Statement JavaDoc use = userConn.createStatement();
68                 use.executeUpdate("USE " + dbs.getString("Database"));
69                 use.close();
70
71                 //get the table status for a database
72
Statement JavaDoc get_tableStatus = userConn.createStatement();
73                 ResultSet JavaDoc tableStatus = get_tableStatus.executeQuery(
74                         "SHOW TABLE STATUS");
75
76                 //get the current time
77
Date JavaDoc d = new Date JavaDoc();
78                 java.sql.Timestamp JavaDoc SQLdate = new java.sql.Timestamp JavaDoc(
79                         d.getTime());
80
81 //table status columns:
82
// source VARCHAR(32),
83
// report_time DATETIME,
84
// name VARCHAR(32),
85
// create_time DATETIME,
86
// rows BIGINT,
87
// average_row_length BIGINT,
88
// data_length BIGINT,
89
// index_length BIGINT,
90
// pdate_time DATETIME,
91
// check_time DATETIME
92
while(tableStatus.next()){
93                     update_tableStatus.setString(1, currentSource);
94                     update_tableStatus.setString(2, dbs.getString("Database"));
95                     update_tableStatus.setTimestamp(3,SQLdate);
96                     update_tableStatus.setString(4, tableStatus.getString("Name"));
97                     update_tableStatus.setTimestamp(5, tableStatus.getTimestamp("Create_time"));
98                     update_tableStatus.setString(6, tableStatus.getString("Rows"));
99                     update_tableStatus.setLong(7, tableStatus.getLong("Avg_row_length"));
100                     update_tableStatus.setLong(8, tableStatus.getLong("Data_length"));
101                     update_tableStatus.setLong(9, tableStatus.getLong("Index_length"));
102                     update_tableStatus.setTimestamp(10, tableStatus.getTimestamp("Update_time"));
103                     update_tableStatus.setTimestamp(11, tableStatus.getTimestamp("Check_time"));
104                     //execute
105
update_tableStatus.executeUpdate();
106                    }
107                 tableStatus.close();
108                 get_tableStatus.close();
109             }
110             dbs.close();
111             update_tableStatus.close();
112             show_databases.close();
113         } catch (SQLException JavaDoc e)
114         {
115             e.printStackTrace();
116             System.out.println("Exception: " + e.getMessage());
117             System.out.println("The Table Status task did not complete.");
118             System.exit(1);
119         }
120         //System.out.println("Table Status Task executed");
121
}
122 }
123
Popular Tags