Guest User

Untitled

a guest
Aug 3rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package javaapplication2;
  7. import java.sql.*;
  8. /**
  9. *
  10. * @author db2inst1
  11. */
  12. public class Praktikum2 {
  13. private Connection con;
  14. private PreparedStatement pstmt;
  15. private String query;
  16.  
  17. public static void main(String[] args) throws SQLException {
  18. try{
  19. Praktikum2 pra = new Praktikum2();
  20. //pra.aufgabe2();
  21. //pra.aufgabe2b();
  22. pra.aufgabe3();
  23. }
  24. catch(Exception ex){
  25. ex.printStackTrace();
  26. }
  27.  
  28.  
  29. }
  30.  
  31. public Praktikum2() throws ClassNotFoundException, SQLException {
  32. Class.forName("com.ibm.db2.jcc.DB2Driver");
  33. con = DriverManager.getConnection("jdbc:db2://localhost:50000/PUPS", "db2inst1", "hda");
  34. con.setAutoCommit(false);
  35. }
  36.  
  37. public void aufgabe2() throws SQLException{
  38. Long starttime = System.currentTimeMillis();
  39. //Aufgabe 1 a)
  40. query = "SELECT KNR, COUNT(KNR) FROM BESTELLUNG GROUP BY KNR ORDER BY COUNT(KNR) DESC";
  41. pstmt = con.prepareStatement(query);
  42. ResultSet rs = pstmt.executeQuery();
  43. con.commit();
  44.  
  45. while(rs.next());
  46.  
  47. //Aufgabe 1 b)
  48. query = "SELECT KNR, COUNT(KNR) FROM BESTELLUNG GROUP BY KNR ORDER BY COUNT(KNR) ASC";
  49. pstmt = con.prepareStatement(query);
  50. rs = pstmt.executeQuery();
  51. con.commit();
  52.  
  53. while(rs.next());
  54.  
  55. //Aufgabe 1 c)
  56. query = "SELECT PID, COUNT(PID) FROM BESTELLUNG GROUP BY PID ORDER BY COUNT(PID) DESC";
  57. pstmt = con.prepareStatement(query);
  58. rs = pstmt.executeQuery();
  59. con.commit();
  60.  
  61. while(rs.next());
  62.  
  63. //Aufgabe 1 d)
  64. query = "SELECT PID, COUNT(PID) FROM BESTELLUNG GROUP BY PID ORDER BY COUNT(PID) ASC";
  65. pstmt = con.prepareStatement(query);
  66. rs = pstmt.executeQuery();
  67. con.commit();
  68.  
  69. while(rs.next());
  70.  
  71. Long endtime = System.currentTimeMillis();
  72. Long totaltime = endtime - starttime;
  73. System.out.println("Zeit: " + totaltime.toString());
  74. }
  75.  
  76. public void aufgabe2b() throws SQLException{
  77. Long starttime = System.currentTimeMillis();
  78. int groesseBestellung = 100000;
  79. ResultSet rs;
  80.  
  81. //Aufgabe 1 a)
  82. query = "SELECT KNR, COUNT(KNR) FROM BESTELLUNG WHERE BID > ? AND BID < ? GROUP BY KNR ORDER BY COUNT(KNR) DESC";
  83. pstmt = con.prepareStatement(query);
  84. for(int i=0;i<(1000000/groesseBestellung);i++){
  85. pstmt.setInt(1, i*groesseBestellung);
  86. pstmt.setInt(2, (i+1)*groesseBestellung);
  87. rs = pstmt.executeQuery();
  88. con.commit();
  89. while(rs.next());
  90. }
  91. //Aufgabe 1 b)
  92. query = "SELECT KNR, COUNT(KNR) FROM BESTELLUNG WHERE BID > ? AND BID < ? GROUP BY KNR ORDER BY COUNT(KNR) ASC";
  93. pstmt = con.prepareStatement(query);
  94. for(int i=0;i<(1000000/groesseBestellung);i++){
  95. pstmt.setInt(1, i*groesseBestellung);
  96. pstmt.setInt(2, (i+1)*groesseBestellung);
  97. rs = pstmt.executeQuery();
  98. con.commit();
  99. while(rs.next());
  100. }
  101.  
  102. //Aufgabe 1 c)
  103. query = "SELECT PID, COUNT(PID) FROM BESTELLUNG WHERE BID > ? AND BID < ? GROUP BY PID ORDER BY COUNT(PID) DESC";
  104. pstmt = con.prepareStatement(query);
  105. for(int i=0;i<(1000000/groesseBestellung);i++){
  106. pstmt.setInt(1, i*groesseBestellung);
  107. pstmt.setInt(2, (i+1)*groesseBestellung);
  108. rs = pstmt.executeQuery();
  109. con.commit();
  110. while(rs.next());
  111. }
  112.  
  113. //Aufgabe 1 d)
  114. query = "SELECT PID, COUNT(PID) FROM BESTELLUNG WHERE BID > ? AND BID < ? GROUP BY PID ORDER BY COUNT(PID) ASC";
  115. pstmt = con.prepareStatement(query);
  116. for(int i=0;i<(1000000/groesseBestellung);i++){
  117. pstmt.setInt(1, i*groesseBestellung);
  118. pstmt.setInt(2, (i+1)*groesseBestellung);
  119. rs = pstmt.executeQuery();
  120. con.commit();
  121. while(rs.next());
  122. }
  123.  
  124. Long endtime = System.currentTimeMillis();
  125. Long totaltime = endtime - starttime;
  126. System.out.println("Zeit: " + totaltime.toString());
  127. }
  128.  
  129. public void aufgabe3() throws SQLException{
  130. Long starttime = System.currentTimeMillis();
  131. Statement stmt;
  132. int groesseBestellung = 1000;
  133. //Aufgabe 1 a)
  134. for(int i=0;i<(1000000/groesseBestellung);i++){
  135. query = "SELECT KNR, COUNT(KNR) FROM BESTELLUNG WHERE BID > " + String.valueOf(i*groesseBestellung) + " AND BID < " + String.valueOf((i+1)*groesseBestellung) +" GROUP BY KNR ORDER BY COUNT(KNR) DESC";
  136. stmt = con.createStatement();
  137.  
  138. ResultSet rs = stmt.executeQuery(query);
  139. con.commit();
  140.  
  141. while(rs.next());
  142.  
  143. stmt.close();
  144.  
  145. }
  146. Long endtime = System.currentTimeMillis();
  147. Long totaltime = endtime - starttime;
  148. System.out.println("Zeit: " + totaltime.toString());
  149. }
  150. }
Add Comment
Please, Sign In to add comment