Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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在choiceBox中使用它们_Java_Mysql_Database_Javafx_Scenebuilder - Fatal编程技术网

从数据库中获取名称,并使用java在choiceBox中使用它们

从数据库中获取名称,并使用java在choiceBox中使用它们,java,mysql,database,javafx,scenebuilder,Java,Mysql,Database,Javafx,Scenebuilder,我正在努力获取数据库中名为“name”的列的值。我还需要将它们插入到可观察列表中,以便将它们用作选择框中的选项 @FXML ObservableList GetTournamentsName() throws SQLException { ObservableList<String> optionList = null; Connection con = DBconnection.getConnection();

我正在努力获取数据库中名为“name”的列的值。我还需要将它们插入到
可观察列表中
,以便将它们用作
选择框中的选项

@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;
        }
代码只是部分工作,实际上我得到了表中的名字,但不是所有其他名字。这就是我要问的,我如何得到所有的值?
提前感谢您的帮助

这里有一个
if(rs.next()){
它不会对
rs.next()进行迭代

您需要在(rs.next()){
时将其设置为
,并根据需要更改添加逻辑

在这里您有一个
如果(rs.next()){
它将不会对
rs.next()进行迭代

您需要在(rs.next()){
时将其设置为
,并根据需要更改添加逻辑

您需要首先声明并初始化您的选项列表。然后将数据添加到其中。您的方法将只包含一个数据字符串。您还在错误的位置关闭了连接

   @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;
    }
@FXML
ObservableList GetTournamentsName()引发SQLException{
ObservableList optionList=FXCollections.observableArrayList();
Connection con=DBconnection.getConnection();
语句st=con.createStatement();
字符串sql=(“从'tournaments'中选择名称”);
结果集rs=st.executeQuery(sql);
while(rs.next()){
optionList.add(rs.getString(“Name”));
}
con.close();
返回选项列表;
}

您需要先声明并初始化您的选项列表。然后将数据添加到其中。您的方法中只包含一个数据字符串。您也在错误的位置关闭了连接

   @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;
    }
@FXML
ObservableList GetTournamentsName()引发SQLException{
ObservableList optionList=FXCollections.observableArrayList();
Connection con=DBconnection.getConnection();
语句st=con.createStatement();
字符串sql=(“从'tournaments'中选择名称”);
结果集rs=st.executeQuery(sql);
while(rs.next()){
optionList.add(rs.getString(“Name”));
}
con.close();
返回选项列表;
}

optionList=FXCollections.observableArrayList(rs.getString(“名称”);这是什么?optionList=FXCollections.observableArrayList(rs.getString(“名称”));这是什么?非常感谢塞德里克!它工作得很好,现在解决方案似乎很明显,哈哈哈!!非常感谢塞德里克!它工作得很好,现在解决方案似乎很明显,哈哈哈!!非常感谢图西塔!!非常感谢图西塔!!