Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-从数据库读取数据,并根据信息制作label+按钮_Java_Database_Loops - Fatal编程技术网

Java-从数据库读取数据,并根据信息制作label+按钮

Java-从数据库读取数据,并根据信息制作label+按钮,java,database,loops,Java,Database,Loops,我要请你帮个大忙 我认为当它打开时,它应该显示数据库中的每一种饮料,并在屏幕上显示出来 它还必须添加+按钮、旁边的金额标签和-按钮。每个项目都应该这样做 顺便说一下,我从中获取项目的表称为dhh_项目 现在,我有了这个: public ArrayList<Item> getBeverages(Item item) { ArrayList<Item> items = new ArrayList<>(); if (item != null) {

我要请你帮个大忙

我认为当它打开时,它应该显示数据库中的每一种饮料,并在屏幕上显示出来

它还必须添加+按钮、旁边的金额标签和-按钮。每个项目都应该这样做

顺便说一下,我从中获取项目的表称为dhh_项目

现在,我有了这个:

public ArrayList<Item> getBeverages(Item item) {
    ArrayList<Item> items = new ArrayList<>();

    if (item != null) {
        // First open a database connnection
        DatabaseConnection connection = new DatabaseConnection(); 
        if (connection.openConnection()) {
            // If a connection was successfully setup, execute the SELECT statement.
            ResultSet resultset = connection.executeSQLSelectStatement(
                    "SELECT * FROM dhh_item ");

            if (resultset != null) {
                try {
                    while (resultset.next()) {
                        String itemName = resultset.getString("itemName");
                        String status = resultset.getString("status");
                        String description = resultset.getString("description");
                        int price = resultset.getInt("price");

                        Item newItem = new Item(itemName, status, description, price);

                        items.add(newItem);
                    }
                } catch (SQLException e) {
                    System.out.println(e);
                    items.clear();
                }
            }
            // else an error occurred leave array list empty.

            // We had a database connection opened. Since we're finished,
            // we need to close it.
            connection.closeConnection();
        }
    }
    return items;
}
这在任何方面都是正确的。我会检索任何数据吗?.getString是正确的

现在,这个方法在另一个类ItemDAO中

从我的观点来看,我能称之为“这”吗?我如何让它为每个标签制作一个新的标签+按钮

非常感谢那些能在这件事上帮助我的人

最后,应该是这样的:


对于桌上的每一种饮料。

听起来有点离谱

Collection<Item> items=dao.getBeverages(someItem) // get all items
    for(Item item:items){
        label=new JLabel(item.getYourItemNameOrLabelOrhatever) // this will be the "coca-cola"
        incButton=new JButton(incrementButtonAction); // craete/get some action 
        decButton=new JButton(decrementButtonAction); // same here
        counter=new JLabel("0");
        yourContainer.add(label);
        yourContainer.add(incButton);
        yourContainer.add(label);
        yourContainer.add(decButton);
        yourContainer.revalidate();
}

听起来有点超前

Collection<Item> items=dao.getBeverages(someItem) // get all items
    for(Item item:items){
        label=new JLabel(item.getYourItemNameOrLabelOrhatever) // this will be the "coca-cola"
        incButton=new JButton(incrementButtonAction); // craete/get some action 
        decButton=new JButton(decrementButtonAction); // same here
        counter=new JLabel("0");
        yourContainer.add(label);
        yourContainer.add(incButton);
        yourContainer.add(label);
        yourContainer.add(decButton);
        yourContainer.revalidate();
}

你在用秋千??你在用秋千??