Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 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 TableReaderCopy {
  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?" +
  17.                             "user=davidlno_student&password=ez24get!");
  18.            
  19.            
  20.            
  21.                     } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  22.                         e.printStackTrace();
  23.                     } catch (SQLException e) {
  24.                         System.out.println("SQLException: " + e.getMessage());
  25.                         System.out.println("SQLState: " + e.getSQLState());
  26.                         System.out.println("VendorError: " + e.getErrorCode());    
  27.                
  28.         }
  29.        
  30.         return connection;
  31.        
  32.     }
  33.  
  34.     static void getAllTickers() {
  35.         Connection connection = getConnection();
  36.         String query = "select Ticker from TickerHistory group by Ticker order by Ticker";
  37.        
  38.         try {
  39.             Statement statement = connection.createStatement();
  40.             ResultSet rs = statement.executeQuery(query);
  41.            
  42.             while(rs.next()) {
  43.                 String ticker = rs.getString("Ticker");
  44.                 System.out.println(ticker);
  45.             }
  46.            
  47.             rs.close();
  48.             connection.close();
  49.            
  50.             rs = null;
  51.             statement = null;
  52.             connection = null;
  53.         } catch (SQLException e) {
  54.             System.out.println("SQLException: " + e.getMessage());
  55.             System.out.println("SQLState: " + e.getSQLState());
  56.             System.out.println("VendorError: " + e.getErrorCode());
  57.         }
  58.        
  59.     }
  60.     static void getExampleQueries() {
  61.         Connection connection = getConnection();
  62.         String query = "select * from TickerHistory where HistoryDate = '2017-03-10'";
  63.        
  64.         try {
  65.             Statement statement = connection.createStatement();
  66.             ResultSet rs = statement.executeQuery(query);
  67.            
  68.             int terms;
  69.            
  70.             Scanner in = new Scanner(System.in);
  71.             System.out.println("-----------Stock Reporting Options-------------");
  72.             System.out.println();
  73.             System.out.println("MENU OPTIONS");
  74.             System.out.println();
  75.             System.out.println("    1. List all ticker");
  76.             System.out.println("    2. Display most recent data for ticker");
  77.             System.out.println("    3. Display high and current close price for ticker");
  78.             System.out.println("    4. Display top five active tickers for date");
  79.             System.out.println();
  80.             System.out.println("Select number or type to quit: ");
  81.             terms = in.nextInt();
  82.            
  83.            
  84.             while(rs.next()) {
  85.                 String ticker = rs.getString("Ticker");
  86.                 if ( terms == 1);
  87.                 System.out.print(ticker + ", ");
  88.             }
  89.             System.out.println();
  90.            
  91.            
  92.             System.out.println("-----------Stock Reporting Options-------------");
  93.             System.out.println();
  94.             System.out.println("MENU OPTIONS");
  95.             System.out.println();
  96.             System.out.println("    1. List all ticker");
  97.             System.out.println("    2. Display most recent data for ticker");
  98.             System.out.println("    3. Display high and current close price for ticker");
  99.             System.out.println("    4. Display top five active tickers for date");
  100.             System.out.println();
  101.             System.out.println("Select number or type to quit: ");
  102.             terms = in.nextInt();  
  103.             System.out.println("Please enter a valid ticker: ");
  104.             terms = in.nextInt();
  105.            
  106.             while(rs.next()) {
  107.                 String ticker = rs.getString("Ticker");
  108.                 if ( terms == 2);
  109.                 System.out.print(ticker + ", ");
  110.                
  111.             }
  112.        
  113.                
  114.            
  115.            
  116.             rs.close();
  117.             connection.close();
  118.            
  119.             rs = null;
  120.             statement = null;
  121.             connection = null;}
  122.            
  123.          catch (SQLException e) {
  124.             System.out.println("SQLException: " + e.getMessage());
  125.             System.out.println("SQLState: " + e.getSQLState());
  126.             System.out.println("VendorError: " + e.getErrorCode());
  127.         }
  128.        
  129.     }
  130.    
  131.     public static void main(String[] args) {
  132.         //getAllTickers();
  133.         getExampleQueries();
  134.    
  135.  
  136.        
  137.        
  138.        
  139.  
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement