Guest User

Untitled

a guest
Jul 8th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. package javaFiles.controllers;
  2.  
  3. import com.jfoenix.controls.JFXTextField;
  4. import javaFiles.util.DbConnection;
  5. import javaFiles.util.UserData;
  6. import javafx.collections.FXCollections;
  7. import javafx.collections.ObservableArray;
  8. import javafx.collections.ObservableList;
  9. import javafx.event.EventHandler;
  10. import javafx.fxml.FXML;
  11. import javafx.fxml.FXMLLoader;
  12. import javafx.fxml.Initializable;
  13. import javafx.scene.Parent;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.*;
  16. import javafx.scene.control.cell.PropertyValueFactory;
  17. import javafx.scene.input.MouseEvent;
  18. import javafx.stage.Stage;
  19.  
  20. import java.awt.event.ActionEvent;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.util.Date;
  28. import java.util.ResourceBundle;
  29.  
  30. public class DashboardController implements Initializable
  31. {
  32.     private String currentUser = LoginFormController.getCurrentUser();
  33.     private String tableToOpen = currentUser + "_database";
  34.  
  35.     private static Stage stage = new Stage();
  36.  
  37.     private LoginFormController loginFormController;
  38.  
  39.     @FXML
  40.     private TextField website;
  41.     @FXML
  42.     private TextField username;
  43.     @FXML
  44.     private TextField password;
  45.     @FXML
  46.     private TextArea notes;
  47.     @FXML
  48.     private Button addBtn;
  49.  
  50.     //TableView Items
  51.     @FXML
  52.     private TableView<UserData> dataTableview;
  53.     @FXML
  54.     private TableColumn<UserData,String> websiteCol;
  55.     @FXML
  56.     private TableColumn<UserData,String> usernameCol;
  57.     @FXML
  58.     private TableColumn<UserData,String> passwordCol;
  59.     @FXML
  60.     private TableColumn<UserData,String> notesCol;
  61.  
  62.     private ObservableList<UserData> data = FXCollections.observableArrayList();;
  63.  
  64.     private DbConnection dbConnection;
  65.  
  66.     public void showDashboardWindow() throws IOException
  67.     {
  68.         Parent root = FXMLLoader.load(getClass().getResource("/resources/fxml/dashboard.fxml"));
  69.  
  70.         Stage stage = getDashboardStage();
  71.         Scene scene = new Scene(root);
  72.  
  73.         stage.setTitle("Sign-Up Page");
  74.         stage.setResizable(false);
  75.         stage.setScene(scene);
  76.         stage.show();
  77.     }
  78.  
  79.     public Stage getDashboardStage()
  80.     {
  81.         return stage;
  82.     }
  83.  
  84.     @Override
  85.     public void initialize(URL url, ResourceBundle resourceBundle)
  86.     {
  87.         try
  88.         {
  89.             notes.setWrapText(true);
  90.             loadData();
  91.         }
  92.         catch (SQLException throwables)
  93.         {
  94.             throwables.printStackTrace();
  95.         }
  96.     }
  97.  
  98.     private void loadData() throws SQLException
  99.     {
  100.         Connection connection = DbConnection.connection();
  101.         //PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Tester3_database" );
  102.         ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM " + tableToOpen);
  103.  
  104.         while (resultSet.next())
  105.         {
  106.             data.add(new UserData(resultSet.getString("Website"),resultSet.getString("Username"), resultSet.getString("Password"), resultSet.getString("Notes")));
  107.         }
  108.  
  109.         this.websiteCol.setCellValueFactory(new PropertyValueFactory<>("Website"));
  110.         this.usernameCol.setCellValueFactory(new PropertyValueFactory<>("Username"));
  111.         this.passwordCol.setCellValueFactory(new PropertyValueFactory<>("Password"));
  112.         this.notesCol.setCellValueFactory(new PropertyValueFactory<>("Notes"));
  113.  
  114.         dataTableview.setItems(data);
  115.  
  116.  
  117.         connection.close();
  118.         resultSet.close();
  119.     }
  120.  
  121.     private void addData() throws SQLException
  122.     {
  123.         Connection conn = DbConnection.connection();
  124.         PreparedStatement ps = conn.prepareStatement("INSERT INTO " + tableToOpen + "(Website,Username,Password,Notes) VALUES (?,?,?,?)");
  125.  
  126.         ps.setString(1,this.website.getText());
  127.         ps.setString(2,this.username.getText());
  128.         ps.setString(3,this.password.getText());
  129.         ps.setString(4,this.notes.getText());
  130.  
  131.         ps.execute();
  132.  
  133.         conn.close();
  134.         ps.close();
  135.     }
  136.  
  137.     private void clearTextField()
  138.     {
  139.         this.username.setText("");
  140.         this.website.setText("");
  141.         this.password.setText("");
  142.         this.notes.setText("");
  143.     }
  144.  
  145.     private void clearTableView()
  146.     {
  147.         dataTableview.getItems().clear();
  148.     }
  149.  
  150.     public void addBtnClicked(MouseEvent mouseEvent) throws SQLException
  151.     {
  152.         clearTableView();
  153.         addData();
  154.         loadData();
  155.         clearTextField();
  156.     }
  157.    
  158. //    dataTableview.setOnMousePressed(new EventHandler<MouseEvent>() {
  159. //    @Override
  160. //    public void handle(MouseEvent event) {
  161. //        if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
  162. //            System.out.println(dataTableview.getSelectionModel().getSelectedItem());
  163. //        }
  164. //    }
  165. //});
  166.  
  167. }
Add Comment
Please, Sign In to add comment