【问题标题】:Parsing values from database into ComboBox将值从数据库解析到 ComboBox
【发布时间】:2017-07-17 17:29:42
【问题描述】:

嗯,我需要从数据库中获取值,然后将它们插入组合框以供选择。

听起来很简单,只需要使用 2 个类,UI 类Entity 类,其中包含所有 SQL 查询(任何与数据库有关的,都在里面):

//This is the UI class

public void fillComboBox(){

    Entity et = new Entity();

    try{
       //call out dbConnector method from Entity class
        conn = et.dbConnector();
        String query="select distinct Name from DbTable order by Name";
        PreparedStatement pst = conn.prepareStatement(query);
        ResultSet rs = pst.executeQuery();

        while(rs.next()){
           //shows topic data in combobox
            comboBoxName.addItem(rs.getString("Name"));
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
     //runs method
      fillComboBox();

现在,上面的输出运行良好,没有任何问题。在我的表单中,组合框在我的指定列中显示从我的数据库中获取的唯一值。

在其中实现另一个层时出现问题。

简而言之,我现在有三个班。

类 1:UI -> 这个类纯粹处理 UI

类 2:控制器 -> 这个类纯粹运行方法

第 3 类:实体 -> 这个类纯粹运行与 sql 数据库查询有关的任何事情

我所做的是将上面的代码修改为:

这是 UI 类:

//Declare Variables
JComboBox comboBoxName = new JComboBox();
Controller ct = new Controller();

comboBoxName.addItem(ct.fillComboBox());

以及Controller类中的某个方法:

//Declare Variables
Entity et = new Entity();

public String fillComboBox(){
    return et.takeNames();
}

最后,我的 Entity 类,其中包含所有 sql 查询。

//Declare all variables first
Connection conn = null;
String task = null, names = null;
String query;

//This method connects to database
//There's nothing wrong with this method, I just placed it here to give a general overview of what this method exactly is for you to understand, as I will be calling it out later. Yes, I removed off the **URL** portion intentionally.
public static Connection dbConnector(){     
    try{
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:URL");
        JOptionPane.showMessageDialog(null, "Connected!");
        return conn;
    }

    catch(Exception ex){
        JOptionPane.showMessageDialog(null, ex);
        return null;
    }
}

public String takeNames(){

        try{
            conn = dbConnector();
            query = "select distinct Name from DbTable order by Name";
            PreparedStatement pst = conn.prepareStatement(query);
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                //shows Name data in combobox
                 names = rs.getString("Name");
            }               
            pst.close();                
        }

        catch(Exception ex){
            ex.printStackTrace();
        }

        return names;
      }

嗯,基本上,这个“新”实现的运行方式是,UI 类 调用 Controller 类,它调用 Entity 类,它在内部运行方法并将值一直解析到 UI。

这种方法很有用,因为它将程序的不同部分分开,使其看起来更整洁。太糟糕了,实施起来很头疼。 >.>

现在,这里的错误是,它将只检索 1 个值,而不是多个值。它检索的是我指定的特定列中的第一个“不同”值。其余的“不同”值将被忽略。

我有一种预感,这与 rs 设置有关,@:

 ResultSet rs = pst.executeQuery();

我想到的是它只需要 1 个值并设置它,然后忽略其余的。有没有人对此有任何解决方案?我尝试了arraylist,但失败了如何在arraylist中存储大量的rs值(这真的难倒我>.>)

对于这篇冗长的帖子,我深表歉意,但在我被困在这部分几个小时之前,我尽了最大的努力......

【问题讨论】:

  • 你能不能做一些事情,比如在顶部插入一些你面临的问题图像。这是一堵文字墙,伙计。人们越早了解问题,您就越有可能得到答案。一张图片值 1000 字!
  • 我指定的那个特定的列 - 是具体的......
  • 编码人员不需要运行评论您是如何编写应用程序的。他们只需要查看现在的代码。他们可以理解代码。如果您提供样本数据表以及您得到的与您想要的,这将澄清事情!
  • 嗯,cmets 基本上不适合我,这是我的讲师希望我拥有的 =/

标签: java sql eclipse swing combobox


【解决方案1】:

修改takeNames()如下

public ArrayList<String> takeNames(){
        //This will collect all names from db
        ArrayList<String> names = new ArrayList<>();

        try{
            conn = dbConnector();
            query = "select distinct Name from DbTable order by Name";
            PreparedStatement pst = conn.prepareStatement(query);
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                //shows Name data in combobox
                //Add data to the array list
                 names.add(rs.getString("Name"));
            }               
            pst.close();                
        }

        catch(Exception ex){
            ex.printStackTrace();
        }

        //Return the array list you created
        return names;
      }

修改fillComboBox()如下

public ArrayList<String> fillComboBox(){
    return et.takeNames();
}

其余的修改如下

JComboBox comboBoxName = new JComboBox();
Controller ct = new Controller();
ArrayList<String> nameList = ct.fillComboBox();

for(String name : nameList){
    comboBoxName.addItem(name);        
}

【讨论】:

  • 在 UI 中声明一个数组并将其存储在其中,呵呵。好主意!这工作,谢谢!
  • 哦,对不起!我不知道我必须这样做。再次抱歉迟到了!
猜你喜欢
  • 2015-11-14
  • 2013-03-16
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
相关资源
最近更新 更多