mport java.util.*;
import java.sql.*;
import java.text.*;
/**
* Dieser Klasse hält Methoden bereit für die Verbindung und die Schliessung mit
* der Datenbank, den einfügen, löschen und die erstmalige Generierung sowie den
* Update und andere wichtigen Methoden.
*/
public class DBManager {
Connection conn;
public PreparedStatement sqlStatement;
public ResultSet rs = null;
GregorianCalendar cal = new GregorianCalendar();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String datum = df.format(cal.getTime());
public Connection getConnection() {
try {
return this.conn;
} catch (Exception e) {
System.out.println("Error in " + e);
}
return null;
}
public void createTable(String tableName, String attributes) {
try {
Statement st = this.getConnection().createStatement();
st.execute("CREATE TABLE " + tableName + " (" + attributes + ")");
} catch (Exception e) {
System.out
.println("Error in DBManager.createTable(tableName, attributes):\n"
+ tableName + "\n" + e);
}
}
/**
* Erzeugt alle Relationen von der Datenbank.
*/
public void createDatenbank() {
try {
createTable("Webserver", "Name VARCHAR(600) NOT NULL, "
+ "Datum DATE NOT NULL, "
+ "Kommentar VARCHAR(2000) NOT NULL, "
+ "PRIMARY KEY (Name)");
createTable("Datenbank", "ID CHAR(10) NOT NULL, "
+ "Link VARCHAR(300) UNIQUE NOT NULL, "
+ "Kommentar VARCHAR(2000), " + "PRIMARY KEY (ID)");
createTable("Pfad", "ID CHAR(10) NOT NULL, "
+ "GlobalPfad VARCHAR(2000) NOT NULL, "
+ "LokalPfad VARCHAR(2000) NOT NULL, "
+ "Dateiname VARCHAR(2000) NOT NULL, "
+ "Endung VARCHAR(600) NOT NULL, "
+ "Zugehoerigkeit VARCHAR(60) NOT NULL, "
+ "Existiert BOOL NOT NULL, " + "Datum DATE, "
+ "PRIMARY KEY (ID)");
createTable("PDF", "Name VARCHAR(60) NOT NULL, "
+ "Titel VARCHAR(60) NOT NULL, "
+ "Autor VARCHAR(250) NOT NULL, "
+ "Zusammenfassung VARCHAR(7000), "
+ "Status BOOL NOT NULL, " + "Art VARCHAR(50) NOT NULL, "
+ "Datenbank CHAR(10) NOT NULL, "
+ "Webserver VARCHAR(600) NOT NULL, "
+ "PRIMARY KEY (Name), "
+ "CONSTRAINT FK_PDF_1 FOREIGN KEY (Datenbank) "
+ " REFERENCES Datenbank (ID), "
+ "CONSTRAINT FK_PDF_2 FOREIGN KEY (Webserver) "
+ " REFERENCES Webserver (Name)");
createTable("Bilder", "Name VARCHAR(60) NOT NULL, "
+ "Hoehe CHAR(10) NOT NULL, "
+ "Breite CHAR(10) NOT NULL, " + "Seite CHAR(4), "
+ "Zustand BOOL NOT NULL, " + "Thumpnail BOOL NOT NULL, "
+ "PDF VARCHAR(60) NOT NULL, " + "PRIMARY KEY (Name), "
+ "CONSTRAINT FK_Bilder_1 FOREIGN KEY (PDF) "
+ "REFERENCES PDF (Name)");
createTable("html", "Name VARCHAR(60) NOT NULL, "
+ "Version CHAR(5), " + "Zustand BOOL NOT NULL, "
+ "PDF VARCHAR(60) NOT NULL, " + "PRIMARY KEY (Name), "
+ "CONSTRAINT FK_html_2 FOREIGN KEY (PDF) "
+ "REFERENCES PDF (Name)");
} catch (Exception e) {
System.out.println("Error in DBManager.createDatenbank(): " + e);
}
}
/**
* Nimmt die Verbindung mit der Datenbank auf.
*/
public void connectDB() {
try {
String driverName = "org.postgresql.Driver";
String dbURL = "jdbc:postgresql://localhost/hallo";
String name = "postgres";
String pass = "pass";
Class.forName(driverName);
this.conn = DriverManager.getConnection(dbURL, name, pass);
getConnection();
} catch (SQLException sqle) {
System.out
.println("S: "
+ sqle + "\n" + ((SQLException) sqle).getSQLState());
} catch (Exception e) {
System.out
.println("pass): "
+ e);
}
}
/**
* Schließt die Verbindung zur Datenbank. Die Instanzvariable conn
* (Connection) der Klasse DBManager wird auf "null" gesetzt.
*/
public void closeDB() {
try {
if (!this.conn.isClosed()) {
this.conn.close();
this.conn = null;
}
} catch (Exception e) {
System.out.println("Error in DBManager.closeDB(): " + e);
}
}
public DBManager() {
this.conn = null;
}
}