将结果集存储到数组中
我知道这应该很简单,我可能会直视问题,但我再次陷入困境,需要代码专家的帮助。
我也尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 ArrayList 来提供自动扩展数组的所有逻辑。
You should use an
ArrayList
which provides all the logic to automatically extend the array.你的意思是不是这样:
Did you mean something like: