Hallo,
habe folgendes Problem:
Ich möchte gern ResultSets sofort in eine ArrayList ablegen, um den Datenbankzugriff zu minimieren. Dazu habe ich mir eine Klasse ResultList geschrieben:
[highlight=Java]
public class ResultList
{
private ArrayList<String> Headline = new ArrayList<String>();
private ArrayList<String> Row = new ArrayList<String>();
private ArrayList<ArrayList> Result = new ArrayList<ArrayList>();
private boolean MultipleColumn;
private ArrayList<String> SingleResult = new ArrayList<String>();
public ResultList( boolean MultipleColumn )
{
this.MultipleColumn = MultipleColumn;
}
protected void addRow()
{
if ( MultipleColumn )
{
Result.add( Row );
Row.clear();
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected void addHeadline( String Headline )
{
if ( MultipleColumn )
{
this.Headline.add(Headline);
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected void addValue( String Value )
{
if ( MultipleColumn )
{
Row.add( Value );
}
else
{
SingleResult.add( Value );
}
}
protected boolean contains( int Search )
{
return contains( String.valueOf( Search ) );
}
protected boolean contains( String Search )
{
if ( MultipleColumn )
{
for ( int i=0; i<Result.size(); i++ )
{
Row = Result.get( i );
for ( int j=0; j<Row.size(); j++ )
{
if ( Row.get( j ).equalsIgnoreCase( Search ) )
{
return true;
}
}
}
return false;
}
else
{
for ( int i=0; i<SingleResult.size(); i++ )
{
if ( SingleResult.get( i ).equalsIgnoreCase( Search ) )
{
return true;
}
}
return false;
}
}
protected int getRowSize( int Index )
{
if ( MultipleColumn )
{
return Result.get( Index ).size();
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected int getSize()
{
if ( MultipleColumn )
{
return Result.size();
}
else
{
return SingleResult.size();
}
}
protected String getValue( int RowIndex, String ColumnLabel )
{
if ( MultipleColumn )
{
Row = Result.get( RowIndex );
for ( int i=0; i<Headline.size(); i++ )
{
if ( ColumnLabel.equalsIgnoreCase( Headline.get( i ) ) )
{
return Row.get( i );
}
}
System.out.println( "Spalte nicht in ResultList vorhanden." );
return null;
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected String getValue( int RowIndex, int ColIndex )
{
if ( MultipleColumn )
{
Row = Result.get( RowIndex );
return Row.get( ColIndex );
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected String getValue( int RowIndex )
{
if ( MultipleColumn )
{
throw new UnsupportedOperationException( "This method is not supported for multiple-column ResultLists." );
}
else
{
return SingleResult.get( RowIndex );
}
}
}
[/highlight]
ResultList wird dann in einer Methode getQueryResult gefüllt und zurückgegeben.
Funktioniert auch soweit. Nur wäre es mir natürlich lieber, das ganze etwas "eleganter" zu gestalten, d.h. ohne die Exceptions.
Würde gern eine Oberklasse ResultList mit den Unterklassen SingleColumnResultList und MultipleColumnResultList basteln. So wie im folgenden Code stelle ich mir den Aufruf vor. Nur leider kann ich dort die unterschiedlichen Methoden der beiden Unterklassen nicht so verlagern, wie ich das möchte. Der Kompiler nimmt ja an, es mit der (mit lediglich den gemeinsamen Methoden bestückten) Oberklasse zu tun haben...
[highlight=Java]
protected ResultList getQueryResult( String Query, boolean MultipleCols )
{
ResultList List;
if ( MultipleCols )
{
List = new MultipleColumnResultList();
}
else
{
List = new SingleColumnResultList();
}
List.getRowSize; // Fehler, da getRowSize in der "neuen" Version nur noch in der Unterklasse Multi... auftaucht.
return List;
}
ResultList myList = getQueryResult( "select sysdate from dual", false);
[/highlight]
Hoffe, die Informationen reichen, um mir ein paar Tipps zu geben. (Kritik am Code ist natürlich auch erwünscht, bin ja am Lernen
)
Danke im Voraus.
MfG
Mirko
habe folgendes Problem:
Ich möchte gern ResultSets sofort in eine ArrayList ablegen, um den Datenbankzugriff zu minimieren. Dazu habe ich mir eine Klasse ResultList geschrieben:
[highlight=Java]
public class ResultList
{
private ArrayList<String> Headline = new ArrayList<String>();
private ArrayList<String> Row = new ArrayList<String>();
private ArrayList<ArrayList> Result = new ArrayList<ArrayList>();
private boolean MultipleColumn;
private ArrayList<String> SingleResult = new ArrayList<String>();
public ResultList( boolean MultipleColumn )
{
this.MultipleColumn = MultipleColumn;
}
protected void addRow()
{
if ( MultipleColumn )
{
Result.add( Row );
Row.clear();
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected void addHeadline( String Headline )
{
if ( MultipleColumn )
{
this.Headline.add(Headline);
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected void addValue( String Value )
{
if ( MultipleColumn )
{
Row.add( Value );
}
else
{
SingleResult.add( Value );
}
}
protected boolean contains( int Search )
{
return contains( String.valueOf( Search ) );
}
protected boolean contains( String Search )
{
if ( MultipleColumn )
{
for ( int i=0; i<Result.size(); i++ )
{
Row = Result.get( i );
for ( int j=0; j<Row.size(); j++ )
{
if ( Row.get( j ).equalsIgnoreCase( Search ) )
{
return true;
}
}
}
return false;
}
else
{
for ( int i=0; i<SingleResult.size(); i++ )
{
if ( SingleResult.get( i ).equalsIgnoreCase( Search ) )
{
return true;
}
}
return false;
}
}
protected int getRowSize( int Index )
{
if ( MultipleColumn )
{
return Result.get( Index ).size();
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected int getSize()
{
if ( MultipleColumn )
{
return Result.size();
}
else
{
return SingleResult.size();
}
}
protected String getValue( int RowIndex, String ColumnLabel )
{
if ( MultipleColumn )
{
Row = Result.get( RowIndex );
for ( int i=0; i<Headline.size(); i++ )
{
if ( ColumnLabel.equalsIgnoreCase( Headline.get( i ) ) )
{
return Row.get( i );
}
}
System.out.println( "Spalte nicht in ResultList vorhanden." );
return null;
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected String getValue( int RowIndex, int ColIndex )
{
if ( MultipleColumn )
{
Row = Result.get( RowIndex );
return Row.get( ColIndex );
}
else
{
throw new UnsupportedOperationException( "This method is not supported for single-column ResultLists." );
}
}
protected String getValue( int RowIndex )
{
if ( MultipleColumn )
{
throw new UnsupportedOperationException( "This method is not supported for multiple-column ResultLists." );
}
else
{
return SingleResult.get( RowIndex );
}
}
}
[/highlight]
ResultList wird dann in einer Methode getQueryResult gefüllt und zurückgegeben.
Funktioniert auch soweit. Nur wäre es mir natürlich lieber, das ganze etwas "eleganter" zu gestalten, d.h. ohne die Exceptions.
Würde gern eine Oberklasse ResultList mit den Unterklassen SingleColumnResultList und MultipleColumnResultList basteln. So wie im folgenden Code stelle ich mir den Aufruf vor. Nur leider kann ich dort die unterschiedlichen Methoden der beiden Unterklassen nicht so verlagern, wie ich das möchte. Der Kompiler nimmt ja an, es mit der (mit lediglich den gemeinsamen Methoden bestückten) Oberklasse zu tun haben...
[highlight=Java]
protected ResultList getQueryResult( String Query, boolean MultipleCols )
{
ResultList List;
if ( MultipleCols )
{
List = new MultipleColumnResultList();
}
else
{
List = new SingleColumnResultList();
}
List.getRowSize; // Fehler, da getRowSize in der "neuen" Version nur noch in der Unterklasse Multi... auftaucht.
return List;
}
ResultList myList = getQueryResult( "select sysdate from dual", false);
[/highlight]
Hoffe, die Informationen reichen, um mir ein paar Tipps zu geben. (Kritik am Code ist natürlich auch erwünscht, bin ja am Lernen
Danke im Voraus.
MfG
Mirko