ResultSet to Array

时间:2016-02-21 12:04:35

标签: java android jdbc

我有一个使用查询返回的结果集:

String query = "select Bookname, SubjectName from books join Subjects on Subjects.SubjectID = Books.subjectID where classID = '1a'";
ResultSet temp = null;
try
{
   Statement st = conn.createStatement();
   ResultSet rs = st.executeQuery(query);

   temp = rs;
}

我只是想知道是否可以将Resultset转换为两个单独的数组:例如BookName []和BookSubject []所以我可以稍后在列表视图中显示它们?结果集和android中的这个概念相对较新

1 个答案:

答案 0 :(得分:4)

您应该能够轻松地遍历结果集上的结果,并在您进行交互时使用结果填充每个数组。

这样的事情:

...
ResultSet rs = st.executeQuery(query);

ArrayList<String> names = new ArrayList<String>();
ArrayList<String> subjects = new ArrayList<String>();

while (rs.next()) { 
    names.add(rs.getString(1));
    subjects.add(rs.getString(2));
}

// finally turn the array lists into arrays - if really needed
String[] nameArr = new String[names.size()];
nameArr = names.toArray(nameArr);

String[] subjectArr = new String[subjects.size()];
subjectArr = subjects.toArray(subjectArr);

希望有所帮助!