package pickMaint; import java.io.FileWriter; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Logger; /** * This is a quick routine I wrote to automatically run some SQL queries I developed to judge how well the layout/tablet programs are * performing. It works by figuring out what and how many rows were adjusted by Gloria/Val after the data was submitted from the tablets * by Chelsea/Traci. * * Spits out an excel file for all 3 shifts for each date in the DATE[] constant. * * @author Wes */ public class Analyze { private static final int[] DATE = new int[]{ 141117 }; private static final Logger LOGGER = Logger.getLogger(Analyze.class.getName()); private static final String[] COLUMN_NAMES = new String[]{ "DATE", "SHIFT", "LOOM #", "WEAVER #", "HOURS", "PICKS", "HOURS ENTERED", "PICKS ACTUAL", "PICKS ENTERED", "HOUR ADJUSTMENT", "PICK ADJUSTMENT", "DOWN TIME CODE"}; private static final String ROWS_CHANGED_QUERY = "SELECT lddate, " + " ldshif, " + " ldloom, " + " ldweav, " + " ldhrs, " + " ldpick, " + " HOURS, " + " pksact, " + " pksent, " + " ldhrs - HOURS HRS_ADJ, " + " ldpick - pksact PKS_ADJ, " + " '(' " + " || stmdata.iedtfl.iedtcd " + " || ') ' " + " || stmdata.iedtfl.iedtds dtcode " + "FROM stmdata.loomds " + " LEFT JOIN stmdata.pksread ON lddate = DATE AND " + " ldshif = shift AND " + " ldloom = loom " + " LEFT JOIN stmdata.IEDTFL ON stmdata.loomds.ldreas = stmdata.iedtfl.iedtcd " + "WHERE lddate = ? AND " + " ldshif = ? AND " + " (ldhrs != HOURS OR " + " ldpick != pksact) AND " + " NOT (ldweav > 699) AND " + " ldloom NOT IN (SELECT ldloom " + " FROM stmdata.loomds " + " WHERE lddate = ? AND " + " ldshif = ? AND " + " ldsty1 = ' SAM') " + "ORDER BY ldloom "; private static final String TOTAL_ROWS = "SELECT COUNT(*) total_rows " + "FROM stmdata.pksread " + "WHERE date = ? AND " + " shift = ? "; public static void main(String[] args) throws SQLException, IOException { // Register AS400 DB2 driver and open a connection with the AS400 LOGGER.info("Connecting to AS400 as user: WRAY\n"); DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver()); Connection conn = DriverManager.getConnection("jdbc:as400://0.0.0.0", "USER", "PASS"); LOGGER.info("Pre-compiling SQL statements...\n"); PreparedStatement rowsChanged = conn.prepareStatement(ROWS_CHANGED_QUERY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); PreparedStatement totalRows = conn.prepareStatement(TOTAL_ROWS); LOGGER.info("Downloading data for all shifts for " + DATE.length + " days...\n"); for (int date : DATE) { //System.out.println("Date: " + date); for (int shift = 1; shift < 4; shift++) { //System.out.println("Shift: " + shift); rowsChanged.setInt(1, date); rowsChanged.setInt(2, shift); rowsChanged.setInt(3, date); rowsChanged.setInt(4, shift); totalRows.setInt(1, date); totalRows.setInt(2, shift); ResultSet changed = rowsChanged.executeQuery(); ResultSet total = totalRows.executeQuery(); LOGGER.info(((shift == 1) ? "1st" : (shift == 2) ? "2nd" : (shift == 3) ? "3rd" : "") + " shift downloaded. Dumping to Excel sheet...\n"); int tot = 0; if (total.next()) tot = total.getInt(1); FileWriter out = new FileWriter(date + "_" + shift + ".xls"); String buffer = ""; for (int i = 0; i < COLUMN_NAMES.length; i++) { buffer += (COLUMN_NAMES[i] + "\t"); } out.write(buffer + "\n"); buffer = ""; while (changed.next()) { for (int i = 0; i < COLUMN_NAMES.length; i++) { if(i == 11) { String dt = "" + changed.getObject(i + 1); if(dt.equalsIgnoreCase("null")) buffer += (" \t"); else buffer += (dt.trim() + "\t"); } else { buffer += (changed.getObject(i + 1) + "\t"); } } out.write(buffer + "\n"); buffer = ""; } int c = getRowCount(changed); out.write("\nROWS CHANGED\tTOTAL ROWS\n"); out.write(c + "\t" + tot + "\t \t" + "=1-(A" + (c + 4) + "/B" + (c + 4) + ")" + "\n"); out.write(" \t \t \t=1-(A" + (c + 5) + "/B" + (c + 4) + ")" + "\n"); out.flush(); // Make sure everything gets written out.close(); out = null; changed.close(); total.close(); } } LOGGER.info("Tidying up open resources and exiting..."); rowsChanged.close(); totalRows.close(); rowsChanged = null; totalRows = null; conn.close(); conn = null; System.exit(0); } /** * The ResultSet you pass to this method must be from a PreparedStatement created with these args: * prepareStatement("select * from ...", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); * * This will throw an exception with FORWARD_ONLY ResultSets (as in the default one you get with no extra arguments) * * ResultSet.CONCUR_READ_ONLY may actually be something else if you so desire. TYPE_SCROLL_INSENSITIVE is what is important. * * @param set - The ResultSet to count rows in * @return The number of rows in the ResultSet * @throws SQLException */ public static int getRowCount(ResultSet set) throws SQLException { int rowCount; int currentRow = set.getRow(); // Get current row rowCount = set.last() ? set.getRow() : 0; // Determine number of rows if (currentRow == 0) // If there was no current row set.beforeFirst(); // We want next() to go to first row else // If there WAS a current row set.absolute(currentRow); // Restore it return rowCount; } }