Renamed de.dis2011 to de.dis2017
* includes also some smaller changes Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
74
02/src/de/dis2017/data/DB2ConnectionManager.java
Normal file
74
02/src/de/dis2017/data/DB2ConnectionManager.java
Normal file
@ -0,0 +1,74 @@
|
||||
package de.dis2017.data;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
153
02/src/de/dis2017/data/Makler.java
Normal file
153
02/src/de/dis2017/data/Makler.java
Normal file
@ -0,0 +1,153 @@
|
||||
package de.dis2017.data;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user