Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1.  
  2. import java.sql.DriverManager; // DriverManager for establishing linkage to DB
  3. import java.sql.SQLException; // SQLException handles exception for DB classes
  4. import java.sql.Connection; // Connection establishes a logon to the DB
  5. import java.sql.Statement; // Holds and executes the sql statements
  6. import java.sql.ResultSet; // Holds the results of a query
  7. import java.util.Date;
  8.  
  9. public class Hw6Pr1
  10. {
  11.  
  12. /**
  13. * Method: main<br>
  14. * Responsibilities: runs sql queries and lists them in the way needed.
  15. *
  16. * @param args
  17. */
  18. public static void main(String[] args)
  19. {
  20. System.out.println("Starting Connection Process");
  21. String myConnStr; // holds the connection string
  22. String myUser; // holds the user name
  23. String myPasswd; // holds the user password
  24. String mySqlStmt; // holds sql statement to be run
  25. String prevGiven; //holds the previous given name from the loop
  26. String prevFamily; //holds the previous family name from the loop
  27. int count = 0;// holds the count of rows retrieved
  28.  
  29. /*
  30. * handling exceptions
  31. */
  32. try
  33. {
  34. //Link to the jdbc driver
  35.  
  36. System.out.println("Link this class to the jdbc driver");
  37. DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  38.  
  39. //connection to the database
  40. myConnStr = "jdbc:oracle:thin:@pythia.etsu.edu:1521:csdb";
  41. myUser = "studers";
  42. myPasswd = "rachelle";
  43. Connection myCon = null; //Build the connection object
  44. myCon = DriverManager.getConnection(myConnStr, myUser, myPasswd); //establish a connection via the jdbc driver
  45.  
  46.  
  47. Statement myStmt = null; //holds the value of the sql statement
  48. myStmt = myCon.createStatement(); // the connection association
  49. Statement statement = null;
  50. statement = myCon.createStatement();
  51.  
  52. ResultSet myReslts = null; // the result set
  53. mySqlStmt = "SELECT INSTRUCTOR.GIVEN_NAME, INSTRUCTOR.FAMILY_NAME, SECTION.COURSE, SECTION.TERM FROM INSTRUCTOR JOIN SECTION ON INSTRUCTOR.ID = SECTION.INSTRUCTOR;"; // the sql statement to run
  54.  
  55.  
  56.  
  57. myReslts = myStmt.executeQuery(mySqlStmt);
  58. // tell the jdbc connection to run the query
  59. // and put the results in my_results
  60.  
  61.  
  62. //looping through the results to extract the data needed
  63.  
  64. String givenName = myReslts.getString("GIVEN_NAME"); //holds the given name
  65. String familyName = myReslts.getString("FAMILY_NAME"); //holds the family name
  66. String course = myReslts.getString("COURSE"); //holds the course number
  67. String term = myReslts.getString("TERM");
  68. prevFamily = familyName;
  69. prevGiven = givenName;
  70. while (myReslts.next()) //ResultSet.next gets the next record,
  71. //it returns false if no more records exist.
  72. {
  73.  
  74. givenName = myReslts.getString("GIVEN_NAME");
  75. familyName = myReslts.getString("FAMILY_NAME");
  76. course = myReslts.getString("COURSE");
  77. term = myReslts.getString("TERM");
  78.  
  79. //counter to see the position of the loop
  80. count = count + 1;
  81. System.out.print("Instructor" + count + ":");
  82. while(givenName.equals(prevGiven)&& familyName.equals(prevFamily))
  83. {
  84. System.out.println(course + " " + term);
  85. }
  86. prevFamily = familyName;
  87. prevGiven = givenName;
  88. //Now use all these pieces to build up a nice output.
  89.  
  90.  
  91. } //end while
  92.  
  93. myReslts.close(); // close the result set
  94. myStmt.close(); // close the statement
  95. myCon.close(); // close the connection
  96.  
  97. } // end try
  98. catch (SQLException mySqlError)
  99. {
  100. //error handling
  101. System.out.println("An Error has occured, dumping error stack");
  102. mySqlError.printStackTrace();
  103. } //end catch
  104. } // end of main
  105. } // end of class Hw6Pr1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement