summaryrefslogtreecommitdiffstats
path: root/Bachelor/Datenbanken 2/MyJdbc.java
blob: 373b2a6a1c15f5171e2fe349295fd54714903418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package JDBC;

import java.io.*;
import java.sql.*;
import java.util.*;

public class MyJdbc {
   
	static private Connection con;
	static private Statement  stmt; 
	static private String     query;
	static private ResultSet  rs;
	
	static Vector  sListe = new Vector();
	static HashMap hm     = new HashMap();

	public static void main(String args[]) {
		
		// Teil-Tabelle lesen und Teil-Objekte instanziieren
		getTeile();
		// Liste-Tabelle lesen und Referenzen implementieren
		getReferences();
		// Komponentenstruktur anzeigen
		showTeile();
    
	}
	   
 	public static void getTeile() {
	   // Hilfsvariablen zum Aufbau der Teileobjekte
	   String s1, s2;
	   Teil t;
	  
	   // Treiber-Klasse suchen
	   String driverClass =
		  "sun.jdbc.odbc.JdbcOdbcDriver";
	   try {
		  Class.forName(driverClass);
	   }
	   catch (ClassNotFoundException exc) {
		  System.out.println("c1: "+exc.getMessage());
		  System.exit(1);
	   }
	   
	   Enumeration e = DriverManager.getDrivers();
	   while (e.hasMoreElements())
			System.out.println(e.nextElement());
			
	   // Connection aufbauen
	   try {
		  String url = "jdbc:odbc:MyJdbc"; 
		  con = DriverManager.getConnection(url);
	   }
	   catch (SQLException exc) {
		  System.out.println("c2: "+exc.getMessage());
	   }
 
	   // SLQ-Anweisungen ausf�hren
	   try { 
		  stmt =con.createStatement();

		  // TEIL-Tabelle lesen
		  rs = stmt.executeQuery("Select * from TEIL");
          while (rs.next()) {
   			s1=rs.getString(2);
   			s2=rs.getString(1);
   			t = new Teil(s1,s2);
   			sListe.addElement(t);
			// HashMap mit Key: (eind.) Teilenummer, Value: Objektref. f�llen
			hm.put(s1,t);			  
		  }
		  rs.close();

	   }
	   catch(SQLException exc) {
		  System.out.println("c3: "+exc.getMessage());
	   }  

	}
	
	public static void getReferences() {
		String s1,s2;
		Teil parent, child;
		
		//	Parent-Objekt bestimmen 
		try { 
		  // LISTE-Tabelle lesen
		  rs = stmt.executeQuery("Select * from LISTE");
		  while (rs.next()) {
			s1=rs.getString(2);
			s2=rs.getString(1);
			parent = (Teil)hm.get(s1);
			child  = (Teil)hm.get(s2);
			parent.tC.addElement(child);
			child.tP.addElement(parent);						  
		  }
		  rs.close();

         }
	     catch(SQLException exc) {
			System.out.println("c4: "+exc.getMessage());
		 }  
	}

	public static void showTeile() {
		Teil fahrrad;
		fahrrad=(Teil)sListe.elementAt(0);
		System.out.println("0.0:"+fahrrad);
        getChildren(fahrrad,0);
	}
	
	private static void getChildren(Teil t, int j) {
		int i=1;
		Teil hilf;
	    Enumeration anz;
		anz = t.getChildren();
		while(anz.hasMoreElements()) {
			hilf = (Teil)anz.nextElement();         
			System.out.println(j+"."+i+":"+hilf);
			getChildren(hilf,i);
			i++;         
		}
	}

}