从数据库获取组合框值

zu0ti5jz  于 2021-07-24  发布在  Java
关注(0)|答案(3)|浏览(258)

我想得到 ComboBox 来自数据库的值。我正在尝试下面的代码,它没有返回任何错误,但是没有添加到组合框中的项。

@FXML
private ComboBox<List> laptopos_combo;
public void filloscombo() throws SQLException
{
    String connectionUrl = "jdbc:sqlserver://WIN\\SQLEXPRESS:1433;database=itinventory;" + 
    "user=sa;"+ "password=Varpal@1234;";
    Connection connect= DriverManager.getConnection(connectionUrl);
    String query = "Select * from operatingsystem";

    PreparedStatement pst = connect.prepareStatement(query);
    // log.conn().prepareStatement(query);
    ResultSet os = pst.executeQuery();

    while(os.next())
    {
        ((List<String>) laptopos_combo).add(os.getString("os"));
    }
}
vnjpjtjt

vnjpjtjt1#

public ArrayList<Object> selectAllObjects() {
        ArrayList<Object> list = new ArrayList<>();
        try {
             String connectionUrl 
             ="jdbc:sqlserver://WIN\\SQLEXPRESS:1433;database=itinventory;" + 
             "user=sa;"+ "password=Varpal@1234;";
             Connection connect= DriverManager.getConnection(connectionUrl);
             Statement stmt = connect.createStatement();
             ResultSet result = stmt.executeQuery("SELECT QUERY");
             while (result.next()) {
                Object obj = new Object();
                obj.setId(result.getInt(1));
                //setters
                list.add(payment);
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } finally {
            if (connect != null) {
                try {
                    connect.close();
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
        }
        return list;
    }

然后可以调用该函数,将其保存到另一个arraylist中,并按如下方式进行设置:

comboBox.setItems(arraylist);
comboB.setValue(arraylist.get(0));
9vw9lbht

9vw9lbht2#

我认为您需要将组合框的项目类型从list更改为string,因为您要向组合框添加一个string值,请尝试以下操作:

@FXML
private ComboBox<String> laptopos_combo;
public void filloscombo() throws SQLException {
    String connectionUrl ="jdbc:sqlserver://WIN\\SQLEXPRESS:1433;database=itinventory;" +
            "user=sa;"+ "password=Varpal@1234;";
    Connection connect= DriverManager.getConnection(connectionUrl);
    String query="Select * from operatingsystem";

    PreparedStatement pst = connect.prepareStatement(query);
    ResultSet os = pst.executeQuery();
    laptopos_combo.getItems().clear(); // Add this line to remove all items before you add the database one
    while(os.next()) {
        laptopos_combo.getItems().add(os.getString("os"));
    }
}
0yg35tkg

0yg35tkg3#

我尝试了下面的代码,但只显示第一个值。我是通过实现可初始化接口来实现的

try
{
    String connectionUrl = "jdbc:sqlserver://WIN\\SQLEXPRESS:1433;database=itinventory;" + 
        "user=sa;"+ "password=Varpal@1234;";
    Connection connect= DriverManager.getConnection(connectionUrl);
    String query="Select * from operatingsystem";

    PreparedStatement pst = connect.prepareStatement(query);
            // log.conn().prepareStatement(query);
    ResultSet os = pst.executeQuery();

    while(os.next())
    {
        ObservableList<String> value = 
  FXCollections.observableArrayList(os.getString("os"));
         System.out.println(value);
        laptopos_combo.setItems(value);
    }
}

相关问题