将结果集存储到数组中 [英] Storing Result set into an array

查看:129
本文介绍了将结果集存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这应该是简单的,我可能直接看到这个问题,但我再一次陷入困境,需要代码大师的帮助。

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.

我也试着jdbc中一列的一行,并将它们放在一个数组中。

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

我这样做如下:

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是一个已经定义的方法,可以完成它显然做的事情。
我创建我的结果集
而另一个,
我将该列的字符串存储到数组中。
然后打印出来以获得良好的衡量标准。也确保它在那里。

creatConnection is an already defined method that does what it obviously does. i creat my result set while theres another one, i store the string of that column into an array. then print it out for good measure. too make sure its there.

问题在于它将整个列存储到contactListNames [1]

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

我想让它存储column1第1行进入[1]

i wanted to make it store column1 row 1 into [1]

然后第1列第2行进入[2]

then column 1 row 2 into [2]

我知道我可以用循环做这个。但我不知道从一列中一次只能一行。任何想法?

i know i could do this with a loop. but i dont know too take only one row at a time from a single column. any ideas?

ps我读过api,我不能看到任何合适的东西。

p.s ive read the api, i jsut cant see anything that fits.

推荐答案

你应该使用 ArrayList ,它提供了自动扩展数组的所有逻辑。

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

这篇关于将结果集存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆