JAVA - 查询 MS Access DB 后如何使用 ResultSet 的内容填充 JList

发布于 2024-10-22 10:40:34 字数 993 浏览 2 评论 0原文

private void populateJlist(){
    try
    {
        Connection connection = newConnection.createConnection();
        Statement newStat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet res = newStat.executeQuery("SELECT AccName FROM AccList");

                String data = "";
        while (res.next()){
            data += res.getString("AccName") + " ";
        }
        String acclist[] = data.split(" ");
        AccountList = new JList(acclist);
        newStat.close();
        connection.close();

    }
    catch(SQLException e){
        System.err.println("SQLException: " + e.getMessage());
    }

}

应该做什么 - 该方法将从 AccList 表中查询 DB AccName 列,获取所有帐户名称并将它们插入到 Jlist 中

我需要什么帮助 - 我需要帮助实现 Jlist,该 Jlist 将填充 DB 列中的字段。

我尝试通过在线搜索帮助,最接近的解决方案就是我上面所做的。

我了解到有两种可能的方法

Resultset ->数组->吉利斯特 结果集->矢量-> Jlist

但是我也不知道如何进行这些转换。我想说我是一个JAVA新手。任何详细的帮助表示赞赏。

private void populateJlist(){
    try
    {
        Connection connection = newConnection.createConnection();
        Statement newStat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet res = newStat.executeQuery("SELECT AccName FROM AccList");

                String data = "";
        while (res.next()){
            data += res.getString("AccName") + " ";
        }
        String acclist[] = data.split(" ");
        AccountList = new JList(acclist);
        newStat.close();
        connection.close();

    }
    catch(SQLException e){
        System.err.println("SQLException: " + e.getMessage());
    }

}

WHAT IS SHOULD DO - The method will query the DB AccName column from AccList Table and get all account names and insert them onto a Jlist

WHAT I NEED HELP WITH - I need help implementing a Jlist which will be filled with fields from a DB column.

I tried by searching online for help and the nearest to a solution was what I manage to do above.

I learnt that there are 2 possible ways

Resultset -> Array -> Jlist
Resultset -> Vector -> Jlist

however I dont know how to do those conversions either. I would say I am a new beginner in JAVA. Any detailed help is appreciated.

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

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

发布评论

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

评论(1

摇划花蜜的午后 2024-10-29 10:40:34

正如你所说,使用数组或向量会比你的分割解决方案更干净、更容易。就我个人而言,我建议使用 Vector。基本上,您想要创建一个空向量,迭代结果集,通过 Vector.add(Object o) 方法将每个值添加到向量中,然后使用该向量在循环外部创建 jlist。像这样的东西(警告:未经测试)

Vector<String> temp = new Vector<String>();
while (res.next()) {
    temp.add(res.getString("AccName"));
}
Jlist acctList = new Jlist(temp);

As you say, using either an array or a vector would be cleaner and easier than your split solution. Personally, I would suggest the Vector. Basically, you want to create an empty vector, iterate over the result set adding each value to the vector via the Vector.add(Object o) method, then create the jlist outside the loop with the vector. Something like this (warning: untested)

Vector<String> temp = new Vector<String>();
while (res.next()) {
    temp.add(res.getString("AccName"));
}
Jlist acctList = new Jlist(temp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文