summaryrefslogtreecommitdiffstats
path: root/Bachelor/Datenbanken 2/MyJdbc.java
diff options
context:
space:
mode:
Diffstat (limited to 'Bachelor/Datenbanken 2/MyJdbc.java')
-rw-r--r--Bachelor/Datenbanken 2/MyJdbc.java124
1 files changed, 124 insertions, 0 deletions
diff --git a/Bachelor/Datenbanken 2/MyJdbc.java b/Bachelor/Datenbanken 2/MyJdbc.java
new file mode 100644
index 0000000..373b2a6
--- /dev/null
+++ b/Bachelor/Datenbanken 2/MyJdbc.java
@@ -0,0 +1,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++;
+ }
+ }
+
+}