Advertisement
Guest User

Untitled

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