Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class TableReaderCopy {
- static Connection getConnection() {
- Connection connection = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection(
- "jdbc:mysql://184.154.73.113/davidlno_StockHistory?" +
- "user=davidlno_student&password=ez24get!");
- } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- return connection;
- }
- static void getAllTickers() {
- Connection connection = getConnection();
- String query = "select Ticker from TickerHistory group by Ticker order by Ticker";
- try {
- Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery(query);
- while(rs.next()) {
- String ticker = rs.getString("Ticker");
- System.out.println(ticker);
- }
- rs.close();
- connection.close();
- rs = null;
- statement = null;
- connection = null;
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- }
- static void getExampleQueries() {
- Connection connection = getConnection();
- String query = "select * from TickerHistory where HistoryDate = '2017-03-10'";
- try {
- Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery(query);
- int terms;
- Scanner in = new Scanner(System.in);
- System.out.println("-----------Stock Reporting Options-------------");
- System.out.println();
- System.out.println("MENU OPTIONS");
- System.out.println();
- System.out.println(" 1. List all ticker");
- System.out.println(" 2. Display most recent data for ticker");
- System.out.println(" 3. Display high and current close price for ticker");
- System.out.println(" 4. Display top five active tickers for date");
- System.out.println();
- System.out.println("Select number or type to quit: ");
- terms = in.nextInt();
- while(rs.next()) {
- String ticker = rs.getString("Ticker");
- if ( terms == 1);
- System.out.print(ticker + ", ");
- }
- System.out.println();
- System.out.println("-----------Stock Reporting Options-------------");
- System.out.println();
- System.out.println("MENU OPTIONS");
- System.out.println();
- System.out.println(" 1. List all ticker");
- System.out.println(" 2. Display most recent data for ticker");
- System.out.println(" 3. Display high and current close price for ticker");
- System.out.println(" 4. Display top five active tickers for date");
- System.out.println();
- System.out.println("Select number or type to quit: ");
- terms = in.nextInt();
- System.out.println("Please enter a valid ticker: ");
- terms = in.nextInt();
- while(rs.next()) {
- String ticker = rs.getString("Ticker");
- if ( terms == 2);
- System.out.print(ticker + ", ");
- }
- rs.close();
- connection.close();
- rs = null;
- statement = null;
- connection = null;}
- catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- }
- public static void main(String[] args) {
- //getAllTickers();
- getExampleQueries();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement