将结果集存储到数组中

发布于 2024-08-30 12:50:35 字数 1182 浏览 4 评论 0原文

我知道这应该很简单,我可能会直视问题,但我再次陷入困境,需要代码专家的帮助。

我也尝试从 jdbc 中的一列中取出一行,并将它们放入一个数组中。

我这样做如下:

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 是一个已经定义的方法,它显然可以完成它的任务。 我创建我的结果集 虽然还有另外一个, 我将该列的字符串存储到一个数组中。 然后打印出来以备不时之需。也要确保它在那里。

问题是它将整个列存储到 contactListNames[1] 中,

我想让它将第 1 行第 1 行存储到 [1] 中,

然后将第 1 列第 2 行存储到 [2] 中,

我知道我可以通过循环来做到这一点。但我也不知道一次只从一列中取出一行。有什么想法吗?

PS 我读过 API,但我看不到任何合适的东西。

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 from a column in jdbc, and put them in an array.

i do this 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 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.

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

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-09-06 12:50:35

您应该使用 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()]);

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()]);
梦里寻她 2024-09-06 12:50:35

你的意思是不是这样:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文