Saving the result specified in the array

I know this should be simpel, and I probably look directly at the problem, but get stuck again and need the help of a code guru.

im also trying to take one row from a column in jdbc and put them in an array.

I do it as follows:

public void fillContactList()
       {
           createConnection();
           try
           {
               Statement stmt = conn.createStatement();
               ResultSet namesList = stmt.executeQuery("SELECT name FROM Users");
               try
               {
                   while (namesList.next())
                   {
                       contactListNames[1] = namesList.getString(1);
                       System.out.println("" + contactListNames[1]);
                   }   
               }
               catch(SQLException q)
               {

               }
               conn.commit();
               stmt.close();
               conn.close();
           }
           catch(SQLException e)
           {

           }

creatConnection is an already defined method that does what it explicitly does. i creat is my result set while theres another, I store the row of this column in an array. then print it for a good grade. make sure he's there too.

the problem is that its storing the entire column in contactListNames [1]

I wanted him to save the row of column 1 in [1]

then column 1 row 2 in [2]

, . , . ?

p.s ive api, jsut can not , .

+5
2

ArrayList, .

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()]);
+20

- :

int i = 0;
ResultSet rs = stmt.executeQuery("select name from users");
while (rs.next()) {
    String name = rs.getString("name");
    names[i++] = name;
}
0

All Articles