Advertisement
Guest User

Untitled

a guest
May 29th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1.  
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javafx.application.Application;
  11. import javafx.geometry.Insets;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.TextField;
  16. import javafx.scene.layout.HBox;
  17. import javafx.scene.layout.VBox;
  18. import javafx.stage.Stage;
  19.  
  20. /**
  21.  * 1. get rid of Egyptian folks (Mahmoud)
  22.  * 2. Add the Driver file to the project.
  23.  * 3.
  24.  *
  25.  *
  26.  *
  27.  */
  28. public class Main extends Application{
  29.    
  30.    
  31.     public static void main(String... args){
  32.        
  33.        
  34.         launch(args);
  35.        
  36.        
  37.     }
  38.    
  39.    
  40.     public Connection getConnection(){
  41.        
  42.         Connection connection = null;
  43.         try {
  44.             Class.forName("com.mysql.jdbc.Driver");
  45.            
  46.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/final", "root", "");
  47.            
  48.            
  49.         } catch (Exception ex) {
  50.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  51.         }
  52.        
  53.         return connection;
  54.        
  55.     }
  56.    
  57.    
  58.     public void start(Stage primary){
  59.        
  60.         VBox root = new VBox(20);
  61.         root.setPadding(new Insets(20));
  62.        
  63.            
  64.         Button add = new Button("Add");
  65.         add.setOnAction(e->addEmployee(root));
  66.        
  67.         root.getChildren().addAll(add);
  68.         try{
  69.            
  70.             Statement stmt = getConnection().createStatement();
  71.             ResultSet result = stmt.executeQuery("SELECT * FROM employee");
  72.             while(result.next()){
  73.                
  74.                 int id = result.getInt("id");
  75.                 String name = result.getString("name");
  76.                 String phone = result.getString("phone");
  77.                 int sal = result.getInt("sal");
  78.                
  79.                 Label lbl = new Label(id + ": " + name + ":" + phone + ":" + sal);
  80.                 Button del = new Button("Delete");
  81.                
  82.                 HBox box = new HBox(lbl, del);
  83.                 del.setOnAction(e->{
  84.                     root.getChildren().remove(box);
  85.                     try{
  86.                         Statement st = getConnection().createStatement();
  87.                         st.executeUpdate("DELETE FROM employee WHERE id=" + id );
  88.                     }catch(Exception exx){
  89.                         System.out.println(exx);
  90.                        
  91.                     }
  92.                 });
  93.                
  94.            
  95.                 root.getChildren().add(box);
  96.                
  97.                
  98.             }
  99.            
  100.         }catch(Exception ex){
  101.             System.out.println(ex);
  102.         }
  103.        
  104.        
  105.    
  106.        
  107.         primary.setScene(new Scene(root, 400, 500));
  108.         primary.show();
  109.        
  110.        
  111.     }
  112.    
  113.     public void addEmployee(VBox root2){
  114.         Stage addWindows = new Stage();
  115.        
  116.         TextField name = new TextField();
  117.         name.setPromptText("Name");
  118.        
  119.         TextField phone = new TextField();
  120.         phone.setPromptText("Phone");
  121.        
  122.         TextField salary = new TextField();
  123.         salary.setPromptText("Salary");
  124.  
  125.         Button save = new Button("Save");
  126.        
  127.         save.setOnAction(e->{
  128.             try{
  129.                
  130.                
  131.                 Statement stmt = getConnection().createStatement();
  132.                 stmt.executeUpdate("INSERT INTO employee (name, phone, sal) VALUES ('" + name.getText() + "', '" + phone.getText() + "', " +salary.getText()+ ")");
  133.                 addWindows.close();
  134.  
  135.                 Label lbl = new Label( name.getText() + ":" + phone.getText() + ":" + salary.getText());
  136.                 Button del = new Button("Delete");
  137.                
  138.                 HBox box = new HBox(lbl, del);
  139.                 del.setOnAction(exxx->{
  140.                     root2.getChildren().remove(box);
  141.                     try{
  142.                         Statement st = getConnection().createStatement();
  143.                         st.executeUpdate("DELETE FROM employee WHERE name='" + name.getText() + "'");
  144.                     }catch(Exception exx){
  145.                         System.out.println(exx);
  146.                        
  147.                     }
  148.                 });
  149.                
  150.                 root2.getChildren().add(box);
  151.                
  152.                
  153.             }catch(Exception ee){
  154.                 System.out.println(ee);
  155.             }
  156.        
  157.        
  158.         });
  159.        
  160.        
  161.         VBox root = new VBox(20, name, phone, salary, save);
  162.         root.setPadding(new Insets(20));
  163.    
  164.         addWindows.setScene(new Scene(root, 250, 200));
  165.         addWindows.show();
  166.         addWindows.setTitle("Add Employee");
  167.        
  168.        
  169.        
  170.     }
  171.  
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement