将javafx中的组合框与数据库连接

时间:2018-04-04 23:28:40

标签: database oracle javafx

我尝试将javafx中的组合框与数据库连接但我无法做到这一点......这是我的fxml控制器代码和fxml文件

它没有显示任何类型的错误,但仍无法正常工作......

public class FXMLDocumentController implements Initializable {

 @FXML
    private Button button;
    final ObservableList options=FXCollections.observableArrayList();
    ComboBox combobox=new ComboBox(options);
    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
         try{
             Class.forName("oracle.jdbc.driver.OracleDriver");
             Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","hetvi9398");
             ResultSet rs = con.createStatement().executeQuery("SELECT username FROM typebuddy;");
                while(rs.next())
                {
                    options.add(rs.getString("username"));
                }

             rs.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
         //combobox.setItems(null);
        combobox.setItems(options);
    }
}

这是我对于combobox的fxml标签

<ComboBox fx:id="combobox" editable="true" layoutX="1010.0" layoutY="56.0" prefWidth="150.0" promptText="Username" />

0 个答案:

没有答案