import java.net.*; import java.sql.*; import java.io.*; import java.text.*; import java.util.*; class MarketAvg { 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){ } ResultSet result = null; double Avg = 0.00; int Count = 0; try { result = stmt.executeQuery("select Last from Companies"); while (result.next()) { Avg += result.getDouble("Last"); Count++; } } catch(Exception e) { } // Compute the average if (Count > 0) { Avg = Avg / Count; } // Add entry to htsys try { stmt.executeUpdate("insert into htsys (Dtime, TradeAt) values (NOW(), " + Avg + ")"); } catch (Exception e) { } } }