db4o - Ausführliches Beispiel

From XennisWiki
Jump to: navigation, search

] Die Grundlagen zu db4o findet du in dem entsprechenden Artikel.

Select, Insert, Update und Delete

Sample.java

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Predicate;
import com.db4o.query.Query;

public class Sample {

	public static String DB_PATH = "./car.db";
	
	public static void main(String[] args) {
		ObjectContainer db = null;

		try {
			EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();			
			db = Db4oEmbedded.openFile(conf, DB_PATH);
			
			createTable(db);
			instertInto(db);
			queryOne(db);
			queryTwo(db);
			queryThree(db);
			queryFour(db);
			queryFive(db);
			querySix(db);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	/**
	 * Create tables
	 * 
	 * @param db
	 */
	private static void createTable(ObjectContainer db) {
		//db.store(Studenten.class);
		//db.store(Vorlesungen.class);
	}
	
	/**
	 * Insert Data into tables
	 * 
	 * @param db
	 */
	private static void instertInto(ObjectContainer db) {
		Studenten s24002 = new Studenten(24002, "Xenokrates", 12);
		Studenten s25403 = new Studenten(25403, "Jonas", 12);
		Studenten s26120 = new Studenten(26120, "Fichte", 10);

		Vorlesungen v5001 = new Vorlesungen(5001, "Grundzuege", 3);
		Vorlesungen v5041 = new Vorlesungen(5041, "Ethik", 1);
		Vorlesungen v5043 = new Vorlesungen(5043, "Erkenntnistheorie", 3);
		Vorlesungen v4052 = new Vorlesungen(4052, "Logik", 4);
		Vorlesungen v5216 = new Vorlesungen(5216, "Bioethik", 2);
		
		hoeren(s25403, v5041);
		hoeren(s25403, v4052);
		hoeren(s26120, v5001);
		hoeren(s26120, v4052);

		db.store(s24002);
		db.store(s25403);
		db.store(s26120);
		db.store(v5001);
		db.store(v5041);
		db.store(v5043);
		db.store(v4052);
		db.store(v5216);
	}
	
	private static void hoeren(Studenten student, Vorlesungen vorlesung) {
		student.addVorlesung(vorlesung);
		vorlesung.addStudent(student);
	}
	
	/**
	 * Query:
	 * 
	 * Select	Name
	 * From		Studenten, Vorlesungen, Hoeren
	 * Where	Hoeren.MatrNr = Studenten.Matr.Nr
	 * And          Hoeren.VorlNr = Vorlesungen.VorlNr
         * And          Vorlesungen.Titel = 'Logik'
	 * 
	 * @param db
	 */
	private static void queryOne(ObjectContainer db) {
		System.out.println("###### Query 1");
		List<Studenten> result = db.query(new Predicate<Studenten>() {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean match(Studenten s) {
				return s.checkVorlesung("Logik");
			}
		});

		for(Studenten v : result) {
			System.out.println(v);
		}
	}
	
	/**
	 * Query:
	 * 
	 * Update 	Studenten
	 * Set		Semester = 13
	 * Where	Name = 'Xenokrates'
	 * 
	 * @param db
	 */
	private static void queryTwo(ObjectContainer db) {
		System.out.println("###### Query 2");
		Studenten student = new Studenten();
		student.setName("Xenokrates");
		
		List<Studenten> result = db.queryByExample(student);
		for(Studenten s : result) {
			s.setSemester(13);
			db.store(s);
			System.out.println("Update: " + s);
		}
	}
	
	/**
	 * Query:
	 * 
	 * Delete From	Vorlesungen
	 * Where	Title = 'Erkenntnistheorie'
	 * 
	 * @param db
	 */
	private static void queryThree(ObjectContainer db) {
		System.out.println("###### Query 3");
		Vorlesungen vorlesung = new Vorlesungen();
		vorlesung.setTitel("Erkenntnistheorie");

		List<Vorlesungen> result = db.queryByExample(vorlesung);
		for(Vorlesungen v : result) {
			System.out.println("Delete: " + v);
			db.delete(v);
		}
	}
	
	/**
	 * Query:
	 * 
	 * Select	*
	 * From		Vorlesungen
	 * Where	Sws Between 2 And 3
	 * 
	 * @param db
	 */
	private static void queryFour(ObjectContainer db) {
		System.out.println("###### Query 4");
		List<Vorlesungen> result = db.query(new Predicate<Vorlesungen>() {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean match(Vorlesungen v) {
				return v.getSws() == 2 || v.getSws() == 3;
			}
		});

		for(Vorlesungen v : result) {
			System.out.println(v);
		}
	}
	
	/**
	 * Query:
	 * 
	 * Select	*
	 * From		Studenten
	 * Where	Name Like '%s'
	 * ORDER BY	Name
	 * 
	 * @param db
	 */
	private static void queryFive(ObjectContainer db) {
		System.out.println("###### Query 5");
		List<Studenten> result = db.query(new Predicate<Studenten>() {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean match(Studenten s) {
				return s.getName().endsWith("s");
			}
		});
		
		for(Studenten s : result) {
			System.out.println(s);
		}
	}
	
	/**
	 * Query:
	 * 
	 * Select	Avg(Sws)
	 * From		Vorlesungen
	 * 
	 * @param db
	 */
	private static void querySix(ObjectContainer db) {
		System.out.println("###### Query 6");
		Query query = db.query();
		query.constrain(Vorlesungen.class);

		List<Vorlesungen> result = query.execute();
		
		double avg = 0;
		for(Vorlesungen v : result) {
			avg += v.getSws();
		}
		avg /= result.size();
		
		System.out.println("Avg: " + avg);
	}
}

Vorlesungen.java

import java.util.LinkedList;
import java.util.List;

public class Vorlesungen {

	private int vorlNr;
	private String titel;
	private int sws;
	private List<Studenten>  hoeren;
	
	public Vorlesungen() {
		this.hoeren = new LinkedList<Studenten>();
	}
	
	public Vorlesungen(int vorlNr, String titel, int sws) {
		this();
		this.vorlNr = vorlNr;
		this.titel = titel;
		this.sws = sws;
	}
	
	public void setTitel(String titel) {
		this.titel = titel;
	}
	
	public String getTitel() {
		return this.titel;
	}
	
	public int getSws() {
		return this.sws;
	}
	
	public void addStudent(Studenten student) {
		this.hoeren.add(student);
	}

	@Override
	public String toString() {
		return "Vorlesung [vorlNr=" + vorlNr + ", titel=" + titel + ", sws="
				+ sws + "]";
	}
}

Studenten.java

import java.util.LinkedList;
import java.util.List;

public class Studenten {

	private int matrNr;
	private String name;
	private int semester;
	private List<Vorlesungen> hoeren;
		
	public Studenten() {
		this.hoeren = new LinkedList<Vorlesungen>();
	}
	
	public Studenten(int matrNr, String name, int semester) {
		this();
		this.matrNr = matrNr;
		this.name = name;
		this.semester = semester;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.getName();
	}
		
	public void setSemester(int semester) {
		this.semester = semester;
	}
	
	public void addVorlesung(Vorlesungen vorlesung) {
		this.hoeren.add(vorlesung);
	}
		
	public boolean checkVorlesung(String titel) {
		for(Vorlesungen v : this.hoeren) {
			if (v.getTitel().equals(titel)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return "Studenten [matrNr=" + matrNr + ", name=" + name
					+ ", semester=" + semester + "]";
	}
}

Commit und Rollback

Das folgende Programm nimmt ein Update an einem Datensatz vor und anschließend wird der Benutzer gefragt, ob er diese Änderung übernehmen (commit) oder rückgängig (rollback) machen will.

CommitRollback.java

import java.util.List;

import info1package.Eingabe;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;


public class CommitRollback {

	public static String DB_PATH = "./car.db";
	
	public static void main(String[] args) {
		ObjectContainer db = null;

		try {
			EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();			
			db = Db4oEmbedded.openFile(conf, DB_PATH);
			
			updateByUser(db);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	private static void updateByUser(ObjectContainer db) {
		Vorlesungen vorlesung = new Vorlesungen();
		vorlesung.setTitel("Ethik");
		List<Vorlesungen> result = db.queryByExample(vorlesung);
		for(Vorlesungen v : result) {
			v.setTitel("WTF");
			db.store(v);
		}
		
		System.out.println("Wollen sie das Update wirklich ausführen? [j/n]");
		Eingabe tastatur = Eingabe.oeffnen("");
		String s = tastatur.readString();
		switch (s) {
		case "j":
			db.commit();
			System.out.println("Commit");
			break;
		/* Im Zweifelsfall immer Rollback */
		default:
			db.rollback();
			System.out.println("Rollback");
			break;
		}
	}
}

Siehe auch

[[Category:Database]