Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.34 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.text.NumberFormat;
  7. import java.util.Scanner;
  8.  
  9. public class TableReader {
  10.  
  11.     static Connection getConnection() {
  12.         Connection connection = null;
  13.        
  14.         try {
  15.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  16.             connection = DriverManager.getConnection(
  17.                     "jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  18.                     + "user=davidlno_student&password=ez24get!");
  19.            
  20.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  21.             e.printStackTrace();
  22.         } catch (SQLException e) {
  23.             System.out.println("SQLException: " + e.getMessage());
  24.             System.out.println("SQLState: " + e.getMessage());
  25.             System.out.println("VendorError: " + e.getErrorCode());
  26.         }
  27.        
  28.         return connection;
  29.     }
  30.    
  31.     static String appMenu() {
  32.         String userSelection = "0";
  33.        
  34.             System.out.print("\n----------Stock Reporting Menu----------\n"
  35.                     + "\nMENU OPTIONS:\n"
  36.                     + "\n1. List all tickers\n"
  37.                     + "2. Display most recent data for ticker\n"
  38.                     + "3. Display high and current close price for ticker\n"
  39.                     + "4. Display top five active tickers for date\n"
  40.                     + "\nSelect number or type quit: ");
  41.            
  42.             Scanner input = new Scanner(System.in);
  43.             userSelection = input.nextLine();
  44.            
  45.             return userSelection;
  46.     }
  47.    
  48.     static void getAllTickers() {
  49.         Connection connection = getConnection();
  50.         String query = "select Ticker from TickerHistory group by Ticker order by Ticker";
  51.        
  52.         try {
  53.             Statement statement = connection.createStatement();
  54.             ResultSet rs = statement.executeQuery(query);
  55.            
  56.             System.out.print("\n");
  57.            
  58.             while(rs.next()) {
  59.                 String ticker = rs.getString("Ticker");
  60.                 System.out.print(ticker);
  61.                
  62.                 if(!rs.isLast()) {
  63.                     System.out.print(", ");
  64.                 }
  65.                 else {
  66.                     System.out.print("\n");
  67.                 }  
  68.             }
  69.            
  70.             rs.close();
  71.             statement.close();
  72.             connection.close();
  73.            
  74.             rs = null;
  75.             statement = null;
  76.             connection = null;
  77.            
  78.         } catch (SQLException e) {
  79.             System.out.println("SQLException: " + e.getMessage());
  80.             System.out.println("SQLState: " + e.getSQLState());
  81.             System.out.println("VendorError: " + e.getErrorCode());
  82.         }
  83.     }
  84.    
  85.     static void getRecentData() {
  86.         System.out.print("\nPlease enter valid ticker: ");
  87.         Scanner input = new Scanner(System.in);
  88.         String  getTicker = input.nextLine();
  89.        
  90.         Connection connection = getConnection();
  91.         String query = "select * from TickerHistory where Ticker = '" + getTicker
  92.                 + "' and HistoryDate = '2017-03-10'";
  93.        
  94.             try {
  95.                 Statement statement = connection.createStatement();
  96.                 ResultSet rs = statement.executeQuery(query);
  97.                
  98.                 System.out.print("\n");
  99.                
  100.                 if(rs.next()) {
  101.                     String ticker = rs.getString("Ticker");
  102.                     String companyName = rs.getString("CompanyName");
  103.                     String historyDate = rs.getString("HistoryDate");
  104.                     int volume = rs.getInt("Volume");
  105.                     String formattedVolume = NumberFormat.getInstance().format(volume);
  106.                     double openPrice = rs.getDouble("OpenPrice");
  107.                     double highPrice = rs.getDouble("HighPrice");
  108.                     double lowPrice = rs.getDouble("LowPrice");
  109.                     double closePrice = rs.getDouble("ClosePrice");
  110.                    
  111.                     System.out.println(ticker + "   " + companyName +  "    Volume: " + formattedVolume);
  112.                     System.out.println("    History Date:   " + historyDate);
  113.                     System.out.println("    Open:   " + openPrice);
  114.                     System.out.println("    High:   " + highPrice);
  115.                     System.out.println("    Low:    " + lowPrice);
  116.                     System.out.println("    Close:  " + closePrice);
  117.                 }
  118.                 else {
  119.                     System.out.print("There is no data matching your criteria.");
  120.                 }
  121.                
  122.                 rs.close();
  123.                 statement.close();
  124.                 connection.close();
  125.                
  126.                 rs = null;
  127.                 statement = null;
  128.                 connection = null;
  129.                
  130.             } catch (SQLException e) {
  131.                 System.out.println("SQLException: " + e.getMessage());
  132.                 System.out.println("SQLState: " + e.getSQLState());
  133.                 System.out.println("VendorError: " + e.getErrorCode());
  134.             }
  135.        
  136.     }
  137.    
  138.     static void highAndClose() {
  139.         System.out.print("\nPlease enter valid ticker: ");
  140.         Scanner input = new Scanner(System.in);
  141.         String getTicker = input.nextLine();
  142.        
  143.         Connection connection = getConnection();
  144.         String query1 = "select * from TickerHistory where Ticker = '" + getTicker
  145.                 + "' order by HighPrice desc limit 1";
  146.         String query2 = "select * from TickerHistory where Ticker = '" + getTicker
  147.                 + "' and HistoryDate = '2017-03-10'";
  148.        
  149.         try {
  150.             Statement statement = connection.createStatement();
  151.             ResultSet rs1 = statement.executeQuery(query1);
  152.            
  153.             System.out.print("\n");
  154.            
  155.             if(rs1.next()) {
  156.                 String highDate = rs1.getString("HistoryDate");
  157.                 double highPrice = rs1.getDouble("HighPrice");
  158.                
  159.                 System.out.println(highDate + " " + highPrice);
  160.             }
  161.             else {
  162.                 System.out.println("There is no data matching your criteria.");
  163.             }
  164.            
  165.             ResultSet rs2 = statement.executeQuery(query2);
  166.            
  167.             while(rs2.next()) {
  168.                 String currentDate = rs2.getString("HistoryDate");
  169.                 double currentClose = rs2.getDouble("ClosePrice");
  170.                
  171.                 System.out.println(currentDate + "  " + currentClose);
  172.                
  173.             }
  174.            
  175.             rs2.close();
  176.             rs1.close();
  177.             statement.close();
  178.             connection.close();
  179.            
  180.             rs2 = null;
  181.             rs1 = null;
  182.             statement = null;
  183.             connection = null;
  184.    
  185.         } catch (SQLException e) {
  186.             System.out.println("SQLException: " + e.getMessage());
  187.             System.out.println("SQLState: " + e.getSQLState());
  188.             System.out.println("VendorError: " + e.getErrorCode());
  189.         }
  190.     }
  191.    
  192.     static void getTopFive(){
  193.         System.out.print("\nPlease enter valid date (yyyy-mm-dd): ");
  194.         Scanner input = new Scanner(System.in);
  195.         String getDate = input.nextLine();
  196.        
  197.         System.out.print("\n");
  198.        
  199.         Connection connection = getConnection();
  200.         String query = "select * from TickerHistory where HistoryDate = '" + getDate +"' order by Volume desc limit 5";
  201.        
  202.         try {
  203.             Statement statement = connection.createStatement();
  204.             ResultSet rs1 = statement.executeQuery(query);
  205.            
  206.             while(rs1.next()) {
  207.                 String ticker = rs1.getString("Ticker");
  208.                 int volume = rs1.getInt("Volume");
  209.                 String formattedVolume = NumberFormat.getInstance().format(volume);
  210.                        
  211.                 System.out.println("Ticker: " + ticker + "  " + formattedVolume);
  212.             }
  213.            
  214.             ResultSet rs2 = statement.executeQuery(query);
  215.            
  216.             if(!rs2.next()) {
  217.                 System.out.println("There is no data matching your criteria.");
  218.             }
  219.            
  220.             rs2.close();
  221.             rs1.close();
  222.             statement.close();
  223.             connection.close();
  224.            
  225.             rs2 = null;
  226.             rs1 = null;
  227.             statement = null;
  228.             connection = null;
  229.            
  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.     public static void main(String[] args) {
  237.        
  238.         String userSelection = appMenu();
  239.        
  240.         while(!userSelection.equals("quit")) {
  241.    
  242.             if(userSelection.equals("1")) {
  243.                 getAllTickers();
  244.             }
  245.             else if(userSelection.equals("2")) {
  246.                 getRecentData();
  247.             }
  248.             else if(userSelection.equals("3")) {
  249.                 highAndClose();
  250.             }
  251.             else if(userSelection.equals("4")) {
  252.                 getTopFive();
  253.             }
  254.            
  255.             userSelection = appMenu();
  256.         }
  257.        
  258.         System.out.print("\nYou have exited from the menu.");
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement