我是JavaFX的新手,也不熟悉在Java中使用数据库,但是我必须转换现有的JavaFX程序来删除填充TableView表的列表并将其替换为项目从数据库中,并为用户提供添加或删除的能力。这本质上是学生课程注册计划的简化版本。一切都按原样运作,我只需要使用一个数据库,以便教师方可以用来查看哪些学生注册了哪些课程。我只是不熟悉调用数据库并将其应用于tableView的最佳方法。我希望能够在桌面上选择一行,单击我的操作按钮并注册该学生。非常感谢任何帮助。
代码:
public class Screen3Controller implements Initializable {
@FXML private Button continuebtn;
@FXML private Button studAddCrseBtn;
@FXML private Button yesBtn;
@FXML private Label regStatus;
@FXML private TableView<SummerClass> table;
@FXML private TableColumn<SummerClass, Integer> id;
@FXML private TableColumn<SummerClass, String> dept;
@FXML private TableColumn<SummerClass,Integer> number;
@FXML private TableColumn<SummerClass, String> title;
@FXML private TableColumn<SummerClass, String> day;
@FXML private TableColumn<SummerClass, String> time;
@FXML private TableColumn<SummerClass, Boolean> checkbox;
private ObservableSet<CheckBox> selectedCheckBoxes = FXCollections.observableSet();
private ObservableSet<CheckBox> unselectedCheckBoxes = FXCollections.observableSet();
private IntegerBinding numCheckBoxesSelected = Bindings.size(selectedCheckBoxes);
@FXML
private IntegerProperty index = new SimpleIntegerProperty();
private final int maxNumSelected = 3;
public ObservableList<SummerClass> list1 = FXCollections.observableArrayList(
new SummerClass (10001, "ACCT", 1010 , "Intro to Acct (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10002, "ACCT", 2010 , "Acct for Bus. (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10003, "ART", 1010 , "Fund. of Art (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10004, "ART", 1110 , "Art History (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10005, "BIOL", 1010 , "Principles of Biology I (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10006, "BIOL", 2010 , "Principles of Biology II (3)", "MWF", "3:00 - 4:15" ),
new SummerClass (10007, "CHEM", 1010 , "Principles of Chemistry I (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10008, "CHEM", 2010 , "Principles of Chemistry II (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10009, "CPSC", 1010 , "Java I (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10010, "CPSC", 2010 , "Java II (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10011, "ENGL", 1010 , "Composition I (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10012, "ENGL", 2010 , "Composition II (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10013, "FIN", 1010 , "Principles of Finance I (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10014, "FIN", 2010 , "Principles of Finance II (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10015, "GEO", 1010 , "Intro to Geology (3)", "MWF", "3:00 - 4:15" ),
new SummerClass (10016, "GEO", 3610 , "Geology in History (3)", "TR", "1:00 - 2:15" ),
new SummerClass (10017, "HIST", 1210 , "Western History (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10018, "HIST", 1510 , "Fund. of History (3)", "MWF", "3:00 - 4:15" ),
new SummerClass (10019, "MATH", 1010 , "Elementary Algebra (3)", "TR", "1:00 - 2:15" ),
new SummerClass (10020, "MATH", 2010 , "Linear Algebra (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10021, "NUTR", 1010 , "Nutrition Fundamentals (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10022, "PHYS", 1010 , "Intro to Physics (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10023, "PHYS", 2010 , "Physics II (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10024, "POL", 1010 , "Political Science I (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10025, "POL", 1010 , "Political Science II (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10026, "STEM", 1010 , "Stem Education I (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10027, "STEM", 2010 , "Advanced Stem Education (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10028, "STEM", 3010 , "Stem Education in Business (3)", "MWF", "1:00 - 2:15" ));
final ObservableList<SummerClass> list2 = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
id.setCellValueFactory(new PropertyValueFactory<>("id"));
dept.setCellValueFactory(new PropertyValueFactory<>("dept"));
number.setCellValueFactory(new PropertyValueFactory<>("number"));
title.setCellValueFactory(new PropertyValueFactory<>("title"));
day.setCellValueFactory(new PropertyValueFactory<>("day"));
time.setCellValueFactory(new PropertyValueFactory<>("time"));
checkbox.setCellValueFactory(new PropertyValueFactory<SummerClass, Boolean>(""));
checkbox.setCellFactory(new Callback<TableColumn<SummerClass, Boolean>, TableCell<SummerClass, Boolean>>(){
public TableCell<SummerClass, Boolean> call(TableColumn<SummerClass, Boolean> p){
return new CheckBoxTableCell<SummerClass, Boolean>();
}
答案 0 :(得分:1)
您的问题可能有点过于宽泛而无法在StackOverflow上得到解答,但是,我会给出一个链接到其他问题的答案以及可能对您有帮助的异地资源。
Sample for accessing a local database from JavaFX使用并发任务进行数据库操作,以便UI保持响应。
示例的相关StackOverflow问题是:
示例基于嵌入式H2 database。
此示例使用JDBC,这适用于一些基本操作,如示例中所示。对于更广泛的系统,最好直接使用JPA而不是JDBC。
更多资源:
比将JavaFX应用程序直接连接到数据库更灵活的设计是使用三层体系结构而不是双层体系结构。为此,您可以使用基于网络的服务层(例如,使用JAX-RS的SpringBoot或WildFly Swarm)在数据库前面,然后从客户端应用程序与服务层进行通信。服务层负责所有与数据库相关的操作。但是,构建这样的应用程序比直接连接到数据库要复杂得多。
您可能希望在应用程序中使用一些基本的MVC设计原则,而不是像在问题中提供的那样将数据模型嵌入到控制器中。做MVC部分可能会让你有点棘手。你可以谷歌关于它并使用一个框架,但这可能会让你感到困惑。也许,只是尝试在没有任何框架的情况下对其进行编码,但也许只需要一些定制设计工作,如果需要,可以改进一个MVC类型系统,如Afterburner.fx或Gluon Ignite,如果你发现你会从这样的事情中受益你在发展方面取得了进步。
鉴于这是一个学生项目,而不是大型商业产品,我建议只选择最简单的解决方案,扩展前面提到的基于H2的样本,以提供您需要的功能,而不是引入许多其他技术或库例如JPA,JAX-RS,网络服务等。