Advertisement
Guest User

RR2

a guest
Apr 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7.  
  8. public class TableReader  {
  9.  
  10.     static Connection getConnection() {
  11.         Connection connection = null;
  12.  
  13.         try {
  14.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  15.             connection = DriverManager.getConnection(
  16.                     "jdbc:mysql://184.154.73.113/davidlno_StockHistory?" + "user=davidlno_student&password=ez24get!");
  17.  
  18.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  19.             e.printStackTrace();
  20.         } catch (SQLException e) {
  21.             System.out.println("SQLException: " + e.getMessage());
  22.             System.out.println("SQLState: " + e.getSQLState());
  23.             System.out.println("VendorError: " + e.getErrorCode());
  24.             e.printStackTrace();
  25.         }
  26.  
  27.         return connection;
  28.     }
  29.  
  30.     static void getAllTickers() {
  31.         Connection connection = getConnection();
  32.         String query = "select ticker from TickerHistory group by ticker order by ticker";
  33.  
  34.         try {
  35.             Statement statement = connection.createStatement();
  36.             ResultSet rs = statement.executeQuery(query);
  37.  
  38.             while (rs.next()) {
  39.  
  40.                 String ticker = rs.getString("Ticker");
  41.                 System.out.print(ticker + ", ");
  42.             }
  43.  
  44.             rs.close();
  45.             statement.close();
  46.             connection.close();
  47.  
  48.             rs = null;
  49.             statement = null;
  50.             connection = null;
  51.  
  52.         } catch (SQLException e) {
  53.             System.out.println("SQLException: " + e.getMessage());
  54.             System.out.println("SQLState: " + e.getSQLState());
  55.             System.out.println("VendorError: " + e.getErrorCode());
  56.  
  57.         }
  58.     }
  59.  
  60.     // Change your methods to pass the scanner you opened in main into the functions
  61.     //static void recentData() {
  62.     static void recentData(Scanner scann) {
  63.  
  64.         String line;
  65.        
  66.         // Now that we are passing the scanner in we don't need to define it any longer.
  67.         //Scanner scann = new Scanner(System.in);
  68.         System.out.println("Enter a valid ticker: ");
  69.         line = scann.nextLine();
  70.  
  71.         Connection connection = getConnection();
  72.         String query = "select * from TickerHistory where Ticker = '" + line + "' order by HistoryDate  desc limit 1";
  73.         //String query = "select * from TickerHistory where Ticker = 'line'";
  74.  
  75.         try {
  76.             Statement statement = connection.createStatement();
  77.             ResultSet rs = statement.executeQuery(query);
  78.  
  79.             int counter = 0;
  80.             while (rs.next()) {
  81.                 String ticker = rs.getString("Ticker");
  82.                 String companyName = rs.getString("CompanyName");
  83.                 int volume = rs.getInt("Volume");
  84.                 double openPrice = rs.getDouble("OpenPrice");
  85.                 double highPrice = rs.getDouble("HighPrice");
  86.                 double lowPrice = rs.getDouble("LowPrice");
  87.                 double closePrice = rs.getDouble("ClosePrice");
  88.  
  89.                 counter++;
  90.                 System.out.println(counter + "  " + ticker + "  " + companyName + "  Volume: " + volume);
  91.                 System.out.println("Open:   " + openPrice);
  92.                 System.out.println("High:   " + highPrice);
  93.                 System.out.println("Low:    " + lowPrice);
  94.                 System.out.println("Close:  " + closePrice);
  95.             }
  96.            
  97.             // Because we are passing the scanner into the method we can't close it--it needs to remain open until main() closes it.
  98.             //scann.close();
  99.             rs.close();
  100.             statement.close();
  101.             connection.close();
  102.  
  103.             rs = null;
  104.             statement = null;
  105.             connection = null;
  106.  
  107.         } catch (SQLException e) {
  108.             System.out.println("SQLException: " + e.getMessage());
  109.             System.out.println("SQLState: " + e.getSQLState());
  110.             System.out.println("VendorError: " + e.getErrorCode());
  111.  
  112.         }
  113.     }
  114.  
  115.     // Pass the scanner into the method.
  116.     static void closePrice(Scanner scann) {
  117.         String line;
  118.         //Scanner scann = new Scanner(System.in);
  119.         System.out.println("Enter a valid ticker: ");
  120.         line = scann.nextLine();
  121.  
  122.         Connection connection = getConnection();
  123.         String query = "select HistoryDate and Ticker and HighPrice and ClosePrice from TickerHistory order by HighPrice where Ticker = 'line'";
  124.  
  125.         try {
  126.             Statement statement = connection.createStatement();
  127.             ResultSet rs = statement.executeQuery(query);
  128.  
  129.             while (rs.next()) {
  130.                 for (int i = 1; i > 1; i++) {
  131.                     double high = rs.getDouble("HighPrice");
  132.                     double close = rs.getDouble("ClosePrice");
  133.                     String date = rs.getString("HistoryDate");
  134.                     System.out.println("High Price: " + high);
  135.                     System.out.println("2017 03 10  " + close);
  136.                 }
  137.             }
  138.  
  139.             rs.close();
  140.             statement.close();
  141.             connection.close();
  142.  
  143.             rs = null;
  144.             statement = null;
  145.             connection = null;
  146.  
  147.         } catch (SQLException e) {
  148.             System.out.println("SQLException: " + e.getMessage());
  149.             System.out.println("SQLState: " + e.getSQLState());
  150.             System.out.println("VendorError: " + e.getErrorCode());
  151.  
  152.         }
  153.     }
  154.  
  155.     // Pass the scanner into the method.
  156.     static void currentDate(Scanner scan) {
  157.         String date;
  158.         //Scanner scan = new Scanner(System.in);
  159.         System.out.println("Enter a valid Date (yyyy-mm-dd): ");
  160.         date = scan.nextLine();
  161.  
  162.         Connection connection = getConnection();
  163.         String query = "select Ticker and Volume from TickerHistory where HistoryDate = 'date' order by Volume desc limit 5";
  164.  
  165.         try {
  166.             Statement statement = connection.createStatement();
  167.             ResultSet rs = statement.executeQuery(query);
  168.  
  169.             while (rs.next()) {
  170.  
  171.                 String ticker = rs.getString("Ticker");
  172.                 int volume = rs.getInt("Volume");
  173.                 System.out.print(ticker + "     " + volume);
  174.             }
  175.  
  176.             rs.close();
  177.             statement.close();
  178.             connection.close();
  179.  
  180.             rs = null;
  181.             statement = null;
  182.             connection = null;
  183.  
  184.         } catch (SQLException e) {
  185.             System.out.println("SQLException: " + e.getMessage());
  186.             System.out.println("SQLState: " + e.getSQLState());
  187.             System.out.println("VendorError: " + e.getErrorCode());
  188.  
  189.         }
  190.     }
  191.  
  192.     public static void main(String[] args) {
  193.  
  194.         String line = "";
  195.         Scanner scan = new Scanner(System.in);
  196.        
  197.         // Get rid of this extra scanner.
  198.         //Scanner scanin = new Scanner(System.in);
  199.         do {
  200.             System.out.println("");
  201.             System.out.println("");
  202.             System.out.println("----------Stock Reporting Menu--------------");
  203.             System.out.println(" ");
  204.             System.out.println("");
  205.             System.out.println("");
  206.             System.out.println("MENU OPTIONS:");
  207.             System.out.println("1. List all tickers");
  208.             System.out.println("2. Display most recent data for ticker");
  209.             System.out.println("3. Display high and current close price for ticker");
  210.             System.out.println("4. Display top five active tickers for date");
  211.             System.out.println("");
  212.             System.out.print("Select number or type quit:");
  213.  
  214.             // Use your scan variable instead.
  215.             //line = scanin.nextLine();
  216.             line = scan.nextLine();
  217.  
  218.             // We modified your methods to accept the scanner that is being created and used here in main.
  219.             // So, the signatures of your calls below had to be modified to pass the scanner to each method with the exception of getAllTickers
  220.             // which doesn't prompt for any additional information.
  221.             if (line.equals("1")) {
  222.                 getAllTickers();
  223.             } else if (line.equals("2")) {
  224.                 recentData(scan);
  225.             } else if (line.equals("3")) {
  226.                 closePrice(scan);
  227.             } else if (line.equals("4")) {
  228.                 currentDate(scan);
  229.             }
  230.  
  231.         } while (!line.equalsIgnoreCase("quit"));
  232.        
  233.         // Close only scan
  234.         //scanin.close();
  235.         scan.close();
  236.     }
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement