import java.net.*; import java.sql.*; import java.io.*; import java.text.*; import java.util.*; class ExchangeParse { private static String MySQL = "isip003.isip.msstate.edu:3306"; // Change this to the name you gave your Exchange database private static String dBase = "bullyse"; private static String _URL = "jdbc:mysql://" + MySQL + "/" + dBase; private static String _user = "bullyse"; private static String _pWord = "BullySe"; // Database objects private static Connection conn; private static Statement stmt; public static void main(String[] args) { // Lets start a database connection try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch(Exception e){ } // Statement shouldn't exist. If it does, close it. if (stmt != null) { try { stmt.close(); } catch(SQLException e) { } } // Connection shouldn't exist. If it does, close it. if (conn != null) { try { conn.close(); } catch(SQLException e) { } } // Open a connection to the database try { conn = DriverManager.getConnection(_URL, _user, _pWord); } catch(SQLException e) { } while (conn == null) { // Error. This isn't good. java.util.Date rightNow = new java.util.Date(); int dMin = rightNow.getMinutes(); rightNow = new java.util.Date(0, 0, 0, 0, dMin, 0); java.util.Date countIt = new java.util.Date(0, 0, 0, 0, (dMin + 5), 0); while (countIt != rightNow) { rightNow = new java.util.Date(); dMin = rightNow.getMinutes() ; rightNow = new java.util.Date(0, 0, 0, 0, dMin, 0); } try { conn = DriverManager.getConnection(_URL, _user, _pWord); } catch(SQLException e) { } } // Set up the statement object try { // initialize the statment object stmt = conn.createStatement(); } catch(SQLException e){ } // Parse the data // First all trading needs to be frozen. try { stmt.executeUpdate("update Parameters set Frozen = 'Y'"); } catch (Exception e) { } ResultSet result = null; ResultSet thisResult = null; Vector Cmps = new Vector(); // Get the names and values of all companies and add to a Vector. try { thisResult = stmt.executeQuery("select Symbol, Last from Companies"); while (thisResult.next()) { String Cmp = thisResult.getString("Symbol"); Cmps.addElement((String)Cmp); } } catch(Exception e) { } // Loop through the Companies and parse whatever is in its // History table. for (int i = 0 ; i < Cmps.size() ; i++) { String Cmp = (String)Cmps.elementAt(i); double avg = 0.00; int count = 0; double val = 0.00; double Lst = 0.00; try { thisResult = stmt.executeQuery("select Last from Companies where Symbol = '" + Cmp + "'"); // Set all daily values. Right now they just go to Last since // there are no numeric manipulations taking place. while (thisResult.next()) { Lst = result.getDouble("Last"); } stmt.executeUpdate("update Companies set Open = " + Lst + ", Previous = " + Lst + ", DayHi = " + Lst + ", DayLo = " + Lst + " where Symbol = '" + Cmp + "'"); thisResult = stmt.executeQuery("select TradeAt from ht" + Cmp.toLowerCase()); while (thisResult.next()) { count++; val += thisResult.getDouble("TradeAt"); } // Get the average value if (count != 0 && val != 0) { avg = val / (double)count; } else { avg = 0; } } catch (Exception e) { } // Get current day of week java.util.Date curDate = new java.util.Date(); int Dat = curDate.getDay() + 1; // Add to symbol's Extended history if (avg > 0) { try { thisResult = stmt.executeQuery("insert into eht" + Cmp.toLowerCase() + " (Value, Date, Shares) values (" + avg + ", NOW(), 10000)"); } catch (Exception e) { } } // Clear symbol's Volume try { stmt.executeUpdate("update Companies set Volume = 0 where Symbol = '" + Cmp + "'"); } catch (Exception e) { } // Delete from History table try { stmt.executeUpdate("delete from ht" + Cmp.toLowerCase()); } catch (Exception e) { } } // Calculate Extended Market Average int count = 0; double val = 0.00; try { thisResult = stmt.executeQuery("select TradeAt from htsys"); while (thisResult.next()) { count++; val += thisResult.getDouble("TradeAt"); } } catch (Exception e) { } if (count > 0) { val = val / count; } try { stmt.executeUpdate("insert into ehtsys (Value, Date) values (" + val + ", NOW())"); stmt.executeUpdate("delete from htsys"); } catch (Exception e) { } // Allow trading to resume try { stmt.executeUpdate("update Parameters set Frozen = 'N'"); } catch (Exception e) { } } }