Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 32.34 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package JFrames;
  7.  
  8. import Project.App;
  9. import Project.Driver;
  10. import Project.Users;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.util.Date;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.swing.JOptionPane;
  21.  
  22. /**
  23.  *
  24.  * @author Utilizador
  25.  */
  26. public class EditarPerfil extends javax.swing.JFrame {
  27.  
  28.    Users currentUser;
  29.    Driver currentDriver;
  30.    App app;
  31.    int userId;
  32.    
  33.     private final String url = "jdbc:postgresql://localhost:5432/RideOnCar";
  34.     private final String user = "borys";
  35.     private final String password = "qwerty";    
  36.  
  37.     public Connection connect() throws SQLException {
  38.         return DriverManager.getConnection(url, user, password);
  39.     }
  40.  
  41.     /**
  42.      * Creates new form EditarPerfil
  43.      * @param currentUser
  44.      */
  45.     public EditarPerfil(Users currentUser) throws SQLException {
  46.         this.currentUser = currentUser;
  47.         userId = currentUser.getUserId();
  48.         currentDriver = new Driver();
  49.         app = new App();
  50.         initComponents();
  51.         myComponents();        
  52.        
  53.     }
  54.  
  55.     /**
  56.      * This method is called from within the constructor to initialize the form.
  57.      * WARNING: Do NOT modify this code. The content of this method is always
  58.      * regenerated by the Form Editor.
  59.      */
  60.     @SuppressWarnings("unchecked")
  61.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  62.     private void initComponents() {
  63.  
  64.         jScrollPane1 = new javax.swing.JScrollPane();
  65.         jEditorPane1 = new javax.swing.JEditorPane();
  66.         jLabel3 = new javax.swing.JLabel();
  67.         textFieldJob = new javax.swing.JTextField();
  68.         jLabel4 = new javax.swing.JLabel();
  69.         textFieldName = new javax.swing.JTextField();
  70.         textFieldCar = new javax.swing.JTextField();
  71.         labelNome = new javax.swing.JLabel();
  72.         jButtonGuardar = new javax.swing.JButton();
  73.         labelEmail = new javax.swing.JLabel();
  74.         jButtonSair = new javax.swing.JButton();
  75.         textFieldEmail = new javax.swing.JTextField();
  76.         jLabel1 = new javax.swing.JLabel();
  77.         jLabel2 = new javax.swing.JLabel();
  78.         textFieldAge = new javax.swing.JTextField();
  79.         passwordField = new javax.swing.JPasswordField();
  80.         jLabel5 = new javax.swing.JLabel();
  81.         textFieldLicense = new javax.swing.JTextField();
  82.         jComboBox1 = new javax.swing.JComboBox();
  83.         jLabel6 = new javax.swing.JLabel();
  84.         jDateChooser1 = new com.toedter.calendar.JDateChooser();
  85.         jLabel7 = new javax.swing.JLabel();
  86.  
  87.         jScrollPane1.setViewportView(jEditorPane1);
  88.  
  89.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  90.  
  91.         jLabel3.setText("Profissão");
  92.  
  93.         textFieldJob.addActionListener(new java.awt.event.ActionListener() {
  94.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  95.                 textFieldJobActionPerformed(evt);
  96.             }
  97.         });
  98.  
  99.         jLabel4.setText("Carro");
  100.  
  101.         textFieldName.addActionListener(new java.awt.event.ActionListener() {
  102.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  103.                 textFieldNameActionPerformed(evt);
  104.             }
  105.         });
  106.  
  107.         textFieldCar.addMouseListener(new java.awt.event.MouseAdapter() {
  108.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  109.                 textFieldCarMouseClicked(evt);
  110.             }
  111.         });
  112.         textFieldCar.addActionListener(new java.awt.event.ActionListener() {
  113.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  114.                 textFieldCarActionPerformed(evt);
  115.             }
  116.         });
  117.  
  118.         labelNome.setText("Nome");
  119.  
  120.         jButtonGuardar.setText("Guardar");
  121.         jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
  122.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  123.                 jButtonGuardarActionPerformed(evt);
  124.             }
  125.         });
  126.  
  127.         labelEmail.setText("Email");
  128.  
  129.         jButtonSair.setText("Sair");
  130.         jButtonSair.addActionListener(new java.awt.event.ActionListener() {
  131.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  132.                 jButtonSairActionPerformed(evt);
  133.             }
  134.         });
  135.  
  136.         textFieldEmail.addActionListener(new java.awt.event.ActionListener() {
  137.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  138.                 textFieldEmailActionPerformed(evt);
  139.             }
  140.         });
  141.  
  142.         jLabel1.setText("Palavra-passe");
  143.  
  144.         jLabel2.setText("Idade");
  145.  
  146.         textFieldAge.addActionListener(new java.awt.event.ActionListener() {
  147.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  148.                 textFieldAgeActionPerformed(evt);
  149.             }
  150.         });
  151.  
  152.         passwordField.setText("jPasswordField1");
  153.         passwordField.addActionListener(new java.awt.event.ActionListener() {
  154.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  155.                 passwordFieldActionPerformed(evt);
  156.             }
  157.         });
  158.  
  159.         jLabel5.setText("Nº Carta Condução");
  160.  
  161.         textFieldLicense.addMouseListener(new java.awt.event.MouseAdapter() {
  162.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  163.                 textFieldLicenseMouseClicked(evt);
  164.             }
  165.         });
  166.         textFieldLicense.addActionListener(new java.awt.event.ActionListener() {
  167.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  168.                 textFieldLicenseActionPerformed(evt);
  169.             }
  170.         });
  171.  
  172.         jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione", "B1", "B", "C1", "C", "D1", "D", "BE", "C1E", "CE", "D1E", "DE" }));
  173.         jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
  174.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  175.                 jComboBox1MouseClicked(evt);
  176.             }
  177.         });
  178.         jComboBox1.addActionListener(new java.awt.event.ActionListener() {
  179.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  180.                 jComboBox1ActionPerformed(evt);
  181.             }
  182.         });
  183.  
  184.         jLabel6.setText("Categoria");
  185.  
  186.         jDateChooser1.addMouseListener(new java.awt.event.MouseAdapter() {
  187.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  188.                 jDateChooser1MouseClicked(evt);
  189.             }
  190.         });
  191.  
  192.         jLabel7.setText("Data Validade");
  193.  
  194.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  195.         getContentPane().setLayout(layout);
  196.         layout.setHorizontalGroup(
  197.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  198.             .addGroup(layout.createSequentialGroup()
  199.                 .addGap(22, 22, 22)
  200.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  201.                     .addGroup(layout.createSequentialGroup()
  202.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  203.                             .addGroup(layout.createSequentialGroup()
  204.                                 .addComponent(jLabel5)
  205.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  206.                                     .addGroup(layout.createSequentialGroup()
  207.                                         .addGap(1, 1, 1)
  208.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  209.                                             .addComponent(textFieldAge, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
  210.                                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  211.                                                 .addComponent(textFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
  212.                                                 .addComponent(textFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
  213.                                                 .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  214.                                             .addComponent(textFieldJob, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
  215.                                         .addGap(118, 118, 118))
  216.                                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  217.                                         .addGap(18, 18, 18)
  218.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  219.                                             .addGroup(layout.createSequentialGroup()
  220.                                                 .addGap(0, 0, Short.MAX_VALUE)
  221.                                                 .addComponent(jButtonSair))
  222.                                             .addGroup(layout.createSequentialGroup()
  223.                                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  224.                                                     .addComponent(textFieldCar)
  225.                                                     .addComponent(textFieldLicense)
  226.                                                     .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
  227.                                                 .addGap(93, 93, 93)
  228.                                                 .addComponent(jLabel6)
  229.                                                 .addGap(18, 18, 18)
  230.                                                 .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  231.                                         .addGap(18, 18, 18))))
  232.                             .addGroup(layout.createSequentialGroup()
  233.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  234.                                     .addComponent(labelEmail)
  235.                                     .addComponent(labelNome)
  236.                                     .addComponent(jLabel1)
  237.                                     .addComponent(jLabel2)
  238.                                     .addComponent(jLabel3)
  239.                                     .addComponent(jLabel4))
  240.                                 .addGap(18, 18, Short.MAX_VALUE)))
  241.                         .addComponent(jButtonGuardar))
  242.                     .addGroup(layout.createSequentialGroup()
  243.                         .addComponent(jLabel7)
  244.                         .addGap(0, 0, Short.MAX_VALUE)))
  245.                 .addGap(94, 94, 94))
  246.         );
  247.         layout.setVerticalGroup(
  248.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  249.             .addGroup(layout.createSequentialGroup()
  250.                 .addGap(20, 20, 20)
  251.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  252.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  253.                         .addComponent(labelNome)
  254.                         .addGap(18, 18, 18)
  255.                         .addComponent(labelEmail)
  256.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  257.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  258.                             .addComponent(jLabel1)
  259.                             .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  260.                         .addGap(18, 18, 18)
  261.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  262.                             .addComponent(textFieldAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  263.                             .addComponent(jLabel2)))
  264.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  265.                         .addComponent(textFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  266.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  267.                         .addComponent(textFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  268.                         .addGap(83, 83, 83)))
  269.                 .addGap(24, 24, 24)
  270.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  271.                     .addComponent(jLabel3)
  272.                     .addComponent(textFieldJob, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  273.                 .addGap(41, 41, 41)
  274.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  275.                     .addComponent(jLabel4)
  276.                     .addComponent(textFieldCar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  277.                 .addGap(23, 23, 23)
  278.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  279.                     .addComponent(jLabel5)
  280.                     .addComponent(textFieldLicense, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  281.                     .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
  282.                     .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  283.                 .addGap(18, 18, 18)
  284.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  285.                     .addComponent(jLabel7)
  286.                     .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  287.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
  288.                 .addComponent(jButtonSair)
  289.                 .addGap(21, 21, 21))
  290.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  291.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  292.                 .addComponent(jButtonGuardar)
  293.                 .addContainerGap())
  294.         );
  295.  
  296.         pack();
  297.     }// </editor-fold>                        
  298.  
  299.     private void textFieldJobActionPerformed(java.awt.event.ActionEvent evt) {                                            
  300.         // TODO add your handling code here:
  301.     }                                            
  302.  
  303.     private void textFieldNameActionPerformed(java.awt.event.ActionEvent evt) {                                              
  304.  
  305.     }                                            
  306.  
  307.     private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {                                              
  308.         String name = textFieldName.getText();
  309.         String email = textFieldEmail.getText();
  310.         String job = textFieldJob.getText();
  311.         String password = passwordField.getText();
  312.         int age = 0;
  313.  
  314.         //gets Age from text field
  315.         try {
  316.             age = Integer.parseInt(textFieldAge.getText());
  317.         }
  318.         catch(NumberFormatException ex) {
  319.             System.out.println("Exception: " +ex);
  320.         }
  321.  
  322.         //gets Driver details
  323.        
  324.         try {
  325.             currentUser.saveProfile(name, email, age, job, password);
  326.         } catch (SQLException ex) {
  327.             Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  328.         }
  329.        
  330.         try {
  331.             if (currentDriver.updateDriver(currentUser.getUserId(), Integer.parseInt(textFieldLicense.getText()), jDateChooser1.getDate(), String.valueOf(jComboBox1.getSelectedItem()), textFieldCar.getText()) == true) {
  332.                 JOptionPane.showMessageDialog(null, "Informação alterada com sucesso");                
  333.             }
  334.         } catch (java.lang.NumberFormatException e) {
  335.             JOptionPane.showMessageDialog(null, "Introduza número de carta válido");
  336.         } catch (java.lang.NullPointerException e) {
  337.             JOptionPane.showMessageDialog(null, "Selecione uma data válida!!");
  338.         } catch (SQLException ex) {
  339.             JOptionPane.showMessageDialog(null, "Selecione uma categoria válida!!");
  340.         }
  341.        
  342.        try {
  343.            new UsersFrame(currentUser).setVisible(true);
  344.            this.dispose();
  345.        } catch (SQLException ex) {
  346.            Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  347.        }
  348.          
  349.        
  350.        
  351.        
  352.     }                                              
  353.  
  354.     private void textFieldEmailActionPerformed(java.awt.event.ActionEvent evt) {                                              
  355.         // TODO add your handling code here:
  356.     }                                              
  357.  
  358.     private void textFieldAgeActionPerformed(java.awt.event.ActionEvent evt) {                                            
  359.         // TODO add your handling code here:
  360.     }                                            
  361.  
  362.     private void jButtonSairActionPerformed(java.awt.event.ActionEvent evt) {                                            
  363.         try {
  364.            new UsersFrame(currentUser).setVisible(true);
  365.            this.dispose();
  366.        } catch (SQLException ex) {
  367.            Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  368.        }
  369.     }                                          
  370.  
  371.     private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
  372.         // TODO add your handling code here:
  373.     }                                            
  374.  
  375.     private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  376.         // TODO add your handling code here:
  377.     }                                          
  378.  
  379.     private void textFieldCarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  380.         // TODO add your handling code here:
  381.     }                                            
  382.  
  383.     private void textFieldCarMouseClicked(java.awt.event.MouseEvent evt) {                                          
  384.        try {
  385.            if(app.verificarCondutor(userId) == false){
  386.                new Condutor(currentUser).setVisible(true);
  387.                this.dispose();
  388.            }
  389.        } catch (SQLException ex) {
  390.            Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  391.        }
  392.     }                                        
  393.  
  394.     private void textFieldLicenseMouseClicked(java.awt.event.MouseEvent evt) {                                              
  395.         try {
  396.            if(app.verificarCondutor(userId) == false){
  397.                new Condutor(currentUser).setVisible(true);
  398.                this.dispose();
  399.            }
  400.        } catch (SQLException ex) {
  401.            Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  402.        }
  403.     }                                            
  404.  
  405.     private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {                                        
  406.         try {
  407.            if(app.verificarCondutor(userId) == false){
  408.                new Condutor(currentUser).setVisible(true);
  409.                this.dispose();
  410.            }
  411.        } catch (SQLException ex) {
  412.            Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  413.        }
  414.     }                                      
  415.  
  416.     private void jDateChooser1MouseClicked(java.awt.event.MouseEvent evt) {                                          
  417.         try {
  418.            if(app.verificarCondutor(userId) == false){
  419.                new Condutor(currentUser).setVisible(true);
  420.                this.dispose();
  421.            }
  422.        } catch (SQLException ex) {
  423.            Logger.getLogger(EditarPerfil.class.getName()).log(Level.SEVERE, null, ex);
  424.        }
  425.     }                                          
  426.  
  427.     private void textFieldLicenseActionPerformed(java.awt.event.ActionEvent evt) {                                                
  428.         // TODO add your handling code here:
  429.     }                                                
  430.  
  431.     /**
  432.      * @param args the command line arguments
  433.      */
  434.     public static void main(String args[]) {
  435.         /* Set the Nimbus look and feel */
  436.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  437.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  438.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  439.          */
  440.         try {
  441.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  442.                 if ("Nimbus".equals(info.getName())) {
  443.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  444.                     break;
  445.                 }
  446.             }
  447.         } catch (ClassNotFoundException ex) {
  448.             java.util.logging.Logger.getLogger(EditarPerfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  449.         } catch (InstantiationException ex) {
  450.             java.util.logging.Logger.getLogger(EditarPerfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  451.         } catch (IllegalAccessException ex) {
  452.             java.util.logging.Logger.getLogger(EditarPerfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  453.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  454.             java.util.logging.Logger.getLogger(EditarPerfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  455.         }
  456.         //</editor-fold>
  457.  
  458.         /* Create and display the form */
  459.         java.awt.EventQueue.invokeLater(new Runnable() {
  460.             public void run() {
  461.                 //new EditarPerfil().setVisible(true);
  462.             }
  463.         });
  464.     }
  465.    
  466.     private void myComponents() throws SQLException {
  467.  
  468.         //Nome
  469.         try (Connection conn = this.connect()) {
  470.             Statement stmt = conn.createStatement();
  471.             String selectSQL = ("SELECT username FROM users WHERE userid = ?");
  472.             PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  473.            
  474.             preparedStmt.setInt(1, currentUser.getUserId());
  475.             ResultSet rs = preparedStmt.executeQuery();    
  476.          
  477.            
  478.             while (rs.next()) {
  479.                 String username = rs.getString("username");
  480.                 textFieldName.setText(username);
  481.                 System.out.println(username);
  482.             }        
  483.            
  484.         } catch (SQLException e) {
  485.                 System.out.println(e.getMessage());
  486.                 throw e;
  487.  
  488.         }
  489.        
  490.         //Email
  491.         try (Connection conn = this.connect()){
  492.             Statement stmt = conn.createStatement();
  493.             String selectSQL = ("SELECT email FROM users WHERE userid = ?");        
  494.        
  495.             PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  496.            
  497.             preparedStmt.setInt(1, currentUser.getUserId());
  498.             ResultSet rs = preparedStmt.executeQuery();    
  499.          
  500.            
  501.             while (rs.next()) {
  502.                 String email = rs.getString("email");
  503.                 textFieldEmail.setText(email);
  504.                 System.out.println(email);
  505.             }        
  506.            
  507.         } catch (SQLException e) {
  508.                 System.out.println(e.getMessage());
  509.                 throw e;
  510.  
  511.         }
  512.        
  513.         //Idade        
  514.         try (Connection conn = this.connect()){
  515.             Statement stmt = conn.createStatement();
  516.             String selectSQL = ("SELECT age FROM users WHERE userid = ?");        
  517.        
  518.             PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  519.            
  520.             preparedStmt.setInt(1, currentUser.getUserId());
  521.             ResultSet rs = preparedStmt.executeQuery();    
  522.          
  523.            
  524.             while (rs.next()) {
  525.                 String idade = rs.getString("age");
  526.                 textFieldAge.setText(idade);
  527.                 System.out.println(idade);
  528.             }        
  529.            
  530.         } catch (SQLException e) {
  531.                 System.out.println(e.getMessage());
  532.                 throw e;
  533.  
  534.         }
  535.        
  536.         //Profissão
  537.         try (Connection conn = this.connect()){
  538.             Statement stmt = conn.createStatement();
  539.             String selectSQL = ("SELECT job FROM users WHERE userid = ?");        
  540.        
  541.             PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  542.            
  543.             preparedStmt.setInt(1, currentUser.getUserId());
  544.             ResultSet rs = preparedStmt.executeQuery();    
  545.          
  546.            
  547.             while (rs.next()) {
  548.                 String job = rs.getString("job");
  549.                 textFieldJob.setText(job);              
  550.             }        
  551.            
  552.         } catch (SQLException e) {
  553.                 System.out.println(e.getMessage());
  554.                 throw e;
  555.  
  556.         }
  557.        
  558.         //Carro
  559.         try (Connection conn = this.connect()){
  560.             Statement stmt = conn.createStatement();
  561.             String selectSQL = ("SELECT job FROM users WHERE userid = ?");        
  562.        
  563.             PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  564.            
  565.             preparedStmt.setInt(1, currentUser.getUserId());
  566.             ResultSet rs = preparedStmt.executeQuery();    
  567.          
  568.            
  569.             while (rs.next()) {
  570.                 String profissao = rs.getString("job");
  571.                 textFieldJob.setText(profissao);
  572.                 System.out.println(profissao);
  573.             }        
  574.            
  575.         } catch (SQLException e) {
  576.                 System.out.println(e.getMessage());
  577.                 throw e;
  578.  
  579.         }
  580.        
  581.         //Retrieves password from database
  582.         try (Connection conn = this.connect()){
  583.             Statement stmt = conn.createStatement();
  584.             String selectSQL = ("SELECT password FROM users WHERE userid = ?");        
  585.        
  586.             PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  587.            
  588.             preparedStmt.setInt(1, currentUser.getUserId());
  589.             ResultSet rs = preparedStmt.executeQuery();    
  590.          
  591.            
  592.             while (rs.next()) {
  593.                 String password = rs.getString("password");
  594.                 passwordField.setText(password);              
  595.             }        
  596.            
  597.         } catch (SQLException e) {
  598.                 System.out.println(e.getMessage());
  599.                 throw e;
  600.  
  601.         }
  602.        
  603.         //DRIVER      
  604.        
  605.         //retrieves Car
  606.         if(app.verificarCondutor(userId) == true){
  607.            
  608.              try (Connection conn = this.connect()){
  609.                 Statement stmt = conn.createStatement();
  610.                 String selectSQL = ("SELECT car FROM driver WHERE userid = ?");        
  611.        
  612.                 PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  613.            
  614.                 preparedStmt.setInt(1, currentUser.getUserId());
  615.                 ResultSet rs = preparedStmt.executeQuery();    
  616.          
  617.            
  618.                 while (rs.next()) {
  619.                     String car = rs.getString("car");
  620.                     textFieldCar.setText(car);      
  621.                 }        
  622.            
  623.                 } catch (SQLException e) {
  624.                         System.out.println(e.getMessage());
  625.                         throw e;
  626.  
  627.                 }
  628.                 } else {
  629.                      textFieldCar.setEditable(false);                          
  630.                 }
  631.        
  632.         if(app.verificarCondutor(userId) == true){
  633.              
  634.              //retrieves number of Driving License
  635.              try (Connection conn = this.connect()){
  636.                 Statement stmt = conn.createStatement();
  637.                 String selectSQL = ("SELECT n_card FROM driver WHERE userid = ?");        
  638.        
  639.                 PreparedStatement preparedStmt = conn.prepareStatement(selectSQL);
  640.            
  641.                 preparedStmt.setInt(1, currentUser.getUserId());
  642.                 ResultSet rs = preparedStmt.executeQuery();  
  643.          
  644.            
  645.                 while (rs.next()) {
  646.                     String license = rs.getString("n_card");
  647.                     textFieldLicense.setText(license);          
  648.                 }        
  649.            
  650.                 } catch (SQLException e) {
  651.                         System.out.println(e.getMessage());
  652.                         throw e;
  653.                 }
  654.                 } else {
  655.                      textFieldLicense.setEditable(false);                          
  656.                 }
  657.                
  658.                 //Retrieves category of driving license
  659.                 if(app.verificarCondutor(userId) == true){
  660.                                    
  661.                
  662.                     try (Connection conn = this.connect()) {
  663.                         Statement stmt = conn.createStatement();
  664.                         String sql = "select * from driver where userid= ?";
  665.                         PreparedStatement preparedStmt = conn.prepareStatement(sql);
  666.                         preparedStmt.setInt(1, userId);
  667.                         ResultSet rs = preparedStmt.executeQuery();
  668.                         if(rs.next()) {                            
  669.                            
  670.                             String category = rs.getString("category");
  671.                             jComboBox1.setSelectedItem(category);
  672.                            
  673.                             Date date = rs.getDate("date");
  674.                             jDateChooser1.setDate(date);
  675.                            
  676.                             String car = rs.getString("car");
  677.                             textFieldCar.setText(car);
  678.                         }
  679.                     }
  680.                
  681.                 }
  682.                
  683.         }
  684.        
  685.    
  686.  
  687.     // Variables declaration - do not modify                    
  688.     private javax.swing.JButton jButtonGuardar;
  689.     private javax.swing.JButton jButtonSair;
  690.     private javax.swing.JComboBox jComboBox1;
  691.     private com.toedter.calendar.JDateChooser jDateChooser1;
  692.     private javax.swing.JEditorPane jEditorPane1;
  693.     private javax.swing.JLabel jLabel1;
  694.     private javax.swing.JLabel jLabel2;
  695.     private javax.swing.JLabel jLabel3;
  696.     private javax.swing.JLabel jLabel4;
  697.     private javax.swing.JLabel jLabel5;
  698.     private javax.swing.JLabel jLabel6;
  699.     private javax.swing.JLabel jLabel7;
  700.     private javax.swing.JScrollPane jScrollPane1;
  701.     private javax.swing.JLabel labelEmail;
  702.     private javax.swing.JLabel labelNome;
  703.     private javax.swing.JPasswordField passwordField;
  704.     private javax.swing.JTextField textFieldAge;
  705.     private javax.swing.JTextField textFieldCar;
  706.     private javax.swing.JTextField textFieldEmail;
  707.     private javax.swing.JTextField textFieldJob;
  708.     private javax.swing.JTextField textFieldLicense;
  709.     private javax.swing.JTextField textFieldName;
  710.     // End of variables declaration                  
  711. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement