Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 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.  
  70.         Connection connection = getConnection();
  71.         String query = "select Ticker from TickerHistory group by Ticker order by Ticker";
  72.  
  73.         try {
  74.             Statement statement = connection.createStatement();
  75.             ResultSet rs = statement.executeQuery(query);
  76.             String separator = ", ";
  77.  
  78.             while (rs.next()) {
  79.                 String ticker = rs.getString("Ticker");
  80.                 System.out.print(ticker + separator);
  81.            
  82.             }
  83.             System.out.println("");
  84.            
  85.             rs.close();
  86.             statement.close();
  87.             connection.close();
  88.  
  89.             rs = null;
  90.             statement = null;
  91.             connection = null;
  92.  
  93.         } catch (SQLException e) {
  94.             System.out.println("SQLException: " + e.getMessage());
  95.             System.out.println("SQLState: " + e.getSQLState());
  96.             System.out.println("VendorError: " + e.getErrorCode());
  97.         }
  98.  
  99.     }
  100.  
  101.     private static String EnterWord(Scanner scanner) {
  102.         System.out.println("Please enter ticker: ");
  103.         String userResponse = scanner.nextLine();
  104.  
  105.         return userResponse;
  106.     }
  107.  
  108.     public static void EnterTicker() {
  109.         Scanner inputTicker = new Scanner(System.in);
  110.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  111.         NumberFormat formatterII = NumberFormat.getNumberInstance();
  112.  
  113.         System.out.println("Please enter ticker: ");
  114.         String userResponse = inputTicker.nextLine();
  115.  
  116.         Connection connection = getConnection();
  117.         String query = "select CompanyName, HistoryDate, OpenPrice, ClosePrice, Volume from TickerHistory where Ticker ='"
  118.                 + userResponse + "' order by HistoryDate desc limit 1";
  119.  
  120.         try {
  121.             Statement statement = connection.createStatement();
  122.             ResultSet rs = statement.executeQuery(query);
  123.  
  124.             while (rs.next()) {
  125.                 String companyName = rs.getString("CompanyName");
  126.                 Date historyDate = rs.getDate("HistoryDate");
  127.                 double openPrice = rs.getDouble("OpenPrice");
  128.                 double closePrice = rs.getDouble("ClosePrice");
  129.                 int volume = rs.getInt("Volume");
  130.  
  131.                 System.out.println(companyName + "  " + historyDate + "  " + (formatter.format(openPrice)) + "  "
  132.                         + (formatter.format(closePrice)) + " " + "  " + (formatterII.format(volume)));
  133.                
  134.             }
  135.             System.out.println("");
  136.             rs.close();
  137.             statement.close();
  138.             connection.close();
  139.  
  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.         }
  148.  
  149.     }
  150.  
  151.     public static void EnterTickerII() {
  152.         Scanner inputTicker = new Scanner(System.in);
  153.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  154.         NumberFormat formatterII = NumberFormat.getNumberInstance();
  155.  
  156.         System.out.println("Please enter ticker: ");
  157.         String userResponse = inputTicker.nextLine();
  158.  
  159.         Connection connection = getConnection();
  160.         String query = "SELECT HistoryDate, ClosePrice, ClosePrice FROM TickerHistory WHERE Ticker ='" + userResponse
  161.                 + "' order by ClosePrice desc limit 1";
  162.  
  163.         Connection connectionII = getConnectionII();
  164.         String queryII = "SELECT ClosePrice as ClosePriceII FROM TickerHistory WHERE Ticker ='" + userResponse
  165.                 + "' order by HistoryDate desc limit 1";
  166.  
  167.         try {
  168.             Statement statement = connection.createStatement();
  169.             Statement statementII = connectionII.createStatement();
  170.  
  171.             ResultSet rs = statement.executeQuery(query);
  172.             ResultSet rsII = statementII.executeQuery(queryII);
  173.  
  174.             while (rs.next() && rsII.next()) {
  175.                 Date historyDate = rs.getDate("HistoryDate");
  176.                 double closePrice = rs.getDouble("ClosePrice");
  177.                 double closePriceII = rsII.getDouble("ClosePriceII");
  178.  
  179.                 double decrease = (closePriceII - closePrice);
  180.                 double differencePercentage = (decrease / closePrice) * 100;
  181.  
  182.                 System.out.println(historyDate + "  " + (formatter.format(closePrice)) + " "
  183.                         + (formatter.format(closePriceII)) + " " + differencePercentage);
  184.                
  185.             }
  186.             System.out.println("");
  187.            
  188.             rs.close();
  189.             rsII.close();
  190.             statement.close();
  191.  
  192.             statementII.close();
  193.             connection.close();
  194.             connectionII.close();
  195.  
  196.             rs = null;
  197.             statement = null;
  198.             connection = null;
  199.  
  200.             rsII = null;
  201.             statementII = null;
  202.             connectionII = null;
  203.  
  204.         } catch (SQLException e) {
  205.             System.out.println("SQLException: " + e.getMessage());
  206.             System.out.println("SQLState: " + e.getSQLState());
  207.             System.out.println("VendorError: " + e.getErrorCode());
  208.         }
  209.  
  210.     }
  211.  
  212.     public static void EnterTickerIII() {
  213.         Scanner inputTicker = new Scanner(System.in);
  214.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  215.  
  216.         System.out.println("Please enter ticker: ");
  217.         String userResponse = inputTicker.nextLine();
  218.  
  219.         Connection connection = getConnection();
  220.         String query = "select CompanyName, Volume from TickerHistory where Ticker ='" + userResponse
  221.                 + "' order by Volume desc limit 5";
  222.  
  223.         try {
  224.             Statement statement = connection.createStatement();
  225.             ResultSet rs = statement.executeQuery(query);
  226.  
  227.             while (rs.next()) {
  228.                 String companyName = rs.getString("CompanyName");
  229.                 int volume = rs.getInt("Volume");
  230.  
  231.                 System.out.println(companyName + "  " + volume);
  232.             }
  233.             System.out.println("");
  234.             rs.close();
  235.             statement.close();
  236.             connection.close();
  237.  
  238.             rs = null;
  239.             statement = null;
  240.             connection = null;
  241.         } catch (SQLException e) {
  242.             System.out.println("SQLException: " + e.getMessage());
  243.             System.out.println("SQLState: " + e.getSQLState());
  244.             System.out.println("VendorError: " + e.getErrorCode());
  245.         }
  246.  
  247.     }
  248.  
  249.     public static void main(String[] args) {
  250.  
  251.         Scanner input = new Scanner(System.in);
  252.         boolean rerun = false;
  253.        
  254.         do {
  255.             System.out.println("");
  256.             int selection = EnterSelection(input);
  257.  
  258.             switch (selection) {
  259.  
  260.             case 1:
  261.                 getAllTickers();
  262.                 rerun = true;
  263.                 break;
  264.             case 2:
  265.                 EnterTicker();
  266.                 rerun = true;
  267.                 break;
  268.             case 3:
  269.                 EnterTickerII();
  270.                 rerun = true;
  271.                 break;
  272.             case 4:
  273.                 EnterTickerIII();
  274.                 rerun = true;
  275.                 break;
  276.             default:
  277.                 System.out.println("Invalid selection");
  278.                 rerun = true;
  279.                 break;
  280.             }
  281.  
  282.         } while (rerun = true);
  283.     }
  284.  
  285.     /*
  286.      * if (selection == 1) { getAllTickers(); } else if (selection == 2) {
  287.      * EnterTicker(); } else if (selection ==3){ EnterTickerII(); } else if
  288.      * (selection ==4){ EnterTickerIII(); }
  289.      */
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement