[02] Added files from example project
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
parent
26089b9f5a
commit
c4bce00924
|
@ -0,0 +1,3 @@
|
||||||
|
jdbc_user=vsisp12
|
||||||
|
jdbc_pass=h9QE2Vxc
|
||||||
|
jdbc_url=jdbc:db2://vsisls4.informatik.uni-hamburg.de:50001/VSISP
|
|
@ -0,0 +1,52 @@
|
||||||
|
package de.dis2011;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kleine Helferklasse zum Einlesen von Formulardaten
|
||||||
|
*/
|
||||||
|
public class FormUtil {
|
||||||
|
/**
|
||||||
|
* Liest einen String vom standard input ein
|
||||||
|
* @param label Zeile, die vor der Eingabe gezeigt wird
|
||||||
|
* @return eingelesene Zeile
|
||||||
|
*/
|
||||||
|
public static String readString(String label) {
|
||||||
|
String ret = null;
|
||||||
|
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.print(label+": ");
|
||||||
|
ret = stdin.readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liest einen Integer vom standard input ein
|
||||||
|
* @param label Zeile, die vor der Eingabe gezeigt wird
|
||||||
|
* @return eingelesener Integer
|
||||||
|
*/
|
||||||
|
public static int readInt(String label) {
|
||||||
|
int ret = 0;
|
||||||
|
boolean finished = false;
|
||||||
|
|
||||||
|
while(!finished) {
|
||||||
|
String line = readString(label);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ret = Integer.parseInt(line);
|
||||||
|
finished = true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.err.println("Ungültige Eingabe: Bitte geben Sie eine Zahl an!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
package de.dis2011;
|
||||||
|
|
||||||
|
import de.dis2011.data.Makler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hauptklasse
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
/**
|
||||||
|
* Startet die Anwendung
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
showMainMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zeigt das Hauptmenü
|
||||||
|
*/
|
||||||
|
public static void showMainMenu() {
|
||||||
|
//Menüoptionen
|
||||||
|
final int MENU_MAKLER = 0;
|
||||||
|
final int QUIT = 1;
|
||||||
|
|
||||||
|
//Erzeuge Menü
|
||||||
|
Menu mainMenu = new Menu("Hauptmenü");
|
||||||
|
mainMenu.addEntry("Makler-Verwaltung", MENU_MAKLER);
|
||||||
|
mainMenu.addEntry("Beenden", QUIT);
|
||||||
|
|
||||||
|
//Verarbeite Eingabe
|
||||||
|
while(true) {
|
||||||
|
int response = mainMenu.show();
|
||||||
|
|
||||||
|
switch(response) {
|
||||||
|
case MENU_MAKLER:
|
||||||
|
showMaklerMenu();
|
||||||
|
break;
|
||||||
|
case QUIT:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zeigt die Maklerverwaltung
|
||||||
|
*/
|
||||||
|
public static void showMaklerMenu() {
|
||||||
|
//Menüoptionen
|
||||||
|
final int NEW_MAKLER = 0;
|
||||||
|
final int BACK = 1;
|
||||||
|
|
||||||
|
//Maklerverwaltungsmenü
|
||||||
|
Menu maklerMenu = new Menu("Makler-Verwaltung");
|
||||||
|
maklerMenu.addEntry("Neuer Makler", NEW_MAKLER);
|
||||||
|
maklerMenu.addEntry("Zurück zum Hauptmenü", BACK);
|
||||||
|
|
||||||
|
//Verarbeite Eingabe
|
||||||
|
while(true) {
|
||||||
|
int response = maklerMenu.show();
|
||||||
|
|
||||||
|
switch(response) {
|
||||||
|
case NEW_MAKLER:
|
||||||
|
newMakler();
|
||||||
|
break;
|
||||||
|
case BACK:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Legt einen neuen Makler an, nachdem der Benutzer
|
||||||
|
* die entprechenden Daten eingegeben hat.
|
||||||
|
*/
|
||||||
|
public static void newMakler() {
|
||||||
|
Makler m = new Makler();
|
||||||
|
|
||||||
|
m.setName(FormUtil.readString("Name"));
|
||||||
|
m.setAddress(FormUtil.readString("Adresse"));
|
||||||
|
m.setLogin(FormUtil.readString("Login"));
|
||||||
|
m.setPassword(FormUtil.readString("Passwort"));
|
||||||
|
m.save();
|
||||||
|
|
||||||
|
System.out.println("Makler mit der ID "+m.getId()+" wurde erzeugt.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
package de.dis2011;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kleine Helferklasse für Menüs
|
||||||
|
* Zuvor müssen mit addEntry Menüoptionen hinzugefügt werden. Mit
|
||||||
|
* der Methode show() wird das Menü angezeigt und die mit der Option
|
||||||
|
* angegebene Konstante zurückgeliefert.
|
||||||
|
*
|
||||||
|
* Beispiel:
|
||||||
|
* Menu m = new Menu("Hauptmenü");
|
||||||
|
* m.addEntry("Hart arbeiten", 0);
|
||||||
|
* m.addEntry("Ausruhen", 1);
|
||||||
|
* m.addEntry("Nach Hause gehen", 2);
|
||||||
|
* int wahl = m.show();
|
||||||
|
*
|
||||||
|
* Angezeigt wird dann das Menü:
|
||||||
|
* Hauptmenü:
|
||||||
|
* [1] Hart arbeiten
|
||||||
|
* [2] Ausruhen
|
||||||
|
* [3] Nach Hause gehen
|
||||||
|
* --
|
||||||
|
*/
|
||||||
|
public class Menu {
|
||||||
|
private String title;
|
||||||
|
private ArrayList<String> labels = new ArrayList<String>();
|
||||||
|
private ArrayList<Integer> returnValues = new ArrayList<Integer>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor.
|
||||||
|
* @param title Titel des Menüs z.B. "Hauptmenü"
|
||||||
|
*/
|
||||||
|
public Menu(String title) {
|
||||||
|
super();
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fügt einen Menüeintrag zum Menü hinzu
|
||||||
|
* @param label Name des Eintrags
|
||||||
|
* @param returnValue Konstante, die bei Wahl dieses Eintrags zurückgegeben wird
|
||||||
|
*/
|
||||||
|
public void addEntry(String label, int returnValue) {
|
||||||
|
this.labels.add(label);
|
||||||
|
this.returnValues.add(new Integer(returnValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zeigt das Menü an
|
||||||
|
* @return Die Konstante des ausgewählten Menüeintrags
|
||||||
|
*/
|
||||||
|
public int show() {
|
||||||
|
int selection = -1;
|
||||||
|
|
||||||
|
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
|
while(selection == -1) {
|
||||||
|
System.out.println(title+":");
|
||||||
|
|
||||||
|
for(int i = 0; i < labels.size(); ++i) {
|
||||||
|
System.out.println("["+(i+1)+"] "+labels.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("-- ");
|
||||||
|
try {
|
||||||
|
selection = Integer.parseInt(stdin.readLine());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(selection < 1 || selection > returnValues.size()) {
|
||||||
|
System.err.println("Ungültige Eingabe!");
|
||||||
|
selection = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnValues.get(selection-1);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,75 @@
|
||||||
|
package de.dis2011.data;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Einfaches Singleton zur Verwaltung von Datenbank-Verbindungen.
|
||||||
|
*
|
||||||
|
* @author Michael von Riegen
|
||||||
|
* @version April 2009
|
||||||
|
*/
|
||||||
|
class DB2ConnectionManager {
|
||||||
|
|
||||||
|
// instance of Driver Manager
|
||||||
|
private static DB2ConnectionManager _instance = null;
|
||||||
|
|
||||||
|
// DB2 connection
|
||||||
|
private Connection _con;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt eine Datenbank-Verbindung
|
||||||
|
*/
|
||||||
|
private DB2ConnectionManager() {
|
||||||
|
try {
|
||||||
|
// Holen der Einstellungen aus der db2.properties Datei
|
||||||
|
Properties properties = new Properties();
|
||||||
|
URL url = ClassLoader.getSystemResource("db2.properties");
|
||||||
|
FileInputStream stream = new FileInputStream(new File(url.toURI()));
|
||||||
|
properties.load(stream);
|
||||||
|
stream.close();
|
||||||
|
|
||||||
|
String jdbcUser = properties.getProperty("jdbc_user");
|
||||||
|
String jdbcPass = properties.getProperty("jdbc_pass");
|
||||||
|
String jdbcUrl = properties.getProperty("jdbc_url");
|
||||||
|
|
||||||
|
// Verbindung zur DB2 herstellen
|
||||||
|
Class.forName("com.ibm.db2.jcc.DB2Driver");
|
||||||
|
_con = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass);
|
||||||
|
|
||||||
|
} catch (IOException | ClassNotFoundException | SQLException | URISyntaxException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liefert Instanz des Managers
|
||||||
|
*
|
||||||
|
* @return DB2ConnectionManager
|
||||||
|
*/
|
||||||
|
static DB2ConnectionManager getInstance() {
|
||||||
|
if (_instance == null) {
|
||||||
|
_instance = new DB2ConnectionManager();
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liefert eine Verbindung zur DB2 zurC<ck
|
||||||
|
*
|
||||||
|
* @return Connection
|
||||||
|
*/
|
||||||
|
Connection getConnection() {
|
||||||
|
return _con;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,155 @@
|
||||||
|
package de.dis2011.data;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
import de.dis2011.data.DB2ConnectionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makler-Bean
|
||||||
|
*
|
||||||
|
* Beispiel-Tabelle:
|
||||||
|
* CREATE TABLE makler(id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO CACHE) PRIMARY KEY,
|
||||||
|
* name varchar(255),
|
||||||
|
* address varchar(255),
|
||||||
|
* login varchar(40) UNIQUE,
|
||||||
|
* password varchar(40));
|
||||||
|
*/
|
||||||
|
public class Makler {
|
||||||
|
private int id = -1;
|
||||||
|
private String name;
|
||||||
|
private String address;
|
||||||
|
private String login;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lädt einen Makler aus der Datenbank
|
||||||
|
* @param id ID des zu ladenden Maklers
|
||||||
|
* @return Makler-Instanz
|
||||||
|
*/
|
||||||
|
public static Makler load(int id) {
|
||||||
|
try {
|
||||||
|
// Hole Verbindung
|
||||||
|
Connection con = DB2ConnectionManager.getInstance().getConnection();
|
||||||
|
|
||||||
|
// Erzeuge Anfrage
|
||||||
|
String selectSQL = "SELECT * FROM makler WHERE id = ?";
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(selectSQL);
|
||||||
|
pstmt.setInt(1, id);
|
||||||
|
|
||||||
|
// Führe Anfrage aus
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
if (rs.next()) {
|
||||||
|
Makler ts = new Makler();
|
||||||
|
ts.setId(id);
|
||||||
|
ts.setName(rs.getString("name"));
|
||||||
|
ts.setAddress(rs.getString("address"));
|
||||||
|
ts.setLogin(rs.getString("login"));
|
||||||
|
ts.setPassword(rs.getString("password"));
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Speichert den Makler in der Datenbank. Ist noch keine ID vergeben
|
||||||
|
* worden, wird die generierte Id von DB2 geholt und dem Model übergeben.
|
||||||
|
*/
|
||||||
|
public void save() {
|
||||||
|
// Hole Verbindung
|
||||||
|
Connection con = DB2ConnectionManager.getInstance().getConnection();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// FC<ge neues Element hinzu, wenn das Objekt noch keine ID hat.
|
||||||
|
if (getId() == -1) {
|
||||||
|
// Achtung, hier wird noch ein Parameter mitgegeben,
|
||||||
|
// damit spC$ter generierte IDs zurC<ckgeliefert werden!
|
||||||
|
String insertSQL = "INSERT INTO makler(name, address, login, password) VALUES (?, ?, ?, ?)";
|
||||||
|
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(insertSQL,
|
||||||
|
Statement.RETURN_GENERATED_KEYS);
|
||||||
|
|
||||||
|
// Setze Anfrageparameter und fC<hre Anfrage aus
|
||||||
|
pstmt.setString(1, getName());
|
||||||
|
pstmt.setString(2, getAddress());
|
||||||
|
pstmt.setString(3, getLogin());
|
||||||
|
pstmt.setString(4, getPassword());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
|
// Hole die Id des engefC<gten Datensatzes
|
||||||
|
ResultSet rs = pstmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
setId(rs.getInt(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
} else {
|
||||||
|
// Falls schon eine ID vorhanden ist, mache ein Update...
|
||||||
|
String updateSQL = "UPDATE makler SET name = ?, address = ?, login = ?, password = ? WHERE id = ?";
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(updateSQL);
|
||||||
|
|
||||||
|
// Setze Anfrage Parameter
|
||||||
|
pstmt.setString(1, getName());
|
||||||
|
pstmt.setString(2, getAddress());
|
||||||
|
pstmt.setString(3, getLogin());
|
||||||
|
pstmt.setString(4, getPassword());
|
||||||
|
pstmt.setInt(5, getId());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
|
pstmt.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue