Storing Result set into an array

前端 未结 2 1225
闹比i
闹比i 2020-12-17 00:51

i know this should be simpel and im probably staring straight at the problem but once again im stuck and need the help of the code gurus.

im trying too take one row

相关标签:
2条回答
  • 2020-12-17 01:36

    Did you mean something like:

    int i = 0;
    ResultSet rs = stmt.executeQuery("select name from users");
    while (rs.next()) {
        String name = rs.getString("name");
        names[i++] = name;
    }
    
    0 讨论(0)
  • 2020-12-17 01:42

    You should use an ArrayList which provides all the logic to automatically extend the array.

    List rowValues = new ArrayList();
    while (namesList.next()) {
        rowValues.add(namesList.getString(1));
    }   
    // You can then put this back into an array if necessary
    contactListNames = (String[]) rowValues.toArray(new String[rowValues.size()]);
    
    0 讨论(0)
提交回复
热议问题