wita datenbankanbindung: schnittstellen, methoden, attribute
TRANSCRIPT
WiTa Datenbankanbindung:
Schnittstellen, Methoden, Attribute
Übersicht• Schnittstellen, Methoden und Attribute für–Speichern
–Laden
–Hilfsstrukturen
Speichern
Speichern
• Interfaces– Storeable: get String()
– Createable: create(), remove()– Updateable: update()
• Hilfsmethoden (static)– executeCommand– executeCreate
CreateUpdateRemove
Interface: Storeable
• public String getCreateString()
“INSERT INTO [Tabelle] ([Tab.-Spalten])VALUES ([Attribute])”
• public String getUpdateString()
“UPDATE [Tabelle] SET [Spalte = Attribut] WHERE [Prim.-Key = Objekt-ID]”
• public String getRemoveString()
“DELETE FROM [Tabelle] WHERE [Prim.-Key = Objekt-ID]”
Interface: Createable
• create():public void create() throws SQLException, ValidationException {
this.validate();
executeCreate(this, this.getLogger());
}
• remove():public void remove() throws SQLException {
executeCommand(this.getRemoveString(), this.getLogger());
}
Interface: Updateable
• update():public void update() throws SQLException, ValidationException {
if(this.getId() == 0){
String[] error = {this+[ERROR-MSG]};
throw new ValidationException(error);
}
this.validate();
String[] strUpdate = {this.getUpdateString()};
executeUpdate(strUpdate, this.getLogger());
}
Hilfsmethode: executeCommand(3 Varianten)
1. public static void executeCommand(String sqlString,Logger logger
)
2. public static void executeCommand(String[] sqlString,Logger logger
)
Hilfsmethode: executeCommand(3. Variante)
3. public static void executeCommand(String sqlString,Logger logger,Connection connect,boolean commit
)
Hilfsmethode: executeCreate(2 Varianten)
1. public static int executeCreate(Storeable ObjectToStore,Logger logger
)
2. public static int executeCreate(Storeable ObjectToStore,Logger logger,Connection connect,boolean commit
)
Laden
Laden
• Interfaces– Restoreable: restore(ResultSet)
• Hilfsmethoden (static)– genericFind– calculateQuery– getMemberFromDatabase
Interface: Restoreable
• restore(ResultSet): (in BankDetail)
public void restore(ResultSet source) throws SQLException {
this.setId (source.getInt ("BankDetail_id") ); this.setBank (source.getString ("bank") ); this.setBankCode (source.getInt ("bankCode") ); this.setAccountNumber( source.getLong("accountNumber") ); this.ownerId = source.getInt("owner"); this.setReasonForPayment (source.getString("reasonForPayment") ); log.info(this+"restore("+ source.getClass().getName() +"): "
+"restoring was successful");}
Hilfsmethode: genericFind
public static Collection genericFind(String queryString,String restoreableClassName,Logger logger
)
Beispiel:genericFind(
"SELECT * FROM Address WHERE street LIKE "+ getQuery(street), "de.uni_marburg.wita.orga.Address", log
);
Hilfsmethode: calculateQuery
public static Object calculateQuery(String sqlQuery,boolean longOrDouble,Logger logger
)
Beispiel:Long nCounter = (Long) calculateQuery(
"SELECT count(*) FROM … " …, true, log
);
Hilfsmethode: getMemberFromDatabase
public static Object getMemberFromDatabase(String sqlQuery,String restoreableObjectName,Logger logger
)
Beispiel:getMemberFromDatabase(
"SELECT * FROM Address WHERE id = "+ addressId, "de.uni_marburg.wita.orga.Address", log
);
Hilfsstrukturen
Hilfsstrukturen
• Interfaces– SimpleIntId: Id([int])
• Hilfsmethoden (static)– Connection getConnection– String makeStringFuzzy– String getCompString– String getQuery– String[] makeSQLString
getset
Hilfsmethode: getQuery
public static String getQuery( )
Beispiele:(String) test (String) ‘test‘(Date) [DATE] (String) ‘yyyy-MM-dd HH:mm:ss‘(boolean) true (String) 1(boolean) false (String) 2
StringDate boolean
Hilfsmethode: makeSQLString
String makeSQLString( String[] fieldList, String prefix,
String separator)
Beispiel: fieldList = {“eins“,“zwei“,“drei“}
prefix = “Obj.“
separator = “; “
“Obj.eins; Obj.zwei; Obj.drei“
Parameter:
Rückgabe:
Hilfsstrukturen
• Hilfsattribute
– final static transient String[] sqlNames
Beispiel: (in BankDetail)private final static transient String[] sqlNames = {
"BankDetail_id“ , "bank" , "bankCode“ , "accountNumber" , "owner“ , "reasonForPayment"
};
privatepublic
Hilfsstrukturen
• Hilfsattribute
– private transient int
[Attribut-Name]Id
Beispiel: (in BankDetail)
…
private Person owner;private transient int ownerId;
...