簡體   English   中英

在數組列表Java中存儲結果集

[英]Storing resultset in an arraylist Java

我的程序在band和CD頻段上查詢MySQL數據庫,並將查詢作為結果集返回。 我試圖將CD表查詢的結果集存儲到arraylist中,以便存儲每個CD的標題和年份,然后通過addCD(CD cd)添加到當前Band。 我的Band類具有CD對象的數組列表,而我的CD對象具有用於標題的字符串和用於年份的int。

您可以在單個SQL查詢中從CD表中選擇TITLE和YEAR值,如下所示。 然后,YEAR將在結果集的索引(2)處可用。 使用包裝器類Integer將YEAR字符串轉換為int

rs = st.executeQuery("SELECT TITLE, YEAR FROM CD WHERE BAND_ID = " + i + ";");

// notice the name change below
Band band = new Band(name); // or, call this "b" to avoid name conflict

// Get the title of each CD for the current band
while (rs.next()) {
    band.addCD(
        new CD(rs.getString(1), Integer.parseInt(rs.getString(2))); // int year;
}

// Add a new band to the arraylist
bandList.add(band); // Added "List" to the name

我可以自由更改一些名稱。 基本上,調用band列表, bandList或什至bands會使變量的目的比將其稱為band (單數)更清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM