Tableview не показывает данные из бд - Java
Формулировка задачи:
Всем привет! При написании курсовой встретился со следущей проблемой: В Tableview не отображаются данные из бд. Они туда заносятся, но не отображаются. Не могу понять, в чём проблема. Может кто сталкивался с таким?
Сlass Table:
Метод, заполняющий Tableview:
Код FXML-файла:
package work; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; /** * Created by 111 on 19.12.2016. */ public class Table { private final StringProperty nameExe; private final StringProperty groupMuscle; private final StringProperty workWeight; private final StringProperty coutPodh; private final StringProperty date; public Table(String nameExe, String groupMuscle, String workWeight, String coutPodh, String date) { this.nameExe = new SimpleStringProperty(nameExe); this.groupMuscle = new SimpleStringProperty(groupMuscle); this.workWeight =new SimpleStringProperty(workWeight); this.coutPodh = new SimpleStringProperty(coutPodh); this.date = new SimpleStringProperty(date); } public String getnameExe() {return nameExe.get();} public String getgroupMuscle() {return groupMuscle.get();} public String getworkWeight() {return workWeight.get();} public String getcoutPodh() {return coutPodh.get();} public String getdate() {return date.get();} public void setnameExe(String value) { nameExe.set(value);} public void setgroupMuscle(String value) { groupMuscle.set(value);} public void setworkWeight(String value) { workWeight.set(value);} public void setcoutPodh(String value) {coutPodh.set(value);} public void setdate(String value) { date.set(value);} public StringProperty nameExePropety() {return nameExe;} public StringProperty groupMusclePropety() {return groupMuscle;} public StringProperty workWeightPropety() {return workWeight;} public StringProperty coutPodhPropety() {return coutPodh;} public StringProperty datePropety() {return date;} }
private static Statement st; private static ObservableList<Table> data = FXCollections.observableArrayList(); @FXML private TableView<Table> table; @FXML private TableColumn<Table,String> exeCol; @FXML private TableColumn<Table,String> groupCol; @FXML private TableColumn<Table,String> workWeightCol; @FXML private TableColumn<Table,String> coutCol; @FXML private TableColumn<Table,String> dateCol; public void setDataBaseInfo() throws SQLException { db.setConnect(); Controller con = new Controller(); st= db.connect.createStatement(); ResultSet rs2 = st.executeQuery("Select DISTINCT Exercise.nameExe, Muscle.groupMuscle, Training.workWeight, Training.coutPodh, Training.date From Training,Exercise, Muscle Where Training.idlog =(SELECT Authoriz.idlog FROM Authoriz WHERE Authoriz.login = '"+con.a+"' AND Authoriz.password = '"+con.b+"') AND Training.id_muscle = Muscle.id_muscle AND Training.id_exercise=Exercise.id_exercise;"); while (rs2.next()) { data.add(new Table(rs2.getString("nameExe"),rs2.getString("groupMuscle"),rs2.getString("workWeight"),rs2.getString("coutPodh"),rs2.getString("date"))); table.setItems(data); } } @Override public void initialize(URL location, ResourceBundle resources) { exeCol.setCellValueFactory(new PropertyValueFactory<>("nameExe")); groupCol.setCellValueFactory(new PropertyValueFactory<>("groupMuscle")); workWeightCol.setCellValueFactory(new PropertyValueFactory<>("workWeight")); coutCol.setCellValueFactory(new PropertyValueFactory<>("coutPodh")); dateCol.setCellValueFactory(new PropertyValueFactory<>("date")); try { setDataBaseInfo(); } catch (SQLException e) { e.printStackTrace(); } }
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Accordion?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.ComboBox?> <?import javafx.scene.control.DatePicker?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SplitPane?> <?import javafx.scene.control.Tab?> <?import javafx.scene.control.TabPane?> <?import javafx.scene.control.TableColumn?> <?import javafx.scene.control.TableView?> <?import javafx.scene.control.TextField?> <?import javafx.scene.control.TitledPane?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="work.WorkController"> <children> <Accordion prefHeight="400.0" prefWidth="800.0"> <panes> <TitledPane animated="false" prefHeight="310.0" prefWidth="800.0" text="Информация о тренеровках" fx:id="x1"> <content> <SplitPane dividerPositions="0.17669172932330826" focusTraversable="true" prefHeight="160.0" prefWidth="200.0"> <items> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="349.0" prefWidth="282.0"> <children> <MenuBar layoutX="0.0" layoutY="0.0" prefHeight="25.0" prefWidth="236.0"> <menus> <Menu mnemonicParsing="false" text="File"> <items> <MenuItem mnemonicParsing="false" text="Close" /> </items> </Menu> <Menu mnemonicParsing="false" text="Edit"> <items> <MenuItem mnemonicParsing="false" text="Delete" /> </items> </Menu> <Menu mnemonicParsing="false" text="Help"> <items> <MenuItem mnemonicParsing="false" text="About" /> </items> </Menu> </menus> </MenuBar> </children> </AnchorPane> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="299.0" prefWidth="416.0"> <children> <TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="655.0"> <columns> <TableColumn fx:id="exeCol" prefWidth="94.0" text="Упражнение" /> <TableColumn fx:id="groupCol" prefWidth="144.0" text="Группа мышц" /> <TableColumn fx:id="workWeightCol" prefWidth="107.0" text="Рабочий вес" /> <TableColumn fx:id="coutCol" prefWidth="166.0" text="Количество подходов" /> <TableColumn fx:id="dateCol" prefWidth="143.0" text="Дата тренировки" /> </columns> </TableView> </children></AnchorPane> </items> </SplitPane> </content> </TitledPane> <TitledPane animated="false" prefHeight="367.0" prefWidth="594.0" text="Редактирование данных" fx:id="x2"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <TabPane layoutX="0.0" layoutY="0.0" prefHeight="352.0" prefWidth="800.0" tabClosingPolicy="UNAVAILABLE"> <tabs> <Tab onSelectionChanged="#fillinsertTypeExeCombo" text="Упражнения"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="264.0" prefWidth="200.0"> <children> <TextField fx:id="insertNameExe" layoutX="146.0" layoutY="54.0" prefHeight="25.0" prefWidth="200.0" /> <Button fx:id="insertExe" layoutX="14.0" layoutY="90.0" mnemonicParsing="false" onAction="#insertExercise" prefWidth="126.0" text="Добавить" /> <Label layoutX="11.0" layoutY="58.0" text="Название упражнения" /> <Label layoutX="24.0" layoutY="27.0" text="Группа мышц" /> <ComboBox fx:id="insertTypeExeCombo" layoutX="146.0" layoutY="23.0" prefHeight="25.0" prefWidth="200.0" /> </children> </AnchorPane> </content> </Tab> <Tab text="Тренировки"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <TextField fx:id="workWeightTextField" layoutX="142.0" layoutY="14.0" /> <TextField fx:id="countPodhTextField" layoutX="142.0" layoutY="58.0" /> <ComboBox fx:id="insertTypeExeCombo2" layoutX="142.0" layoutY="103.0" onAction="#fillinsertNameExeCombo" prefWidth="150.0" /> <ComboBox fx:id="insertNameExeCombo" layoutX="142.0" layoutY="149.0" prefWidth="150.0" /> <DatePicker fx:id="dateTrainingPicker" layoutX="141.0" layoutY="195.0" prefHeight="25.0" prefWidth="152.0" /> <Label layoutX="29.0" layoutY="18.0" text="Рабочий вес" /> <Label layoutX="28.0" layoutY="199.0" text="Дата тренировки" /> <Label layoutX="14.0" layoutY="62.0" text="Количество подходов" /> <Label layoutX="26.0" layoutY="107.0" text="Группа мышц" /> <Label layoutX="13.0" layoutY="153.0" text="Название упражнения" /> <Button fx:id="addTrainBut" layoutX="142.0" layoutY="243.0" mnemonicParsing="false" onAction="#insertTraining" prefHeight="25.0" prefWidth="151.0" text="Button" /> </children></AnchorPane> </content> </Tab> </tabs> </TabPane> </children> </AnchorPane> </content> </TitledPane> </panes> </Accordion> </children> </AnchorPane>
Решение задачи: «Tableview не показывает данные из бд»
textual
Листинг программы
ResultSet rs2 = st.executeQuery("Select DISTINCT Exercise.nameExe, Muscle.groupMuscle, Training.workWeight, Training.coutPodh, Training.date From Training,Exercise, Muscle Where Training.idlog =(SELECT Authoriz.idlog FROM Authoriz WHERE Authoriz.login = '"+con.a+"' AND Authoriz.password = '"+con.b+"') AND Training.id_muscle = Muscle.id_muscle AND Training.id_exercise=Exercise.id_exercise;"); while (rs2.next()) { data.add(new Table(rs2.getString("Exercise.nameExe"),rs2.getString("Muscle.groupMuscle"),rs2.getString("Training.workWeight"),rs2.getString("Training.coutPodh"),rs2.getString("Training.date"))); table.setItems(data); }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д