Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 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.  
  7. public class TableReader {
  8.  
  9.     static Connection getConnection() {
  10.         Connection connection = null;
  11.        
  12.         try {
  13.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  14.             connection = DriverManager.getConnection(
  15.                     "jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  16.                     + "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.getMessage());
  23.             System.out.println("VendorError: " + e.getErrorCode());
  24.         }
  25.        
  26.         return connection;
  27.     }
  28.    
  29.     static void getAllTickers() {
  30.         Connection connection = getConnection();
  31.         String query = "select Ticker from TickerHistory group by Ticker order by Ticker";
  32.        
  33.         try {
  34.             Statement statement = connection.createStatement();
  35.             ResultSet rs = statement.executeQuery(query);
  36.            
  37.             while(rs.next()) {
  38.                 String ticker = rs.getString("Ticker");
  39.                 System.out.print(ticker);
  40.             }
  41.            
  42.             rs.close();
  43.             statement.close();
  44.             connection.close();
  45.            
  46.             rs = null;
  47.             statement = null;
  48.             connection = null;
  49.            
  50.         } catch (SQLException e) {
  51.             System.out.println("SQLException: " + e.getMessage());
  52.             System.out.println("SQLState: " + e.getMessage());
  53.             System.out.println("VendorError: " + e.getErrorCode());
  54.         }
  55.        
  56.     }
  57.    
  58.     public static void main(String[] args) {
  59.         getAllTickers();
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement