Advertisement
Guest User

Project issue

a guest
Apr 29th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 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 TableReader {
  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?" + "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.getSQLState());
  23. System.out.println("VendorError: " + e.getErrorCode());
  24. e.printStackTrace();
  25. }
  26.  
  27. return connection;
  28. }
  29.  
  30. static void getAllTickers() {
  31. Connection connection = getConnection();
  32. String query = "select ticker from TickerHistory group by ticker order by ticker";
  33.  
  34. try {
  35. Statement statement = connection.createStatement();
  36. ResultSet rs = statement.executeQuery(query);
  37.  
  38. while (rs.next()) {
  39.  
  40. String ticker = rs.getString("Ticker");
  41. System.out.print(ticker + ", ");
  42. }
  43.  
  44. rs.close();
  45. statement.close();
  46. connection.close();
  47.  
  48. rs = null;
  49. statement = null;
  50. connection = null;
  51.  
  52. } catch (SQLException e) {
  53. System.out.println("SQLException: " + e.getMessage());
  54. System.out.println("SQLState: " + e.getSQLState());
  55. System.out.println("VendorError: " + e.getErrorCode());
  56.  
  57. }
  58. }
  59.  
  60. static void recentData() {
  61.  
  62. String line;
  63. Scanner scann = new Scanner(System.in);
  64. System.out.println("Enter a valid ticker: ");
  65. line = scann.nextLine();
  66.  
  67. Connection connection = getConnection();
  68. String query = "select * from TickerHistory where Ticker = 'line'";
  69.  
  70. try {
  71. Statement statement = connection.createStatement();
  72. ResultSet rs = statement.executeQuery(query);
  73.  
  74. int counter = 0;
  75. while (rs.next()) {
  76. String ticker = rs.getString("Ticker");
  77. String companyName = rs.getString("CompanyName");
  78. int volume = rs.getInt("Volume");
  79. double openPrice = rs.getDouble("OpenPrice");
  80. double highPrice = rs.getDouble("HighPrice");
  81. double lowPrice = rs.getDouble("LowPrice");
  82. double closePrice = rs.getDouble("ClosePrice");
  83.  
  84. counter++;
  85. System.out.println(counter + " " + ticker + " " + companyName + " Volume: " + volume);
  86. System.out.println("Open: " + openPrice);
  87. System.out.println("High: " + highPrice);
  88. System.out.println("Low: " + lowPrice);
  89. System.out.println("Close: " + closePrice);
  90. }
  91. scann.close();
  92. rs.close();
  93. statement.close();
  94. connection.close();
  95.  
  96. rs = null;
  97. statement = null;
  98. connection = null;
  99.  
  100. } catch (SQLException e) {
  101. System.out.println("SQLException: " + e.getMessage());
  102. System.out.println("SQLState: " + e.getSQLState());
  103. System.out.println("VendorError: " + e.getErrorCode());
  104.  
  105. }
  106. }
  107.  
  108. static void closePrice() {
  109. String line;
  110. Scanner scann = new Scanner(System.in);
  111. System.out.println("Enter a valid ticker: ");
  112. line = scann.nextLine();
  113.  
  114. Connection connection = getConnection();
  115. String query = "select HistoryDate and Ticker and HighPrice and ClosePrice from TickerHistory order by HighPrice where Ticker = 'line'";
  116.  
  117. try {
  118. Statement statement = connection.createStatement();
  119. ResultSet rs = statement.executeQuery(query);
  120.  
  121. while (rs.next()) {
  122. for (int i = 1; i > 1; i++) {
  123. double high = rs.getDouble("HighPrice");
  124. double close = rs.getDouble("ClosePrice");
  125. String date = rs.getString("HistoryDate");
  126. System.out.println("High Price: " + high);
  127. System.out.println("2017 03 10 " + close);
  128. }
  129. }
  130.  
  131. rs.close();
  132. statement.close();
  133. connection.close();
  134.  
  135. rs = null;
  136. statement = null;
  137. connection = null;
  138.  
  139. } catch (SQLException e) {
  140. System.out.println("SQLException: " + e.getMessage());
  141. System.out.println("SQLState: " + e.getSQLState());
  142. System.out.println("VendorError: " + e.getErrorCode());
  143.  
  144. }
  145. }
  146.  
  147. static void currentDate() {
  148. String date;
  149. Scanner scan = new Scanner(System.in);
  150. System.out.println("Enter a valid Date (yyyy-mm-dd): ");
  151. date = scan.nextLine();
  152.  
  153. Connection connection = getConnection();
  154. String query = "select Ticker and Volume from TickerHistory where HistoryDate = 'date' order by Volume desc limit 5";
  155.  
  156. try {
  157. Statement statement = connection.createStatement();
  158. ResultSet rs = statement.executeQuery(query);
  159.  
  160. while (rs.next()) {
  161.  
  162. String ticker = rs.getString("Ticker");
  163. int volume = rs.getInt("Volume");
  164. System.out.print(ticker + " " + volume);
  165. }
  166.  
  167. rs.close();
  168. statement.close();
  169. connection.close();
  170.  
  171. rs = null;
  172. statement = null;
  173. connection = null;
  174.  
  175. } catch (SQLException e) {
  176. System.out.println("SQLException: " + e.getMessage());
  177. System.out.println("SQLState: " + e.getSQLState());
  178. System.out.println("VendorError: " + e.getErrorCode());
  179.  
  180. }
  181. }
  182.  
  183. public static void main(String[] args) {
  184.  
  185. String line = "";
  186. Scanner scan = new Scanner(System.in);
  187. Scanner scanin = new Scanner(System.in);
  188. do {
  189. System.out.println("");
  190. System.out.println("");
  191. System.out.println("----------Stock Reporting Menu--------------");
  192. System.out.println(" ");
  193. System.out.println("");
  194. System.out.println("");
  195. System.out.println("MENU OPTIONS:");
  196. System.out.println("1. List all tickers");
  197. System.out.println("2. Display most recent data for ticker");
  198. System.out.println("3. Display high and current close price for ticker");
  199. System.out.println("4. Display top five active tickers for date");
  200. System.out.println("");
  201. System.out.print("Select number or type quit:");
  202.  
  203. line = scanin.nextLine();
  204.  
  205. if (line.equals("1")) {
  206. getAllTickers();
  207. } else if (line.equals("2")) {
  208. recentData();
  209. } else if (line.equals("3")) {
  210. closePrice();
  211. } else if (line.equals("4")) {
  212. currentDate();
  213. }
  214.  
  215. } while (!line.equalsIgnoreCase("quit"));
  216. scanin.close();
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement