2014-01-07 60 views
0

得到一個結果我有一個ResultSet對象如何從外部Java類

ResultSet theInfo = stmt.executeQuery(sqlQuery); 

,但我希望有一個可以從另一個Java類

public Vector loadStuff(){ 
    try { 
     while (theInfo.next()){ 
      aVector.addElement(new String(theInfo.getString("aColumn"))); // puts results into vectors 
     } 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    return aVector; 
} 

我調用的函數不完全確定如何去做這件事。我想要一些如何調用返回填充矢量的無效方法。這可能嗎?

+2

將向量作爲參數傳遞。 –

+0

爲什麼不使用[DAO方法](http://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm)? –

回答

1

假設您有一個Demo類,並且它的方法getVector遵循給定的方法。

class Demo { 

public Vector getVector(ResultSet theInfo) { 
    if(theInfo==null){ 
     throw new IllegalArgumentException("ResultSet is null"); 
    } 
    Vector aVector = new Vector(); 
    try { 
     while (theInfo.next()) { 
      aVector.addElement(new String(theInfo.getString("aColumn"))); 
     } 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    return aVector; 
} 

}

現在所說的getVector後得到的結果集。

ResultSet theInfo = stmt.executeQuery(sqlQuery); 

Demo demo =new Demo(); 

Vector vector=demo.getVetor(theInfo); 
+0

謝謝你,簡單的代碼,我喜歡它 – wjhplano