从DataBase获取名称并在带有java的choiceBox中使用它们

时间:2021-10-12 17:04:34

I'm Struggling with getting the Values of a Column called 'Names' in my Database. I also need them to be insered into an Observabllelist so I can use them as options in a choiceBox.

我正在努力获取我的数据库中名为“名称”的列的值。我还需要将它们插入到Observabllelist中,这样我就可以将它们用作choiceBox中的选项。

@FXML
        ObservableList GetTournamentsName() throws SQLException {

            ObservableList<String> optionList = null;

            Connection con = DBconnection.getConnection();
            Statement st = con.createStatement();
            String sql = ("SELECT Name FROM `tournaments`");
            ResultSet rs = st.executeQuery(sql);

            if (rs.next()) {
                optionList =  FXCollections.observableArrayList(rs.getString("Name"));
                con.close();
            }
            return optionList;
        }

and then I initialize the scene of my application by assigning to the Choicbox TournamentsOption the Values that I get from the GetTournamentsName() method.

然后我通过为Choicbox TournamentsOption指定从GetTournamentsName()方法获得的值来初始化我的应用程序的场景。

@FXML
     void initialize() throws SQLException {
        TournamentsOption.setItems(GetTournamentsName());
     }

The code Works just partially, I infact get the first Name in the Table but not all the other ones. So that is what I am asking about, how do I get ALL the values? Thank you in advance for all the help!

代码只是部分工作,我实际上得到了表中的第一个名称而不是所有其他名称。这就是我要问的问题,如何获得所有值?提前感谢您的帮助!

2 个解决方案

#1


0  

You need to declare and initialize your optionList first. Then you add the data to it. Your approach will only ever have one data String in it. You also closed your connection in the wrong place.

您需要先声明并初始化您的optionList。然后您将数据添加到它。您的方法只会有一个数据字符串。您还在错误的地方关闭了连接。

   @FXML
    ObservableList GetTournamentsName() throws SQLException {

        ObservableList<String> optionList = FXCollections.observableArrayList();

        Connection con = DBconnection.getConnection();
        Statement st = con.createStatement();
        String sql = ("SELECT Name FROM `tournaments`");
        ResultSet rs = st.executeQuery(sql);

        while(rs.next()) {
            optionList.add(rs.getString("Name")));

        }

        con.close();
        return optionList;
    }

#2


2  

In here you have a if (rs.next()) { it will not iterate for rs.next() You need to make it while (rs.next()) { and changed the adding logic as required

在这里你有一个if(rs.next()){它不会迭代rs.next()你需要在while(rs.next()){并根据需要更改添加逻辑

#1


0  

You need to declare and initialize your optionList first. Then you add the data to it. Your approach will only ever have one data String in it. You also closed your connection in the wrong place.

您需要先声明并初始化您的optionList。然后您将数据添加到它。您的方法只会有一个数据字符串。您还在错误的地方关闭了连接。

   @FXML
    ObservableList GetTournamentsName() throws SQLException {

        ObservableList<String> optionList = FXCollections.observableArrayList();

        Connection con = DBconnection.getConnection();
        Statement st = con.createStatement();
        String sql = ("SELECT Name FROM `tournaments`");
        ResultSet rs = st.executeQuery(sql);

        while(rs.next()) {
            optionList.add(rs.getString("Name")));

        }

        con.close();
        return optionList;
    }

#2


2  

In here you have a if (rs.next()) { it will not iterate for rs.next() You need to make it while (rs.next()) { and changed the adding logic as required

在这里你有一个if(rs.next()){它不会迭代rs.next()你需要在while(rs.next()){并根据需要更改添加逻辑