Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. /*
  2. * Second Assessed Coursework Database Systems 3
  3. * @author: Nelly Temelkova
  4. * @date 15/03/2016
  5. * GUID: 2091751t
  6. * */
  7.  
  8. import java.sql.DriverManager;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. public class Lev3152091751t {
  15.  
  16. static Connection connection = null;
  17. static Statement statement;
  18.  
  19. // Methods for Queries
  20. public static void method3A(){
  21.  
  22. try {
  23. int i=1;
  24.  
  25. //Fetch SQL statement for first query
  26. ResultSet result = statement.executeQuery("SELECT AVG(release.rating)"
  27. + "FROM release "
  28. + "WHERE release.rating >= 8;");
  29.  
  30. //Display results, loop around Resultset
  31. System.out.println(" Results for Method3A:");
  32. while(result.next()){
  33. System.out.println("[" + i + "] " +
  34. result.getString("AVG"));
  35. i++;
  36. }
  37. result.close();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42.  
  43. public static void method3B(){
  44. try {
  45. int i=1;
  46.  
  47. //Fetch SQL statement for second query
  48. ResultSet result=statement.executeQuery("SELECT member.name "
  49. + "FROM member "
  50. + "INNER JOIN memberof "
  51. + "ON member.mid = memberof.mid "
  52. + "AND (member.stillalive = 'Y') AND (memberof.endyear IS NULL) "
  53. + "GROUP BY member.name "
  54. + "HAVING (COUNT(DISTINCT memberof.bid) > 1) AND (COUNT( DISTINCT memberof.instrument) > 1);");
  55.  
  56. //Display results, loop around Resultset
  57. System.out.println(" Results for Method3b:");
  58. while(result.next()){
  59. System.out.println("[" + i + "] " +
  60. result.getString("name"));
  61. i++;
  62. }
  63. result.close();
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68.  
  69. public static void method3C(){
  70. try {
  71. int i=1;
  72.  
  73. //Fetch SQL statement for third query
  74. ResultSet result=statement.executeQuery("SELECT band.name, D.num_of_members "
  75. + "FROM( "
  76. + "SELECT bid, COUNT(DISTINCT memberof.mid) AS num_of_members "
  77. + "FROM memberof "
  78. + "WHERE memberof.endyear IS NULL "
  79. + "GROUP BY memberof.bid) AS D NATURAL JOIN band "
  80. + "WHERE D.num_of_members = (SELECT MAX(num) "
  81. + "FROM (SELECT COUNT(DISTINCT mid) AS num "
  82. + "FROM memberof"
  83. + " WHERE memberof.endyear IS NULL "
  84. + "GROUP BY bid) count); ");
  85.  
  86. //Display results, loop around Resultset
  87. System.out.println(" Results for Method3C:");
  88. while(result.next()){
  89. System.out.println("[" + i + "] " +
  90. result.getString("name") + " " + result.getShort("num_of_members"));
  91. i++;
  92. }
  93. result.close();
  94. } catch (SQLException e) {
  95. e.printStackTrace();
  96. }
  97.  
  98. }
  99.  
  100. public static void main(String[] argv) {
  101.  
  102. // load the JDBC driver
  103. try {
  104. Class.forName("org.postgresql.Driver");
  105. } catch (ClassNotFoundException e) {
  106. System.out.println("Could not find JDBC Driver");
  107. e.printStackTrace();
  108. return;
  109. } // try-catch exception
  110. // the driver is loaded...
  111.  
  112. System.out.println("PostgreSQL JDBC Driver found!");
  113.  
  114. // proceed with a database connection
  115. // connect to the yacata.dcs.gla.ac.uk server, on port:5432, with
  116. // username: lev3_15_username
  117. // password: username
  118. try {
  119. connection = DriverManager.getConnection("jdbc:postgresql://yacata.dcs.gla.ac.uk:5432/", "lev3_15_2091751t",
  120. "2091751t");
  121. } catch (SQLException e) {
  122. System.out.println("Connection Failed!");
  123. e.printStackTrace();
  124. return;
  125. } // try-catch exception
  126. // connection to the database is done!
  127.  
  128. if (connection != null) {
  129. try {
  130. statement= connection.createStatement();
  131. System.out.println("Controlling your database...");
  132. method3A();
  133. method3B();
  134. method3C();
  135.  
  136.  
  137. statement.close(); // close statement
  138. connection.close(); // do not forget to close the connection to the database!
  139. } catch (SQLException e) {
  140. e.printStackTrace();
  141. } // try-catch exception
  142. } else {
  143. System.out.println("Failed to establish connection!");
  144. } // if-else
  145. }// main
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement