Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.11 KB | None | 0 0
  1. import java.nio.DoubleBuffer;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.text.DecimalFormat;
  8. import java.util.Scanner;
  9. import javax.management.Descriptor;
  10.  
  11. public class TableReader {
  12.     static Connection getConnection() {
  13.         Connection connection = null;
  14.         try {
  15.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  16.             connection = DriverManager.getConnection(
  17.                     "jdbc:mysql://184.154.73.113/davidlno_StockHistory?" + "user=davidlno_student&password=ez24get!");
  18.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  19.             e.printStackTrace();
  20.         } catch (SQLException e) {
  21.             e.printStackTrace();
  22.             System.out.println("SQLException: " + e.getMessage());
  23.             System.out.println("SQLState: " + e.getSQLState());
  24.             System.out.println("VendorError: " + e.getErrorCode());
  25.         }
  26.         return connection;
  27.     }
  28.  
  29.     public static void reportMenu() {
  30.         System.out.println("\n----------Stock Reporting Menu----------");
  31.         System.out.println("MENU OPTIONS\n");
  32.         System.out.println("   1. List all tickers");
  33.         System.out.println("   2. Display most recent data for ticker");
  34.         System.out.println("   3. Display high and current close price for ticker");
  35.         System.out.println("   4. Display top five active tickers for date\n");
  36.         System.out.println("select number or type quit: ");
  37.     }
  38.  
  39.     static void getAllTickers() {
  40.         String query = "select Ticker from TickerHistory group by Ticker order by Ticker";
  41.  
  42.         try {
  43.             Connection connection = getConnection();
  44.             Statement statement = connection.createStatement();
  45.             ResultSet rs = statement.executeQuery(query);
  46.             rs = statement.executeQuery(query);
  47.             while (rs.next()) {
  48.                 String ticker = rs.getString("Ticker");
  49.                 System.out.print(ticker + ", ");
  50.             }
  51.             rs.close();
  52.             statement.close();
  53.             connection.close();
  54.             rs = null;
  55.             statement = null;
  56.             connection = null;
  57.         } catch (SQLException e) {
  58.             System.out.println("SQLException: " + e.getMessage());
  59.             System.out.println("SQLState: " + e.getSQLState());
  60.             System.out.println("VendorError: " + e.getErrorCode());
  61.         } catch (NumberFormatException e) {
  62.         }
  63.     }
  64.  
  65.     static void getOption2(Scanner keyboard) {
  66.         System.out.println("Please enter a valid Ticker");
  67.         String tickerInput = keyboard.nextLine();
  68.         String query = "select * from TickerHistory where HistoryDate = '2017-03-10' and Ticker = '" + tickerInput
  69.                 + "'";
  70.  
  71.         try {
  72.             Connection connection = getConnection();
  73.             Statement statement = connection.createStatement();
  74.             ResultSet rs = statement.executeQuery(query);
  75.             rs = statement.executeQuery(query);
  76.             while (rs.next()) {
  77.                 String ticker = rs.getString("Ticker");
  78.                 String companyName = rs.getString("companyName");
  79.                 int volum = rs.getInt("Volume");
  80.                 String historyDate = rs.getString("HistoryDate");
  81.                 double openPrice = rs.getDouble("OpenPrice");
  82.                 double highPrice = rs.getDouble("HighPrice");
  83.                 double lowPrice = rs.getDouble("LowPrice");
  84.                 double closePrice = rs.getDouble("ClosePrice");
  85.                 DecimalFormat dff = new DecimalFormat("#,###");
  86.                 System.out.println(ticker + "\t" + companyName + "\t" + "Volume: " + dff.format(volum));
  87.                 System.out.println("\tHistory Date: " + historyDate);
  88.                 System.out.println("\tOpen:   " + openPrice);
  89.                 System.out.println("\tHigh:   " + highPrice);
  90.                 System.out.println("\tLow:    " + lowPrice);
  91.                 System.out.println("\tClose:  " + closePrice);
  92.             }
  93.             rs.close();
  94.             statement.close();
  95.             connection.close();
  96.             rs = null;
  97.             statement = null;
  98.             connection = null;
  99.         } catch (SQLException e) {
  100.             System.out.println("SQLException: " + e.getMessage());
  101.             System.out.println("SQLState: " + e.getSQLState());
  102.             System.out.println("VendorError: " + e.getErrorCode());
  103.         } catch (NumberFormatException e) {
  104.         }
  105.     }
  106.  
  107.     static void getOption3(Scanner keyboard) {
  108.         System.out.println("Please enter a valid Ticker");
  109.         String tickerInput = keyboard.nextLine();
  110.  
  111.         try {
  112.             Connection connection = getConnection();
  113.             Statement statement = connection.createStatement();
  114.             String query = "select * from TickerHistory where Ticker = '" + tickerInput + "'"
  115.                     + " order by ClosePrice desc limit 1";
  116.             ResultSet rs = statement.executeQuery(query);
  117.             double result1 = 0, result2 = 0;
  118.             while (rs.next()) {
  119.                 Double closePrice = rs.getDouble("ClosePrice");
  120.                 String historyDate = rs.getString("HistoryDate");
  121.                 result1 = closePrice;
  122.                 System.out.println(historyDate + "\t" + closePrice);
  123.             }
  124.             query = "select * from TickerHistory where Ticker = '" + tickerInput + "'"
  125.                     + " order by HistoryDate desc limit 1 ";
  126.             rs = statement.executeQuery(query);
  127.  
  128.             while (rs.next()) {
  129.                 Double closePrice = rs.getDouble("ClosePrice");
  130.                 result2 = closePrice;
  131.                 Double differenceA = ((result2 - result1) / result1) * 100;
  132.                 String historyDate = rs.getString("HistoryDate");
  133.                 System.out.println(historyDate + "\t" + closePrice);
  134.                 System.out.printf("Perfect Difference is %.2f ", differenceA);
  135.             }
  136.  
  137.             rs.close();
  138.             statement.close();
  139.             connection.close();
  140.             rs = null;
  141.             statement = null;
  142.             connection = null;
  143.         } catch (SQLException e) {
  144.             System.out.println("SQLException: " + e.getMessage());
  145.             System.out.println("SQLState: " + e.getSQLState());
  146.             System.out.println("VendorError: " + e.getErrorCode());
  147.         } catch (NumberFormatException e) {
  148.         }
  149.     }
  150.  
  151.     static void getOption4(Scanner keyboard) {
  152.         System.out.print("Please enter a valid date (yyyy-mm-dd): ");
  153.         String tickerInput = keyboard.nextLine();
  154.         try {
  155.             Connection connection = getConnection();
  156.             Statement statement = connection.createStatement();
  157.             String query = "select * from TickerHistory where HistoryDate = '" + tickerInput + "'"
  158.                     + " order by Volume desc limit 5";
  159.             ResultSet rSet = statement.executeQuery(query);
  160.             while (rSet.next()) {
  161.                 Double volume = rSet.getDouble("Volume");
  162.                 String ticker = rSet.getString("Ticker");
  163.                 DecimalFormat dff = new DecimalFormat("#,###");
  164.  
  165.                 System.out.println("Ticker: " + ticker + "\t" + dff.format(volume));
  166.             }
  167.             rSet.close();
  168.             statement.close();
  169.             connection.close();
  170.             rSet = null;
  171.             statement = null;
  172.             connection = null;
  173.  
  174.         } catch (SQLException e) {
  175.             System.out.println("SQLException: " + e.getMessage());
  176.             System.out.println("SQLState: " + e.getSQLState());
  177.             System.out.println("VendorError: " + e.getErrorCode());
  178.         } catch (NumberFormatException e) {
  179.         }
  180.     }
  181.  
  182.     public static void main(String[] args) {
  183.         boolean isFound = true;
  184.  
  185.         while (isFound) {
  186.             reportMenu();
  187.             Scanner keyboard = new Scanner(System.in);
  188.             String input = keyboard.nextLine();
  189.             switch (input) {
  190.             case "1":
  191.                 getAllTickers();
  192.                 break;
  193.             case "2":
  194.                 getOption2(keyboard);
  195.                 break;
  196.             case "3":
  197.                 getOption3(keyboard);
  198.                 break;
  199.             case "4":
  200.                 getOption4(keyboard);
  201.                 break;
  202.  
  203.             case "quit":
  204.                 System.out.println("Application is quiting");
  205.                 isFound = false;
  206.                 break;
  207.             default:
  208.                 System.out.println("\nYou entered an invalid option\n");
  209.                 break;
  210.             }
  211.  
  212.            
  213.         }
  214.  
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement