Ich möchte etwas wie das machen. Wie kann ich das in Java mit Generika schön machen?Java-Generics setzen String oder Integer-Argument in Methode
public <T [Integer or String]> Float getFloat(<T> column) throws SQLException{
Float result;
try {
result = ResultSet.getFloat(column);
} catch (NullPointerException e) {
result = null;
}
return result;
}
Jetzt sieht mein Code wie folgt aus:
public Float getFloat(String column) throws SQLException{
Float result;
try {
result = rs.getFloat(column);
} catch (NullPointerException e) {
result = null;
}
return result;
}
public Float getFloat(int column) throws SQLException{
Float result;
try {
result = rs.getFloat(column);
} catch (NullPointerException e) {
result = null;
}
return result;
}
ich dies tun, weil Methode rs.getFloat() String empfangen kann (Spalte Label) & Integer (Spalte Index) nur. Aber das ist nicht wichtig für meine Methode.
Es wäre möglich, lambdas zu verwenden: 'public Float getFloat (Funktion Funktion, T t)' aber wegen der geprüften Ausnahmen von JDBC wird es nicht viel Spaß machen, das zu tun, –
user140547