Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.00 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.Date;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.text.NumberFormat;
  8. import java.util.Scanner;
  9.  
  10. public class Database_Reader {
  11.  
  12.     private static int EnterSelection(Scanner scanner) {
  13.  
  14.         System.out.println("MENU OPTIONS:");
  15.         System.out.println("");
  16.         System.out.println("    1. List all tickers");
  17.         System.out.println("    2. Display most recent data for ticker");
  18.         System.out.println("    3. Display high and current close price for ticker");
  19.         System.out.println("    4. Display top five active tickers for date");
  20.         System.out.println("");
  21.  
  22.         System.out.println("Select number or type quit: ");
  23.         int userResponse = scanner.nextInt();
  24.  
  25.         return userResponse;
  26.     }
  27.  
  28.     static Connection getConnection() {
  29.  
  30.         Connection connection = null;
  31.  
  32.         try {
  33.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  34.             connection = DriverManager.getConnection("jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  35.                     + "user=davidlno_student&password=ez24get!&allowMultiQueries=true");
  36.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  37.             e.printStackTrace();
  38.         } catch (SQLException e) {
  39.             // TODO Auto-generated catch block
  40.             System.out.println("SQLException: " + e.getMessage());
  41.             System.out.println("SQLState: " + e.getSQLState());
  42.             System.out.println("VendorError: " + e.getErrorCode());
  43.         }
  44.  
  45.         return connection;
  46.     }
  47.  
  48.     static Connection getConnectionII() {
  49.  
  50.         Connection connection = null;
  51.  
  52.         try {
  53.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  54.             connection = DriverManager.getConnection("jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  55.                     + "user=davidlno_student&password=ez24get!&allowMultiQueries=true");
  56.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  57.             e.printStackTrace();
  58.         } catch (SQLException e) {
  59.             // TODO Auto-generated catch block
  60.             System.out.println("SQLException: " + e.getMessage());
  61.             System.out.println("SQLState: " + e.getSQLState());
  62.             System.out.println("VendorError: " + e.getErrorCode());
  63.         }
  64.  
  65.         return connection;
  66.     }
  67.  
  68.     static void getAllTickers() {
  69.         Connection connection = getConnection();
  70.         String query = "SELECT GROUP_CONCAT(DISTINCT Ticker SEPARATOR ', ') as Ticker FROM TickerHistory";
  71.  
  72.         try {
  73.             Statement statement = connection.createStatement();
  74.             ResultSet rs = statement.executeQuery(query);
  75.  
  76.             while (rs.next()) {
  77.  
  78.                 String ticker = rs.getString("Ticker");
  79.                 System.out.print(ticker);
  80.  
  81.             }
  82.             System.out.println("");
  83.  
  84.             rs.close();
  85.             statement.close();
  86.             connection.close();
  87.  
  88.             rs = null;
  89.             statement = null;
  90.             connection = null;
  91.  
  92.         } catch (SQLException e) {
  93.             System.out.println("SQLException: " + e.getMessage());
  94.             System.out.println("SQLState: " + e.getSQLState());
  95.             System.out.println("VendorError: " + e.getErrorCode());
  96.         }
  97.  
  98.     }
  99.  
  100.     public static void EnterTicker() {
  101.         Scanner inputTicker = new Scanner(System.in);
  102.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  103.         NumberFormat formatterII = NumberFormat.getNumberInstance();
  104.  
  105.         System.out.println("Please enter ticker: ");
  106.         String userResponse = inputTicker.nextLine();
  107.  
  108.         Connection connection = getConnection();
  109.         String query = "select CompanyName, HistoryDate, OpenPrice, ClosePrice, Volume from TickerHistory where Ticker ='"
  110.                 + userResponse + "' order by HistoryDate desc limit 1";
  111.         try {
  112.             Statement statement = connection.createStatement();
  113.             ResultSet rs = statement.executeQuery(query);
  114.  
  115.             while (rs.next()) {
  116.                 String companyName = rs.getString("CompanyName");
  117.                 Date historyDate = rs.getDate("HistoryDate");
  118.                 double openPrice = rs.getDouble("OpenPrice");
  119.                 double closePrice = rs.getDouble("ClosePrice");
  120.                 int volume = rs.getInt("Volume");
  121.  
  122.                 System.out.println(companyName + "  " + historyDate + "  " + (formatter.format(openPrice)) + "  "
  123.                         + (formatter.format(closePrice)) + " " + "  " + (formatterII.format(volume)));
  124.  
  125.             }
  126.             System.out.println("");
  127.             rs.close();
  128.             statement.close();
  129.             connection.close();
  130.  
  131.             rs = null;
  132.             statement = null;
  133.             connection = null;
  134.         } catch (SQLException e) {
  135.             System.out.println("SQLException: " + e.getMessage());
  136.             System.out.println("SQLState: " + e.getSQLState());
  137.             System.out.println("VendorError: " + e.getErrorCode());
  138.         }
  139.  
  140.     }
  141.  
  142.     public static void EnterTickerII() {
  143.         Scanner inputTicker = new Scanner(System.in);
  144.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  145.         NumberFormat formatterII = NumberFormat.getNumberInstance();
  146.  
  147.         System.out.println("Please enter ticker: ");
  148.         String userResponse = inputTicker.nextLine();
  149.  
  150.         Connection connection = getConnection();
  151.         String query = "SELECT HistoryDate, ClosePrice, ClosePrice FROM TickerHistory WHERE Ticker ='" + userResponse
  152.                 + "' order by ClosePrice desc limit 1";
  153.  
  154.         Connection connectionII = getConnectionII();
  155.         String queryII = "SELECT ClosePrice as ClosePriceII FROM TickerHistory WHERE Ticker ='" + userResponse
  156.                 + "' order by HistoryDate desc limit 1";
  157.  
  158.         try {
  159.             Statement statement = connection.createStatement();
  160.             Statement statementII = connectionII.createStatement();
  161.  
  162.             ResultSet rs = statement.executeQuery(query);
  163.             ResultSet rsII = statementII.executeQuery(queryII);
  164.  
  165.             while (rs.next() && rsII.next()) {
  166.                 Date historyDate = rs.getDate("HistoryDate");
  167.                 double closePrice = rs.getDouble("ClosePrice");
  168.                 double closePriceII = rsII.getDouble("ClosePriceII");
  169.                 double decrease = (closePriceII - closePrice);
  170.                 double differencePercentage = (decrease / closePrice) * 100;
  171.  
  172.                 System.out.println(historyDate + "  " + (formatter.format(closePrice)) + " "
  173.                         + (formatter.format(closePriceII)) + " " + differencePercentage);
  174.             }
  175.             System.out.println("");
  176.  
  177.             rs.close();
  178.             rsII.close();
  179.             statement.close();
  180.  
  181.             statementII.close();
  182.             connection.close();
  183.             connectionII.close();
  184.  
  185.             rs = null;
  186.             statement = null;
  187.             connection = null;
  188.  
  189.             rsII = null;
  190.             statementII = null;
  191.             connectionII = null;
  192.  
  193.         } catch (SQLException e) {
  194.             System.out.println("SQLException: " + e.getMessage());
  195.             System.out.println("SQLState: " + e.getSQLState());
  196.             System.out.println("VendorError: " + e.getErrorCode());
  197.         }
  198.  
  199.     }
  200.  
  201.     public static void EnterTickerIII() {
  202.         Scanner inputTicker = new Scanner(System.in);
  203.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  204.  
  205.         System.out.println("Please enter ticker: ");
  206.         String userResponse = inputTicker.nextLine();
  207.  
  208.         Connection connection = getConnection();
  209.         String query = "select CompanyName, Volume from TickerHistory where Ticker ='" + userResponse
  210.                 + "' order by Volume desc limit 5";
  211.  
  212.         try {
  213.             Statement statement = connection.createStatement();
  214.             ResultSet rs = statement.executeQuery(query);
  215.  
  216.             while (rs.next()) {
  217.                 String companyName = rs.getString("CompanyName");
  218.                 int volume = rs.getInt("Volume");
  219.                 System.out.println(companyName + "  " + volume);
  220.             }
  221.  
  222.             System.out.println("");
  223.             rs.close();
  224.             statement.close();
  225.             connection.close();
  226.  
  227.             rs = null;
  228.             statement = null;
  229.             connection = null;
  230.         } catch (SQLException e) {
  231.             System.out.println("SQLException: " + e.getMessage());
  232.             System.out.println("SQLState: " + e.getSQLState());
  233.             System.out.println("VendorError: " + e.getErrorCode());
  234.         }
  235.  
  236.     }
  237.  
  238.     public static void main(String[] args) {
  239.  
  240.         Scanner input = new Scanner(System.in);
  241.         boolean rerun = false;
  242.  
  243.         do {
  244.             System.out.println("");
  245.             int selection = EnterSelection(input);
  246.  
  247.             switch (selection) {
  248.  
  249.             case 1:
  250.                 getAllTickers();
  251.                 rerun = true;
  252.                 break;
  253.             case 2:
  254.                 EnterTicker();
  255.                 rerun = true;
  256.                 break;
  257.             case 3:
  258.                 EnterTickerII();
  259.                 rerun = true;
  260.                 break;
  261.             case 4:
  262.                 EnterTickerIII();
  263.                 rerun = true;
  264.                 break;
  265.             default:
  266.                 System.out.println("Invalid selection");
  267.                 rerun = true;
  268.                 break;
  269.             }
  270.  
  271.         } while (rerun = true);
  272.     }
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement