简体   繁体   English

在数组列表Java中存储结果集

[英]Storing resultset in an arraylist Java

My program queries a MySQL database on the band and CD band, and return the query as a resultset. 我的程序在band和CD频段上查询MySQL数据库,并将查询作为结果集返回。 I am trying to store the resultset of the CD table query into an arraylist so the Title and Year of each CD is stored and then added to the current Band through addCD(CD cd). 我试图将CD表查询的结果集存储到arraylist中,以便存储每个CD的标题和年份,然后通过addCD(CD cd)添加到当前Band。 My Band class has an arraylist of CD objects, and my CD objects has a String for the title, and int for the year. 我的Band类具有CD对象的数组列表,而我的CD对象具有用于标题的字符串和用于年份的int。

You can select TITLE and YEAR values from CD table in a single SQL query as below. 您可以在单个SQL查询中从CD表中选择TITLE和YEAR值,如下所示。 YEAR would then become available at index(2) of the resultset . 然后,YEAR将在结果集的索引(2)处可用。 Use the wrapper class Integer to convert the YEAR string to an int . 使用包装器类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

I've taken the liberty of changing a few names. 我可以自由更改一些名称。 Basically, calling a list of bands, bandList or even bands makes the purpose of the variable more clearer than calling it a band (singular). 基本上,调用band列表, bandList或什至bands会使变量的目的比将其称为band (单数)更清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM