Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class Main {
  6.  
  7.     private static int[] nestedLoopJoin(List<Professor> profs, List<Vorlesung> vls) {
  8.         int counterMatches = 0;
  9.         int counter = 0;
  10.  
  11.         for (int i = 0; i < vls.size(); i++) {
  12.             for (int j = 0; j < profs.size(); j++) {
  13.                 if (vls.get(i).getGelesenVon() == profs.get(j).getPersNr()) {
  14.                     counterMatches++;
  15.                 }
  16.                 counter++;
  17.             }
  18.         }
  19.  
  20.         return new int[]{counter,counterMatches};
  21.     }
  22.  
  23.     private static int[] mergeJoin(List<Professor> profs, List<Vorlesung> vls) {
  24.         int counterMatches = 0;
  25.         int counter = 0;
  26.         int currentProf = 0;
  27.  
  28.         for (int i = 0; i < vls.size(); i++) {
  29.             if (vls.get(i).getGelesenVon() == profs.get(currentProf).getPersNr()) {
  30.                 counterMatches++;
  31.             } else {
  32.                 currentProf++;
  33.             }
  34.             counter++;
  35.         }
  36.  
  37.         return new int[]{counter,counterMatches};
  38.     }
  39.  
  40.     /**
  41.      * Führen Sie den kompilierten Code mit
  42.      *   java -cp .:postgresql-9.4.1208.jar Exercise3
  43.      * aus. Passen Sie gegebenenfalls den Pfad zur postgres-jar an.
  44.      */
  45.     public static void main(String[] args) {
  46.         try{
  47.             // setup connection
  48.             Class.forName("org.postgresql.Driver");
  49.             String url = "jdbc:postgresql://localhost/uni?user=postgres&password=Sur.15.Mi";
  50.             Connection conn = DriverManager.getConnection(url);
  51.  
  52.             // setup lists
  53.             List<Professor> professoren = new ArrayList<Professor>();
  54.             List<Vorlesung> vorlesungen = new ArrayList<Vorlesung>();
  55.  
  56.             // setup stmt
  57.             Statement stmt = conn.createStatement();
  58.  
  59.             // Professoren
  60.             ResultSet rsetProf = stmt.executeQuery("select * from Professoren") ;
  61.             while (rsetProf.next()) {
  62.                 professoren.add(new Professor(rsetProf));
  63.             }
  64.  
  65.             // Vorlesungen
  66.             ResultSet rsetVorl = stmt.executeQuery("select * from Vorlesungen ORDER BY gelesenVon") ;
  67.             while (rsetVorl.next()) {
  68.                 vorlesungen.add(new Vorlesung(rsetVorl));
  69.             }
  70.  
  71.             int[] ergNL = nestedLoopJoin(professoren,vorlesungen);
  72.             System.out.println("Vergleiche Nested-Loop-Join: "+ergNL[0]);
  73.             System.out.println("Matches Nested-Loop-Join: "+ergNL[1]);
  74.  
  75.             int[] ergMerge = mergeJoin(professoren,vorlesungen);
  76.             System.out.println("Vergleiche Merge-Join: "+ergMerge[0]);
  77.             System.out.println("Matches Merge-Join: "+ergMerge[1]);
  78.  
  79.         }
  80.         catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.     }
  84. }
  85.  
  86. class Professor {
  87.     private String name;
  88.     private int persnr;
  89.     private String rang;
  90.     private int raum;
  91.  
  92.     public Professor(ResultSet rset) throws SQLException {
  93.         name = rset.getString("name");
  94.         persnr = rset.getInt("persnr");
  95.         rang = rset.getString("rang");
  96.         raum = rset.getInt("raum");
  97.     }
  98.  
  99.     public String getName() {
  100.         return name;
  101.     }
  102.  
  103.     public int getPersNr() {
  104.         return persnr;
  105.     }
  106.  
  107.     public String getRang() {
  108.         return rang;
  109.     }
  110.  
  111.     public int getRaum() {
  112.         return raum;
  113.     }
  114. }
  115.  
  116. class Vorlesung {
  117.     private int vorlnr;
  118.     private String titel;
  119.     private int sws;
  120.     private int gelesenVon;
  121.  
  122.     public Vorlesung(ResultSet rset) throws SQLException {
  123.         vorlnr = rset.getInt("vorlnr");
  124.         titel = rset.getString("titel");
  125.         sws = rset.getInt("sws");
  126.         gelesenVon = rset.getInt("gelesenVon");
  127.     }
  128.  
  129.     public int getVorlnr() {
  130.         return vorlnr;
  131.     }
  132.  
  133.     public String getTitel() {
  134.         return titel;
  135.     }
  136.  
  137.     public int getSws() {
  138.         return sws;
  139.     }
  140.  
  141.     public int getGelesenVon() {
  142.         return gelesenVon;
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement