1 8 package org.mmbase.applications.crontab; 9 10 import java.sql.*; 11 import javax.sql.DataSource ; 12 import org.mmbase.storage.implementation.database.GenericDataSource; 13 import org.mmbase.module.core.MMBase; 14 import org.mmbase.util.logging.*; 15 16 23 24 public class MMBaseQueryJob extends AbstractCronJob { 25 private static final Logger log = Logging.getLoggerInstance(MMBaseQueryJob.class); 26 27 private String sql; 28 protected void init() { 29 sql = cronEntry.getConfiguration(); 31 32 } 33 34 public final void run() { 35 try { 36 MMBase mmbase = MMBase.getMMBase(); 37 38 DataSource dataSource = ((org.mmbase.storage.implementation.database.DatabaseStorageManagerFactory) mmbase.getStorageManagerFactory()).getDataSource(); 40 Connection connection; 42 if (dataSource instanceof GenericDataSource) { 43 connection = ((GenericDataSource) dataSource).getDirectConnection(); 45 } else { 46 connection = dataSource.getConnection(); 47 } 48 long start = System.currentTimeMillis(); 49 Statement statement = connection.createStatement(); 50 ResultSet results = statement.executeQuery(sql); 51 log.service("Executed " + sql + " in " + (System.currentTimeMillis() - start) + " ms"); 52 ResultSetMetaData meta; 53 54 try { 55 meta = results.getMetaData(); 56 StringBuffer heading = new StringBuffer (); 57 for (int i = 1; i <= meta.getColumnCount(); i++) { 58 heading.append(results.getMetaData().getColumnName(i)); 59 if (i < meta.getColumnCount()) heading.append("|"); 60 } 61 log.service(heading.toString()); 62 while(results.next()) { 63 StringBuffer row = new StringBuffer (); 64 for (int i = 1; i <= meta.getColumnCount(); i++) { 65 heading.append(results.getString(i)); 66 if (i < meta.getColumnCount()) heading.append("|"); 67 } 68 log.service(row); 69 } 70 } catch (Exception e) { 71 log.service(e.getMessage() + ": Cannot show results of '" + sql + "'"); 74 } 75 if (results != null) results.close(); 76 if (statement != null) statement.close(); 77 if (connection != null) connection.close(); 78 } catch (Exception e) { 79 log.error(e.getMessage(), e); 80 } 81 } 82 } 83 | Popular Tags |