Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package GUI;
- import JDBC.DB_Access;
- import MAIN.DB_VIEW;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ResourceBundle;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.collections.transformation.*;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.TextField;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.input.TouchEvent;
- /**
- * FXML Controller class
- *
- * @author Kamil
- */
- public class SklepController implements Initializable {
- private int ID_Przedmiotu;
- private ObservableList<DB_VIEW> obList = FXCollections.observableArrayList();
- public void updateList() throws ClassNotFoundException, SQLException
- {
- obList.clear();
- //System.out.println("Załadowane poprawnie dane do tabeli.");
- ResultSet rs = DB_Access.executeSelect("SELECT * FROM Magazyn");
- try{
- while(rs.next()){
- //System.out.println("Nazwa: "+rs.getString("Nazwa_Przedmiotu")+" Rodzaj: "+ rs.getString("Rodzaj_Przedmiotu"));
- obList.add(new DB_VIEW(rs.getString("Nazwa_Przedmiotu"), rs.getString("Rodzaj_Przedmiotu"),
- rs.getDouble("Cena_Przedmiotu"),(int) rs.getDouble("Ilosc"), (int) rs.getDouble("ID_Przedmiotu")));
- }
- }catch(SQLException e){
- throw new RuntimeException(e.getMessage());
- }
- TableView.setItems(obList);
- }
- public void refreshScene(){
- SceneManager.renderScene("Sklep");
- }
- @FXML
- private Button Przycisk_Aktualizuj;
- @FXML
- private Button Przycisk_Dodaj;
- @FXML
- private TextField TextField_Wyszukiwanie;
- @FXML
- private Button Przycisk_Menu_5;
- @FXML
- private TableColumn<DB_VIEW, String> Kolumna_Nazwa;
- @FXML
- private TableColumn<DB_VIEW, String> Kolumna_Rodzaj;
- @FXML
- private TableColumn<DB_VIEW, Double> Kolumna_Ilość_Sztuk;
- @FXML
- private TableView<DB_VIEW> TableView;
- @FXML
- private TableColumn<DB_VIEW, Double> Kolumna_Cena;
- @FXML
- private TextField TextField_Nazwa;
- @FXML
- private TextField TextField_Rodzaj;
- @FXML
- private TextField TextField_Ilosc_Szt;
- @FXML
- private TextField TextField_Cena;
- @FXML
- private Button Przycisk_Delete;
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- Kolumna_Nazwa.setCellValueFactory(new PropertyValueFactory<>("Nazwa_Przedmiotu"));
- Kolumna_Rodzaj.setCellValueFactory(new PropertyValueFactory<>("Rodzaj_Przedmiotu"));
- Kolumna_Ilość_Sztuk.setCellValueFactory(new PropertyValueFactory<>("Ilosc"));
- Kolumna_Cena.setCellValueFactory(new PropertyValueFactory<>("Cena_Przedmiotu"));
- try {
- updateList();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- }
- FilteredList<DB_VIEW> filteredData = new FilteredList<>(obList, b -> true);
- TextField_Wyszukiwanie.textProperty().addListener((observable, oldValue, newValue) -> {
- filteredData.setPredicate(DB_VIEW -> {
- if (newValue == null || newValue.isEmpty()) {
- return true;
- }
- String lowerCaseFilter = newValue.toLowerCase();
- if (DB_VIEW.getNazwa_Przedmiotu().toLowerCase().contains(lowerCaseFilter)) {
- return true;
- } else if (DB_VIEW.getRodzaj_Przedmiotu().toLowerCase().contains(lowerCaseFilter)) {
- return true;
- }
- else if (String.valueOf(DB_VIEW.getCena_Przedmiotu()).toLowerCase().contains(lowerCaseFilter)){
- return true;
- }
- else if (String.valueOf(DB_VIEW.getIlosc()).toLowerCase().contains(lowerCaseFilter))
- return true;
- else
- return false;
- });
- });
- SortedList<DB_VIEW> sortedData = new SortedList<>(filteredData);
- sortedData.comparatorProperty().bind(TableView.comparatorProperty());
- TableView.setItems(sortedData);
- }
- @FXML
- private void Idz_do_Menu(ActionEvent event) {
- SceneManager.renderScene("Menu");
- }
- @FXML
- private void Usuń_Dane(ActionEvent event) throws ClassNotFoundException, SQLException {
- try{
- int ID = obList.size();
- String Nazwa=TextField_Nazwa.getText();
- String Rodzaj=TextField_Rodzaj.getText();
- String Ilosc=TextField_Ilosc_Szt.getText();
- String Cena=TextField_Cena.getText();
- // String ZabezpieczeniaT = "[A-Za-z%](?!^\\d+$)^.+$";
- // String ZabezpieczeniaI = "[0-9\\.\\,]";
- //
- // if(!Nazwa.matches(ZabezpieczeniaT)||!Rodzaj.matches(ZabezpieczeniaT)){
- // System.out.println("Wprowadzaono złe dane!");
- // return ;
- // }
- // if(!Ilosc.matches(ZabezpieczeniaI)||!Cena.matches(ZabezpieczeniaI)){
- // System.out.println("Wprowadzaono złe dane!");
- // return ;
- // }
- String sqlA = "DELETE FROM Magazyn WHERE Nazwa_Przedmiotu=? AND Rodzaj_Przedmiotu=? AND Ilosc=? AND Cena_Przedmiotu=?";
- try{
- Connection connection = DB_Access.connect();
- PreparedStatement STMTA = connection.prepareStatement(sqlA); {
- Double.parseDouble(Ilosc);
- Double.parseDouble(Cena);
- STMTA.setString(1, Nazwa);
- STMTA.setString(2, Rodzaj);
- STMTA.setString(3, Ilosc);
- STMTA.setString(4, Cena);
- STMTA.executeUpdate();
- System.out.println("Usunięto poprawnie przedmiot! ");
- refreshScene();
- }
- try {
- updateList();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- catch (ClassNotFoundException ex) {
- Logger.getLogger(ProfilController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @FXML
- private void Dodaj_Dane(ActionEvent event) throws ClassNotFoundException, SQLException {
- try{
- int N_id = obList.size()+1;
- String NowaNazwa=TextField_Nazwa.getText();
- String NowyRodzaj=TextField_Rodzaj.getText();
- String NowaIlosc=TextField_Ilosc_Szt.getText();
- String NowaCena=TextField_Cena.getText();
- // String ZabezpieczeniaT = "[A-Za-z%](?!^\\d+$)^.+$";
- // String ZabezpieczeniaI = "[0-9\\.\\,]";
- //
- // if(!NowaNazwa.matches(ZabezpieczeniaT)||!NowyRodzaj.matches(ZabezpieczeniaT)){
- // System.out.println("Wprowadzaono złe dane!");
- // return ;
- // }
- // if(!NowaIlosc.matches(ZabezpieczeniaI)||!NowaCena.matches(ZabezpieczeniaI)){
- // System.out.println("Wprowadzaono złe dane!");
- // return ;
- // }
- String sql = "INSERT INTO Magazyn(ID_Przedmiotu, Nazwa_Przedmiotu,Rodzaj_Przedmiotu,Ilosc,Cena_Przedmiotu) VALUES(?,?,?,?,?)";
- if(!obList.isEmpty()){
- obList.clear();
- }
- try{
- Connection connection = DB_Access.connect();
- PreparedStatement pstmt = connection.prepareStatement(sql); {
- Double.parseDouble(NowaIlosc);
- pstmt.setInt(1, N_id);
- pstmt.setString(2, NowaNazwa);
- pstmt.setString(3, NowyRodzaj);
- pstmt.setString(4, NowaIlosc);
- pstmt.setString(5, NowaCena);
- pstmt.executeUpdate();
- System.out.println("Dodano poprawnie przedmiot! ");
- refreshScene();
- }
- try {
- updateList();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- catch (ClassNotFoundException ex) {
- Logger.getLogger(ProfilController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @FXML
- private void Aktualizuj_Dane(ActionEvent event) {
- try{
- String NazwaUpdate=TextField_Nazwa.getText();
- String RodzajUpdate=TextField_Rodzaj.getText();
- String IloscUpdate=TextField_Ilosc_Szt.getText();
- String CenaUpdate=TextField_Cena.getText();
- // String ZabezpieczeniaT = "[A-Za-z%](?!^\\d+$)^.+$";
- // String ZabezpieczeniaI = "[0-9\\.\\,]";
- //
- // if(!NazwaUpdate.matches(ZabezpieczeniaT)||!RodzajUpdate.matches(ZabezpieczeniaT)){
- // System.out.println("Wprowadzaono złe dane!");
- // return ;
- // }
- // if(!IloscUpdate.matches(ZabezpieczeniaI)||!CenaUpdate.matches(ZabezpieczeniaI)){
- // System.out.println("Wprowadzaono złe dane!");
- // return ;
- // }
- String sql = "UPDATE Magazyn SET Nazwa_Przedmiotu=?, Rodzaj_Przedmiotu=?, Ilosc=?, Cena_Przedmiotu=? WHERE ID_Przedmiotu=?;";
- try{
- Connection connection = DB_Access.connect();
- PreparedStatement pstmtU = connection.prepareStatement(sql); {
- pstmtU.setString(1, NazwaUpdate);
- pstmtU.setString(2, RodzajUpdate);
- pstmtU.setString(3, IloscUpdate);
- pstmtU.setString(4, CenaUpdate);
- pstmtU.setString(5, String.valueOf(ID_Przedmiotu));
- pstmtU.executeUpdate();
- System.out.println("Zaktualizowano poprawnie przedmiot! ");
- refreshScene();
- ID_Przedmiotu = -1;
- TextField_Nazwa.setText("");
- TextField_Rodzaj.setText("");
- TextField_Ilosc_Szt.setText("");
- TextField_Cena.setText("");
- }
- try {
- updateList();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- catch (ClassNotFoundException ex) {
- Logger.getLogger(ProfilController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @FXML
- private void TableView_Klikniece_Myszką(MouseEvent event) {
- DB_VIEW Dane = TableView.getSelectionModel().getSelectedItem();
- ID_Przedmiotu = TableView.getSelectionModel().getSelectedItem().getID_Przedmiotu();
- TextField_Nazwa.setText(Dane.getNazwa_Przedmiotu());
- TextField_Rodzaj.setText(Dane.getRodzaj_Przedmiotu());
- TextField_Ilosc_Szt.setText(String.valueOf(Dane.getIlosc()));
- TextField_Cena.setText(String.valueOf(Dane.getCena_Przedmiotu()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement