2011-06-14 39 views
3

我有我從我的數據庫中提取一個結果:如何把一個結果爲Array使用多次

 String selStmt = "SELECT code_id, internal_code, representation, pos, decode(substr(internal_code, 5, 1), 'Q', 2, 1) as sorter, " 
       + "  to_char(term_start, 'MM-DD-YYYY') as sDate " 
       + " FROM table1, terms WHERE code_id = 'SEARCH.TERMS' AND internal_code = terms.terms_id (+) ORDER BY 5, 4 "; 

    stmt = conn.prepareStatement(selStmt); 
    result = stmt.executeQuery(); 

我想現在要做的就是把這些結果到一個數組,我知道我可以使用while循環來循環並獲得結果,但是我只能使用那些,我需要在整個頁面的其餘部分繼續使用它們。我想這可以使用數組來完成,但是如果有更簡單的方法,請告訴我,如果需要更多信息,請讓我知道。

回答

1

沒有我知道的用於Java的DataSet(.NET)。因此,可以考慮類似如下:

ArrayList<Integer> data = new ArrayList<Integer>(); 

ResultSet rs = ....; 

// For each record in the result...  
while (rs.next()) { 
    // Want more values? Create a custom Type representing the Row! 
    // The following is just an example taking the first column (as an int). 
    // This would be done manually for each column... and possibly loadeded 
    // in a custom object which is then added to the list... blah blah. 
    // (Alternatively each row could be represented as Object[] or List<Object> 
    // at the expense of losing static typing.) 
    int id = rs.getInt(0); 
    data.add(id); 
} 

// Then later, if you *really* want an array... 
// (Java is such a backwards language and lacks a trivial way 
// to go to int[] from Integer[] but I digress...) 
Integer[] array = data.toArray(new Integer[0]); 

快樂編碼

+0

這不會工作,因爲getInt()必須以1而不是0 – Timo 2014-05-31 13:28:16

0

一個數組可能是實現它的方法。在你的while循環之前初始化它,在while循環中填充它,然後使用它:)

+0

好吧,我不確定的部分是「在你的while循環中填充它」我可以初始化,我只是不能得到填充的數組工作 – Dan 2011-06-14 18:55:45

2

既然你不知道事先的行數,我實際上會選擇ArrayList。然後,您可以隨意插入自定義對象(從結果集構建)。

1

您可以將數據存儲在二維數組中。但更友好的方法是創建一個對象來表示一行數據所代表的內容,並從ResultSet創建這些對象的列表。

要拉取數據,只需循環遍歷ResultSet,然後爲每個循環創建一個新的自定義對象實例並將其添加到列表中。

1

的標準方法是遍歷ResultSet中一次和(可能)的每個記錄存儲在數組中哪位以後可以遍歷並隨你喜歡。這就是大多數的答案,這是正確的。

相關的一點就是:你(顯然)喜歡保持在每個next()調用ResultSet返回(這樣就可以每個記錄存儲在一個ArrayList「原始」的記錄,並把它作爲一個滾動 ResultSet或類別)。我認爲不能這樣做,我堅信,在大多數情況下,這是一個壞主意。 ResultSet屬於JDBC層,它是一個相當低層的層,它有它的問題和缺陷。一般來說,ResultSet應儘快完成並關閉,不應泄漏到上層。對於「完成」,我的意思是將它循環併爲每個curson位置構建自己的Java對象(使用與JDBC api不相關的方法)。這就是大多數人和框架(例如iBatis)所做的事情,而且在大多數情況下,這樣做是不好的做法。

例如,我曾經被看做是(僞代碼)實施了一些僞道:

public ResultSet getUsers() { 
     conn = openConnection(); 
     stmt = conn.prepareStatement(...); 
     result = stmt.executeQuery(); 
     stmt.close(); 
     conn.close(); 
     return result; 
    } 

,主叫方將隨後遍歷結果集。這太可怕了,可能會失敗,因爲結果集可能會嘗試從數據庫獲取數據(但連接已關閉!)。

如果你真的想這樣做,你應該看看RowSet類。但我認爲它沒有多大用處。

1

有一次我用下面的代碼來解決類似的挑戰:

Vector rows = new Vector(); 
Vector nrow; 
int cnt = 0; 

while(result.next()) 
{ 
    nrow=new Vector(); 
    cnt+=1; 
    nrow.addElement(String.valueOf(cnt)); 
    for(int i=1;i<=3;i++) //replace 3 with the length of the columns 
    { 
     nrow.addElement(result.getObject(i)); 
    } 
    rows.addElement(nrow); 
} 

現在,你可以通過行循環,並利用數據的,只要你喜歡。每次迭代返回的每個對象都包含整個行數據。

0

如果您需要在整個頁面中不斷引用它們,則另一種可能性是使用HashMap存儲行。爲索引使用數據庫的主鍵。

這樣您可以根據需要快速引用特定的行。如果你只需要循環遍歷結果集,而不是像剛纔所說的那樣在這裏工作的很好。