将数据库表名添加到java中的JList中

发布于 2024-09-01 23:10:02 字数 966 浏览 3 评论 0原文

// Declare JList
private JList jlstTab, jlstCol;
.
.
.
DefaultListModel dlmTables = new DefaultListModel();
DefaultListModel dlmCol = new DefaultListModel();

        // Instantiate 
        dlmTables.addElement("kl");
        jlstTab= new JList(dlmTables);
     jlstTab.setSelectedIndex(0);
        jlstTab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

.
.
.
.

//Connect to the database
public static void main(String args[])
    {
   DBToolSwing cs = new DBToolSwing("DB Tool Swing");
   try


                  DBAccessObject dbAccess1 = new DBAccessObject("jdbc:odbc:JavaClassDSN");
          DBAccessObject dbAccess2 = new DBAccessObject();
                  ResultSet rsTables = dbAccess1.getDatabaseTableNames();
          while (rsTables.next())
          System.out.println(rsTables.getString("TABLE_NAME"));

我需要从数据库获取表名,输出不应打印在屏幕上,而是需要将输出添加到 JlstTab 所以 dlmTables.addElement("TABLE_NAME"); 如果有人可以帮助我,我将不胜感激。提前致谢。

// Declare JList
private JList jlstTab, jlstCol;
.
.
.
DefaultListModel dlmTables = new DefaultListModel();
DefaultListModel dlmCol = new DefaultListModel();

        // Instantiate 
        dlmTables.addElement("kl");
        jlstTab= new JList(dlmTables);
     jlstTab.setSelectedIndex(0);
        jlstTab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

.
.
.
.

//Connect to the database
public static void main(String args[])
    {
   DBToolSwing cs = new DBToolSwing("DB Tool Swing");
   try


                  DBAccessObject dbAccess1 = new DBAccessObject("jdbc:odbc:JavaClassDSN");
          DBAccessObject dbAccess2 = new DBAccessObject();
                  ResultSet rsTables = dbAccess1.getDatabaseTableNames();
          while (rsTables.next())
          System.out.println(rsTables.getString("TABLE_NAME"));

I need to get the table names from the database, the output shouldn't be printed on the screen, instead I need the output added to the JlstTab
so dlmTables.addElement("TABLE_NAME");
Please if someone can help I would appreciate it. Thanks in advance.

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

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

发布评论

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

评论(1

心房的律动 2024-09-08 23:10:02

假设这两个片段都适合您。我的意思是您可以在控制台上打印表名称,并且还可以在 JList 中显示一些固定值。那么,为什么不使用 dlmTables.addElement(rsTables.getString("TABLE_NAME")) 来代替 System.out.println(rsTables.getString("TABLE_NAME")) 。问题出在哪里?

Assuming both snippet is working for you. I mean you are able to print the table names on the console, and you are also able to show some fixed value in your JList. So, why not, instead of System.out.println(rsTables.getString("TABLE_NAME")) use dlmTables.addElement(rsTables.getString("TABLE_NAME")). Where is the problem?

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