daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Mar 19th, 2017 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class LoginController
  2.  
  3.     private databaseConnection conn = new databaseConnection("student","test");
  4.     private PreparedStatement mySt = null;
  5.     private Statement st = null;
  6.     private boolean noConnection;
  7.  
  8.     @FXML
  9.     private Button button;
  10.  
  11.     @FXML
  12.     private TextField username;
  13.  
  14.     @FXML
  15.     private TextField password;
  16.  
  17.     @FXML
  18.     private Label invalidPass;
  19.  
  20.  
  21.  
  22.     @FXML
  23.     private void buttonAction (ActionEvent event) throws IOException, SQLException{
  24.  
  25.         Parent mainPageParent = FXMLLoader.load(getClass().getResource("fxml/DatabaseUi.fxml"));
  26.         Scene mainPageScene = new Scene(mainPageParent);
  27.  
  28.         Stage loginStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
  29.  
  30.         if(this.isValidLogin()){
  31.             loginStage.hide();
  32.             loginStage.setScene(mainPageScene);
  33.             loginStage.show();
  34.  
  35.         }else{
  36.  
  37.             username.clear();
  38.             password.clear();
  39.             if(noConnection){
  40.                 invalidPass.setText("No connection to the Database");
  41.  
  42.             }else{
  43.                 invalidPass.setText("Invalid username or password !");
  44.             }
  45.  
  46.  
  47.  
  48.         }
  49.  
  50.  
  51.     }
  52.  
  53.     private boolean isValidLogin() throws IOException, SQLException {
  54.  
  55.  
  56.         try {
  57.             conn.getConnection();
  58.  
  59.             mySt = conn.getConnection().prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
  60.  
  61.             mySt.setString(1, username.getText());
  62.             mySt.setString(2, password.getText());
  63.  
  64.             ResultSet result  = mySt.executeQuery();
  65.  
  66.             if(result.next()){
  67.  
  68.                 return true;
  69.             }else{
  70.                 return false;
  71.             }
  72.  
  73.         } catch (SQLException e ) {
  74.             this.noConnection=true;
  75.             e.printStackTrace();
  76.  
  77.         }
  78.  
  79.         return false;
  80.  
  81.  
  82.     }
  83.  
  84. }
  85.    
  86. public class DatabaseUiController {
  87.  
  88.     private databaseConnection conn = new databaseConnection("student","test");
  89.     private PreparedStatement mySt = null;
  90.     private Statement st = null;
  91.     private ResultSet result = null;
  92.     private ObservableList<Customer> customers=FXCollections.observableArrayList();
  93.     private Connection myConn =null;
  94.  
  95.     @FXML
  96.     private Button searchButton;
  97.  
  98.     @FXML
  99.     private Button addButton;
  100.  
  101.     @FXML
  102.     private Button updateButton;
  103.  
  104.     @FXML
  105.     private Button deleteButton;
  106.  
  107.     @FXML
  108.     private TextField searchField;
  109.  
  110.     @FXML
  111.     private TableView<Customer> databaseTable;
  112.     @FXML
  113.     private TableColumn columnId;
  114.     @FXML
  115.     private TableColumn columnFirstName;
  116.     @FXML
  117.     private TableColumn columnLastName;
  118.     @FXML
  119.     private TableColumn columnEmail;
  120.     @FXML
  121.     private Label warningLabel;
  122.  
  123.  
  124.  
  125.     @FXML
  126.     private void searchInformation(ActionEvent event) {
  127.         this.tableRefresh();
  128.     }
  129.  
  130.  
  131.     @FXML
  132.     private void addButtonAction(ActionEvent event){
  133.  
  134.  
  135.         try {
  136.             Parent  parentMainAddingScreen = FXMLLoader.load(getClass().getResource("fxml/AddingScreen.fxml"));
  137.             Scene mainAddingScreenScene = new Scene(parentMainAddingScreen);
  138.             Stage addingStage = new Stage();
  139.             addingStage.setTitle("Add customer");
  140.             addingStage.setScene(mainAddingScreenScene);
  141.             addingStage.showAndWait();
  142.             this.tableRefresh();
  143.  
  144.         } catch (IOException e) {
  145.             e.printStackTrace();
  146.             }
  147.  
  148.         }
  149.     @FXML
  150.     private void updateButtonAction(ActionEvent event){
  151.  
  152.  
  153.         try {
  154.             FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/UpdateScreen.fxml"));
  155.  
  156.             Stage updateStage = new Stage();
  157.  
  158.             updateStage.setScene(
  159.                     new Scene((Pane) loader.load()));
  160.  
  161.             UpdateScreenController controller = loader.getController();
  162.  
  163.             Customer selectedCustomer = databaseTable.getSelectionModel().getSelectedItem();
  164.             controller.setSelectedCustomer(selectedCustomer);
  165.  
  166.             if(selectedCustomer==null){
  167.                 warningLabel.setText("You must select the Customer!");
  168.             }else{
  169.  
  170.                 controller.fillTextFields();
  171.  
  172.                 updateStage.showAndWait();
  173.  
  174.                 this.tableRefresh();
  175.             }
  176.  
  177.  
  178.         } catch (IOException e) {
  179.             e.printStackTrace();
  180.             }
  181.  
  182.         }
  183.  
  184.     @FXML
  185.     private void deleteEvent(ActionEvent event){
  186.         try {
  187.             Connection myConn = conn.getConnection();
  188.             Customer selectedItem = databaseTable.getSelectionModel().getSelectedItem();
  189.  
  190.             mySt = myConn.prepareStatement("DELETE FROM `customer`.`customer` WHERE id = ?;");
  191.                     if(selectedItem !=null){
  192.  
  193.                         if(selectedItem.getFirstName() != null) {
  194.  
  195.                         mySt.setLong(1, selectedItem.getId());
  196.                         mySt.executeUpdate();
  197.                         this.tableRefresh();
  198.                         }
  199.                     }
  200.  
  201.                 } catch (SQLException e) {
  202.                     // TODO Auto-generated catch block
  203.                     e.printStackTrace();
  204.                 }finally{
  205.                     try { if (result != null) result.close(); } catch (Exception e) {};
  206.                     try { if (st != null) st.close(); } catch (Exception e) {};
  207.                     //try { if (conn != null) myConn.close(); } catch (Exception e) {};
  208.                 }
  209.             }
  210.  
  211.  
  212.     public void tableRefresh() {
  213.             customers.clear();
  214.  
  215.         try {
  216.  
  217.             Connection myConn = conn.getConnection();
  218.             mySt = myConn.prepareStatement("SELECT * FROM Customer");
  219.             result = mySt.executeQuery();
  220.  
  221.  
  222.         if(!searchField.getText().isEmpty() ){
  223.  
  224.             while(result.next()){
  225.                 if( result.getString(2).toLowerCase().contains((searchField.getText().toLowerCase())) ||
  226.                         result.getString(3).toLowerCase().contains((searchField.getText().toLowerCase() ))){
  227.  
  228.                     customers.add(new Customer(result.getInt(1), result.getString(2),
  229.                             result.getString(3), result.getString(4)));
  230.                 }
  231.  
  232.             }
  233.         }else{
  234.  
  235.         while(result.next()){
  236.             customers.add(new Customer(result.getInt(1), result.getString(2),
  237.                     result.getString(3), result.getString(4)));
  238.             }
  239.  
  240.         }
  241.  
  242.         if(columnId.getCellValueFactory() ==null){
  243.             columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
  244.             columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName"));
  245.  
  246.             columnLastName.setCellValueFactory(new PropertyValueFactory<>("lastName"));
  247.             columnEmail.setCellValueFactory(new PropertyValueFactory<>("email"));
  248.  
  249.             databaseTable.setItems(customers);
  250.         }
  251.  
  252.  
  253.  
  254.         } catch (SQLException e) {
  255.             warningLabel.setText("Lost connection to the database");
  256.  
  257.             e.printStackTrace();
  258.         } finally{
  259.              try { if (result != null) result.close(); } catch (Exception e) {};
  260.              try { if (st != null) st.close(); } catch (Exception e) {};
  261.              try { if (conn != null) myConn.close(); } catch (Exception e) {};
  262.         }
  263.  
  264.     }
  265.  
  266.  
  267.  
  268. }
  269.    
  270. public class AddingScreenController {
  271.  
  272. private databaseConnection conn = new databaseConnection("student","test");
  273. private PreparedStatement stmt =null;
  274. private Connection myConn=null;
  275.  
  276. @FXML
  277. private TextField firstNameTextField;
  278. @FXML
  279. private TextField lastNameTextField;
  280. @FXML
  281. private TextField emailTextField;
  282. @FXML
  283. private Button okButton;
  284. @FXML
  285. private Button cancelButton;
  286. @FXML
  287. private Label warningLabel;
  288.  
  289.  
  290.  
  291. @FXML
  292. private void okbuttonAction (ActionEvent event){
  293.  
  294.  
  295.     if(firstNameTextField.getLength() <1 || lastNameTextField.getLength() <1 || emailTextField.getLength()<1){
  296.         warningLabel.setText("Every field must be filled !");
  297.     }else{
  298.  
  299.     try {
  300.          myConn= conn.getConnection();  
  301.  
  302.          stmt = myConn.prepareStatement("INSERT INTO `customer`.`customer` (`firstName`, `lastName`, `email`) "
  303.                 + "VALUES (?,?,?)");
  304.  
  305.     stmt.setString(1, firstNameTextField.getText());    
  306.     stmt.setString(2, lastNameTextField.getText());
  307.     stmt.setString(3, emailTextField.getText());    
  308.     stmt.executeUpdate();
  309.  
  310.  
  311.  
  312.     this.hideAddingScreen(event);
  313.  
  314.  
  315.  
  316.     } catch (SQLException e) {
  317.         // TODO Auto-generated catch block
  318.         e.printStackTrace();
  319.     } finally{
  320.         // try { if (result != null) result.close(); } catch (Exception e) {};
  321.          try { if (stmt != null) stmt.close(); } catch (Exception e) {};
  322.          try { if (myConn != null) myConn.close(); } catch (Exception e) {};
  323.     }
  324.  
  325.     }
  326. }
  327.  
  328. @FXML
  329. private void cancelbuttonAction (ActionEvent event) throws IOException{
  330.     this.hideAddingScreen(event);
  331.  
  332. }
  333.  
  334. private void hideAddingScreen (ActionEvent event){
  335.     Stage addStage = (Stage) ((Node) event.getTarget()).getScene().getWindow();
  336.     addStage.hide();
  337. }
  338.    
  339. public class UpdateScreenController {
  340. private databaseConnection conn = new databaseConnection("student","test");
  341. private PreparedStatement stmt =null;
  342. private Connection myConn=null;
  343.  
  344. private Customer selectedCustomer;
  345.  
  346. @FXML
  347. private TextField firstNameTextField;
  348. @FXML
  349. private TextField lastNameTextField;
  350. @FXML
  351. private TextField emailTextField;
  352. @FXML
  353. private Button okButton;
  354. @FXML
  355. private Button updateButton;
  356. @FXML
  357. private Label warningLabel;
  358.  
  359. public void setSelectedCustomer(Customer customer){
  360.     this.selectedCustomer=customer;
  361. }
  362.  
  363. @FXML
  364. private void updateButtonAction(ActionEvent event){
  365.      try {
  366.  
  367.          if(firstNameTextField.getLength() <1 || lastNameTextField.getLength() <1 || emailTextField.getLength()<1){
  368.                 warningLabel.setText("Every field must be filled !");
  369.          } else{
  370.  
  371.  
  372.  
  373.         myConn= conn.getConnection();
  374.  
  375.          stmt = myConn.prepareStatement("UPDATE customer SET firstName=?, lastName=?, email=? "
  376.                 + "WHERE id=?");
  377.  
  378.         stmt.setString(1, firstNameTextField.getText());    
  379.         stmt.setString(2, lastNameTextField.getText());
  380.         stmt.setString(3, emailTextField.getText());    
  381.         stmt.setLong(4, selectedCustomer.getId());
  382.  
  383.  
  384.  
  385.         stmt.executeUpdate();
  386.         this.hideUpdateScreen(event);
  387.         }
  388.  
  389.  
  390.     } catch (SQLException e) {
  391.         // TODO Auto-generated catch block
  392.         e.printStackTrace();
  393.     }  
  394.  
  395.  
  396.  
  397. }
  398.  
  399. @FXML
  400. private void cancelButtonAction(ActionEvent event){
  401.     this.hideUpdateScreen(event);
  402.  
  403. }
  404.  
  405. private void hideUpdateScreen (ActionEvent event){
  406.     Stage updateStage = (Stage) ((Node) event.getTarget()).getScene().getWindow();
  407.     updateStage.hide();
  408. }
  409.  
  410. public void fillTextFields(){
  411.     firstNameTextField.setText(selectedCustomer.getFirstName());
  412.     lastNameTextField.setText(selectedCustomer.getLastName());
  413.     emailTextField.setText(selectedCustomer.getEmail());
  414. }
  415.    
  416. public class databaseConnection {
  417. private String username;
  418. private String password;
  419. private Connection conn =null;
  420. private PreparedStatement mySt = null;
  421. private Statement st = null;
  422. private ResultSet result = null;
  423.  
  424.  
  425. public databaseConnection(String username,String password){
  426.     this.username=username;
  427.     this.password=password;
  428. }
  429.  
  430.  
  431. public Connection getConnection() throws SQLException{
  432.  
  433.     if(conn ==null){
  434.     conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/customer",username,password);
  435.  
  436.     }
  437.     return conn;
  438. }
  439.    
  440. public class Customer {
  441.  
  442. private int id;
  443. private String lastName;
  444. private String firstName;
  445. private String email;
  446.  
  447.  
  448. public Customer(int id, String lastName, String firstName, String email) {
  449.  
  450.     this.id = id;
  451.     this.lastName = lastName;
  452.     this.firstName = firstName;
  453.     this.email = email;
  454.  
  455. }
  456.  
  457. public int getId() {
  458.     return id;
  459. }
  460.  
  461. public void setId(int id) {
  462.     this.id = id;
  463. }
  464.  
  465. public String getLastName() {
  466.     return lastName;
  467. }
  468.  
  469. public void setLastName(String lastName) {
  470.     this.lastName = lastName;
  471. }
  472.  
  473. public String getFirstName() {
  474.     return firstName;
  475. }
  476.  
  477. public void setFirstName(String firstName) {
  478.     this.firstName = firstName;
  479. }
  480.  
  481. public String getEmail() {
  482.     return email;
  483. }
  484.  
  485. public void setEmail(String email) {
  486.     this.email = email;
  487. }
  488.  
  489.  
  490.  
  491. @Override
  492. public String toString() {
  493.     return String.format("Customer [id=%s, lastName=%s, firstName=%s, email=%s]",
  494.                     id, lastName, firstName, email);
  495. }
  496.    
  497. *public class Main extends Application {
  498.  
  499.  
  500. @Override
  501. public void start(Stage primaryStage) {
  502.  
  503.     try {
  504.         Pane root =FXMLLoader.load(getClass().getResource("fxml/Login.fxml"));
  505.         Scene scene = new Scene(root);
  506.         primaryStage.setScene(scene);
  507.         primaryStage.show();
  508.  
  509.     } catch(Exception e) {
  510.         e.printStackTrace();
  511.     }
  512. }
  513.  
  514. public static void main(String[] args) {
  515.     launch(args);
  516. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top