Advertisement
Guest User

prog3

a guest
Apr 1st, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*; // For access to the SQL interaction methods
  3. import java.util.Scanner;
  4.  
  5. public class Prog3 {
  6. public static void main(String[] args) {
  7.  
  8. final String oracleURL = // Magic lectura -> aloe access spell
  9. "jdbc:oracle:thin:@aloe.cs.arizona.edu:1521:oracle";
  10.  
  11. String username = null, // Oracle DBMS username
  12. password = null; // Oracle DBMS password
  13.  
  14. Statement stmt = null;
  15.  
  16. if (args.length == 2) { // get username/password from cmd line args
  17. username = args[0];
  18. password = args[1];
  19. } else {
  20. System.out.println("\nUsage: java JDBC <username> <password>\n"
  21. + " where <username> is your Oracle DBMS" + " username,\n and <password> is your Oracle"
  22. + " password (not your system password).\n");
  23. System.exit(-1);
  24. }
  25.  
  26. // load the (Oracle) JDBC driver by initializing its base
  27. // class, 'oracle.jdbc.OracleDriver'.
  28.  
  29. try {
  30.  
  31. Class.forName("oracle.jdbc.OracleDriver");
  32.  
  33. } catch (ClassNotFoundException e) {
  34.  
  35. System.err.println("*** ClassNotFoundException: " + "Error loading Oracle JDBC driver. \n"
  36. + "\tPerhaps the driver is not on the Classpath?");
  37. System.exit(-1);
  38.  
  39. }
  40.  
  41. // make and return a database connection to the user's
  42. // Oracle database
  43.  
  44. Connection dbconn = null;
  45.  
  46. try {
  47. dbconn = DriverManager.getConnection(oracleURL, username, password);
  48.  
  49. } catch (SQLException e) {
  50.  
  51. System.err.println("*** SQLException: " + "Could not open JDBC connection.");
  52. System.err.println("\tMessage: " + e.getMessage());
  53. System.err.println("\tSQLState: " + e.getSQLState());
  54. System.err.println("\tErrorCode: " + e.getErrorCode());
  55. System.exit(-1);
  56.  
  57. }
  58.  
  59. // PRINT QUERY OPTIONS FOR USER
  60.  
  61. System.out.println("Queries: ");
  62. System.out.println(
  63. "\t (1) For each of the four years, what are the top five longest road segments, as measured by the distance between BMP and EMP values?");
  64. System.out.println(
  65. "\t (2) For two years provided by the user, which road segments (identified by their location ids) were added and which were removed between those years?");
  66. System.out.println(
  67. "\t (3) For a year and a route given by the user, which TCSMP is closest to the midpoint of the road segment?");
  68. System.out.println("\t (4) TBD");
  69.  
  70. Scanner scanner = new Scanner(System.in);
  71.  
  72. System.out.println("Enter the number of the query you would like to execute: ");
  73. String option = scanner.nextLine();
  74.  
  75. if (option.equals("1")) {
  76. String query2012 = "select CNTLOCID, Route, abs(EMP - BMP) as Dist from hheavlin.ADOT_2012 where emp is not null and bmp is not null order by abs(EMP - BMP) desc";
  77. ResultSet result2012 = execute_query(dbconn, query2012);
  78. System.out.println("2012: ");
  79. processQuery1Output(result2012);
  80. System.out.println();
  81.  
  82. try {
  83. result2012.close();
  84. } catch (SQLException e) {
  85. e.printStackTrace();
  86. }
  87.  
  88. String query2013 = "select CNTLOCID, Route, abs(EMP - BMP) as Dist from hheavlin.ADOT_2013 where emp is not null and bmp is not null order by abs(EMP - BMP) desc";
  89. ResultSet result2013 = execute_query(dbconn, query2013);
  90. System.out.println("2013: ");
  91. processQuery1Output(result2013);
  92. System.out.println();
  93.  
  94. try {
  95. result2013.close();
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99.  
  100. String query2014 = "select CNTLOCID, Route, abs(EMP - BMP) as Dist from hheavlin.ADOT_2014 where emp is not null and bmp is not null order by abs(EMP - BMP) desc";
  101. ResultSet result2014 = execute_query(dbconn, query2014);
  102. System.out.println("2014: ");
  103. processQuery1Output(result2014);
  104. System.out.println();
  105.  
  106. try {
  107. result2014.close();
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111.  
  112. String query2015 = "select CNTLOCID, Route, abs(EMP - BMP) as Dist from hheavlin.ADOT_2015 where emp is not null and bmp is not null order by abs(EMP - BMP) desc";
  113. ResultSet result2015 = execute_query(dbconn, query2015);
  114. System.out.println("2015: ");
  115. processQuery1Output(result2015);
  116. System.out.println();
  117.  
  118. try {
  119. result2015.close();
  120. } catch (SQLException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. if (option.equals("2")) {
  126. System.out.println("Enter start year: ");
  127. String startYear = scanner.nextLine();
  128. System.out.println("Enter end year: ");
  129. String endYear = scanner.nextLine();
  130.  
  131. String queryStartYear = "select CNTLOCID, Route, Start_, End from hheavlin.ADOT_" + startYear;
  132. ResultSet resultStartYear = execute_query(dbconn, queryStartYear);
  133.  
  134. String queryEndYear = "select CNTLOCID, Route, Start_, End from hheavlin.ADOT_" + endYear;
  135. ResultSet resultEndYear = execute_query(dbconn, queryEndYear);
  136.  
  137. System.out.println("Added: ");
  138. processQuery2Output(resultEndYear, resultStartYear);
  139. System.out.println();
  140.  
  141. System.out.println("------------------------------------------------------");
  142.  
  143. System.out.println("Removed: ");
  144. processQuery2Output(resultStartYear, resultEndYear);
  145. System.out.println();
  146.  
  147. try {
  148. resultStartYear.close();
  149. resultEndYear.close();
  150. } catch (SQLException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154.  
  155. if (option.equals("3")) {
  156. System.out.println("Enter a year: ");
  157. String year = scanner.nextLine();
  158.  
  159. System.out.println("Enter a route: ");
  160. String route = scanner.nextLine();
  161.  
  162. String query = "";
  163. ResultSet result = execute_query(dbconn, query);
  164. }
  165.  
  166. if (option.equals("4")) {
  167. String query = "";
  168. ResultSet result = execute_query(dbconn, query);
  169. }
  170.  
  171. try {
  172. dbconn.close();
  173. } catch (SQLException e) {
  174. e.printStackTrace();
  175. }
  176. }
  177.  
  178. public static ResultSet execute_query(Connection dbconn, String query) {
  179. // Send the query to the DBMS, and get the results
  180. Statement stmt = null;
  181. ResultSet answer = null;
  182.  
  183. try {
  184. stmt = dbconn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  185. answer = stmt.executeQuery(query);
  186. // Shut down the connection to the DBMS.
  187.  
  188. } catch (SQLException e) {
  189. System.err.println("*** SQLException: " + "Could not fetch query results.");
  190. System.err.println("\tMessage: " + e.getMessage());
  191. System.err.println("\tSQLState: " + e.getSQLState());
  192. System.err.println("\tErrorCode: " + e.getErrorCode());
  193. System.exit(-1);
  194. }
  195. return answer;
  196. }
  197.  
  198. // prints out top 5 results in a given ResultSet
  199. public static void processQuery1Output(ResultSet result) {
  200. if (result != null) {
  201. try {
  202. int i = 0;
  203. while (result.next() && i < 5) {
  204. System.out.println(result.getInt("CNTLOCID") + "\t" + result.getString("Route") + "\t"
  205. + result.getFloat("Dist"));
  206. i++;
  207. }
  208.  
  209. float current = result.getFloat("Dist");
  210. while (result.next()) {
  211. if (Float.compare(current, result.getFloat("Dist")) == 0) {
  212. System.out.println(result.getInt("CNTLOCID") + "\t" + result.getString("Route") + "\t"
  213. + result.getFloat("Dist"));
  214. } else {
  215. break;
  216. }
  217. }
  218. } catch (SQLException e) {
  219. e.printStackTrace();
  220. }
  221. }
  222. }
  223.  
  224. public static void processQuery2Output(ResultSet result1, ResultSet result2) {
  225. try{
  226. if (result1 != null && result2 != null) {
  227.  
  228. while(result1.next()){
  229. boolean notFound = true;
  230. while (result2.next()) {
  231. if (result1.getInt("CNTLOCID") != result2.getInt("CNTLOCID")) {
  232. notFound = false;
  233. break;
  234. }
  235. }
  236. if (notFound == true) {
  237. System.out.println(result1.getInt("CNTLOCID") + "\t" + result1.getString("Route") + "\t"
  238. + result1.getString("Start_") + "\t" + result1.getString("End"));
  239. }
  240.  
  241. result2.beforeFirst();
  242. }
  243. }
  244. } catch (SQLException e) {
  245. e.printStackTrace();
  246. }
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement