package jfxapp; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.GridPane; import javafx.util.Callback; import org.jooq.*; import org.jooq.impl.DSL; import org.jooq.util.GenerationTool; import org.jooq.util.jaxb.*; import org.jooq.util.jaxb.Configuration; import java.math.BigDecimal; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Date; import java.util.ResourceBundle; import static jfxapp.Tables.UDAJE; public class samexam5 { private Connection CONN = null; private String host = "jdbc:postgresql://127.0.0.1:5432/fxguidedb"; Integer pid = null; @FXML private ResourceBundle resources; @FXML private URL location; @FXML private TextField u_Cele; @FXML private TextField u_Datum; @FXML private TextField u_Maledes; @FXML private TextField n_Desetinne; @FXML private Button sr_Button; @FXML private Button nr_Button; @FXML private GridPane grid1; @FXML private TextField n_Datum; @FXML private AnchorPane contentPane; @FXML private TextField u_ID; @FXML private TableView table1; @FXML private TableColumn col1; @FXML private TableColumn col2; @FXML private TableColumn col3; @FXML private TableColumn col4; @FXML private TableColumn col5; @FXML private TableColumn col6; @FXML private TextField u_Desetinne; @FXML private GridPane grid11; @FXML private TextField u_Text; @FXML private Button d_Button; @FXML private Button e_Button; @FXML private TextField n_Cele; @FXML private TextField n_Text; @FXML private TextField n_Maledes; @FXML private DatePicker dp_Datum; @FXML private Button a_Button; @FXML private Button u_Button; @FXML private TabPane tabPane; @FXML private TextField n_ID; @FXML void btn_D_Click(ActionEvent event) { onDelete(); } @FXML void btn_A_Click(ActionEvent event) { jooq_Query(); } @FXML void btn_U_Click(ActionEvent event) { viewTable(); } @FXML void btn_E_Click(ActionEvent event) { } @FXML void btn_NR_Click(ActionEvent event) { } @FXML void btn_SR_Click(ActionEvent event) { } @FXML void initialize() { viewTable(); table1.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent mouseEvent) { if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) { if (mouseEvent.getClickCount() == 2) { tableClick(); } } } }); } private void code_Gener() { Configuration configuration = new Configuration() .withJdbc(new Jdbc() .withDriver("org.postgresql.Driver") .withUrl(host) //"jdbc:postgresql://127.0.0.1:5432/fxguidedb") .withUser("fxguide") .withPassword("fxguide")) .withGenerator(new Generator() .withDatabase(new Database() .withName("org.jooq.util.postgres.PostgresDatabase") .withIncludes("udaje.*") .withExcludes("") .withInputSchema("public")) .withTarget(new Target() .withPackageName("jfxapp") .withDirectory(""))); try { GenerationTool.generate(configuration); } catch (Exception e) { e.printStackTrace(); } } private Connection connDB (final String jmeno, final String heslo) { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { CONN = DriverManager.getConnection(host, jmeno, heslo); return CONN; } catch (SQLException e) { e.getMessage(); return CONN; } } private void tf_RO() { u_Cele.setEditable(false); u_Desetinne.setEditable(false); u_Maledes.setEditable(false); u_Text.setEditable(false); u_Datum.setEditable(false); } private void tf_RW() { u_Cele.setEditable(true); u_Desetinne.setEditable(true); u_Maledes.setEditable(true); u_Text.setEditable(true); u_Datum.setEditable(true); } private void jooq_Query() { DSLContext create = DSL.using(connDB("fxguide", "fxguide"), SQLDialect.POSTGRES); Result result = create .select() .from(UDAJE) .where(UDAJE.ID.lt((short) 10)) .orderBy(UDAJE.ID) .fetch(); for (Record r : result) { Short id = r.getValue(UDAJE.ID); Integer cis = r.getValue(UDAJE.CELECIS); BigDecimal des = r.getValue(UDAJE.DESCIS); BigDecimal mad = r.getValue(UDAJE.MALEDES); String str = r.getValue(UDAJE.RETEZEC); Date dtm = r.getValue(UDAJE.DATUM); System.out.println("ID= " + id.toString() + " Cele= " + cis.toString() + " Desetinne= " + des.toString() + " Male des.= " + mad.toString() + " String: " + str + " Datum= " + dtm); } } private void initTableColumn(final TableColumn[] column) { for (int i=0;i, ObservableValue>() { @Override public ObservableValue call(TableColumn.CellDataFeatures param) { return new SimpleObjectProperty(param.getValue().getValue(finalI)); } }); } } private void alignTableColumn(final TableColumn[] column, final String[] align) { String pos = null; for (int i=0;i dataView() { DSLContext create = DSL.using(connDB("fxguide", "fxguide"), SQLDialect.POSTGRES); ObservableList data = javafx.collections.FXCollections.observableArrayList(create .select() .from(UDAJE) //.where(UDAJE.ID.lt((short) 10)) .orderBy(UDAJE.ID) .fetch()); // System.out.println(data.toString()); return data; } private void viewTable() { ObservableList data = dataView(); final TableColumn[] tc = new TableColumn[]{col1, col2, col3, col4, col5, col6}; initTableColumn(tc); alignTableColumn(tc, new String[]{"CA", "RA", "RA", "RA", "LA", "CA"}); table1.setItems(data); } private void deleteRec(final String tabname, final String colname, final Integer recval) { DSLContext create = DSL.using(connDB("fxguide", "fxguide"), SQLDialect.POSTGRES); create .deleteFrom(DSL.tableByName(tabname)) .where(DSL.fieldByName(colname).equal(recval.shortValue())) .execute(); } private void onDelete() { deleteRec("udaje", "id", pid); viewTable(); tabPane.getSelectionModel().select(0); } private void tableClick() { pid = Integer.valueOf(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(0))); u_ID.setText(pid.toString()); u_Cele.setText(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(1))); u_Desetinne.setText(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(2))); u_Maledes.setText(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(3))); u_Text.setText(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(4))); u_Datum.setText(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(5))); tabPane.getSelectionModel().select(1); tf_RO(); d_Button.setDisable(false); a_Button.setDisable(false); u_Button.setDisable(true); e_Button.setDisable(true); System.out.println(String.valueOf(table1.getSelectionModel().getSelectedItems().get(0).getValue(0))); } }