我的程序查询波段和CD波段上的MySQL数据库,并将查询作为结果集返回。我尝试将CD表查询的结果集存储到一个数组列表中,这样就可以存储每张CD的标题和年份,然后通过addCD( cd )将其添加到当前的乐队中。我的Band类有一个CD对象的数组列表,我的CD对象有一个字符串表示标题,int表示年份。
发布于 2013-05-29 05:37:17
您可以在单个SQL查询中从CD表中选择标题和年份值,如下所示。然后,YEAR将在结果集的索引(2)处变为可用。使用包装类Integer将年份字符串转换为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 (单数)相比,调用bands列表、bandList甚至bands使变量的用途更清晰。
https://stackoverflow.com/questions/16801753
复制相似问题