【问题标题】:How to Retrieve Another Statement Records Using HashTable?如何使用 HashTable 检索另一条语句记录?
【发布时间】:2013-07-11 15:44:38
【问题描述】:

晚上好,我尝试使用 HashTable() 来临时存储数据库表记录。问题是我不知道为什么放在 HashTable() 中的记录总是第一条记录。我认为问题的出现是因为错误的循环概念,相关代码:

声明

Hashtable hashsample = new Hashtable();

for循环

for (i = 0; i< db.getNumberOfRows(); i++) {

        hashsample.put(i, db.getData());
        System.out.println(hashsample);
    }

p/s:我是 HashTable 的新手,db(数据库语句)运行良好...

需要一些提示和建议,谢谢先进^^

【问题讨论】:

  • 首先以Map 开头。你如何遍历数据库结果集?
  • 我的代码没有问题。可能是您验证的方式不正确。尝试打印元素而不是哈希表,即 System.out.println(hashsample.get(i));
  • 谢谢...我解决了它,这是因为我的愚蠢错误...应该将 hashsample.put(i, db.getData()) 更改为 ("key", db.getDataAtRow (i))

标签: java for-loop hashtable language-concepts


【解决方案1】:

我试过你的代码来模拟数据库:

public static void main(String[] args) {

    HashMap db = new HashMap();
    db.put(0, "zero");
    db.put(1, "one");
    db.put(2, "two");
    db.put(3, "three");

    Hashtable hashsample = new Hashtable();

    for (int i = 0; i < db.size(); i++) {

            hashsample.put(i, db.get(i));
            System.out.println(hashsample);
    }

}

它工作得很好。这里的输出:

{0=zero}
{1=one, 0=zero}
{2=two, 1=one, 0=zero}
{3=three, 2=two, 1=one, 0=zero}

我认为问题是您的 db.getData() 返回的总是相同的东西。

【讨论】:

  • 是的,我还需要做更多的功课才能掌握HashTable()... 打心底里的TQ^^
猜你喜欢
  • 2019-05-04
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多