كيفاش تسجل وتعدل وتمسح من قاعدة البيانات بإستعمال Java و Mysql الجزء الثاني


فهاد الجزء الثاني والآخير من كيفاش تسجل وتعدل وتمسح من قاعدة البيانات بإستعمال Java و Mysql غادي نكملو الملفات الرئيسية ديالنا لي غادي تمكنا من عرض المستخدمين الملف لي غادي يمكن من إضافة مستخدم وتعديله ثم الملف لي غادي يمكن من عرض جدول المستخدمين والقائمة لي غادي تمكن من تصفح كل الملفات.

نظرة سريعة بالفيديو

- الملف Home.java

اول حاجة كنزيد واحد لpackage جديد كنسميه forms من بعد كنزيد فيه ملف جديد كنسميه Home عبارة عن JFrame ولي غادي يكون عبارة عن صفحة الترحيب ولي غادي تبان أول ما تفتح ل application فيه فقط واحد label لي يمكنلك تغير المحتوى ديالها وتدير فيها رسالة ترحيب الكود ديال الملف هو :

                                /*
 * 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 Forms;

/**
 *
 * @author Imad
 */
public class Home extends javax.swing.JPanel {

    /**
     * Creates new form Home
     */
    public Home() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 51));
        jLabel1.setText("Gestion Employés Par DarijaCoding @2017");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 445, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(129, Short.MAX_VALUE))
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}

                            

- الملف AddForm.java

دائما فforms كنزيد فيه ملف جديد كنسميه AddForm عبارة عن JFrame ولي غادي يكون فيه الform لي غادي يمكن من إضافة مستخدم جديد فمن بعد مكنكليكي على الزر إضافة كنسترجع القيم لي دخل المستخدم من بعد كن نعيط للfonction insert لي كاينة فل Controller من بعد كن سد لملف AddForm بواسطة dispose وكنفتح الملف لي فيه جدول المستخدمين ولي غادي نزيدوه من بعد الكود ديال الملف هو :

                                /*
 * 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 Forms;

import employes.Employes;
import employes.model.Controller;
import employes.model.Database;
import employes.model.Employe;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Imad
 */
public class AddForm extends javax.swing.JFrame {

    /**
     * Creates new form AddForm
     */
    public AddForm() {
        initComponents();
        setTitle("Ajouter un employé");
        setSize(320,350);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        nomText = new javax.swing.JTextField();
        prenomText = new javax.swing.JTextField();
        posteText = new javax.swing.JTextField();
        dateText = new javax.swing.JTextField();
        genderCombo = new javax.swing.JComboBox();
        addBtn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Nom");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("Prénom");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel2, gridBagConstraints);

        jLabel3.setText("Poste");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel3, gridBagConstraints);

        jLabel4.setText("Date Recru");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel4, gridBagConstraints);

        jLabel5.setText("Gender");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(nomText, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(prenomText, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(posteText, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(dateText, gridBagConstraints);

        genderCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Homme", "Femme" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(genderCombo, gridBagConstraints);

        addBtn.setText("Ajouter");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(addBtn, gridBagConstraints);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 51, 51));
        jLabel6.setText("Ajouter un employé");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 50, 0);
        add(jLabel6, gridBagConstraints);
    }// </editor-fold>                        

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        String nom = nomText.getText();
        String prenom = prenomText.getText();
        String sexe = genderCombo.getSelectedItem().toString();
        String poste = posteText.getText();
        String dateRec = dateText.getText();
        Employe emp = new Employe(nom,prenom,sexe,poste,dateRec);
        emp.toString();
        Controller db = new Controller();
        try {
            db.Insert(emp);
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        dispose();
        try {
            new Employes(new EmpList());
        } catch (SQLException ex) {
            Logger.getLogger(AddForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                      


    // Variables declaration - do not modify                     
    private javax.swing.JButton addBtn;
    private javax.swing.JTextField dateText;
    private javax.swing.JComboBox genderCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField nomText;
    private javax.swing.JTextField posteText;
    private javax.swing.JTextField prenomText;
    // End of variables declaration                   
}

                            

- الملف UpdateForm.java

دائما فforms كنزيد فيه ملف جديد كنسميه UpdateForm عبارة عن JFrame ولي غادي يكون فيه الform لي غادي يمكن من تعديل مستخدم فمن بعد مكنكليكي على الزر تعديل كنسترجع القيم لي دخل المستخدم كنزيد objet من Employe وكنعطيه القيم لي دخل المستخدم من بعد كن نعيط للfonction Update لي كاينة فل Controller من بعد كن سد لملف UpdateForm بواسطة dispose ثم كاين الزر ديال الحدف ولي منبعد مكتكليكي عليه كن نعيط للfonction delete لي كاينة فل Controller ولي كاتخد ل id ديال المستخدم لي غادي يتحدف الكود ديال الملف هو :

                                /*
 * 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 Forms;

import employes.Employes;
import employes.model.Controller;
import employes.model.Employe;
import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Imad
 */
public class UpdateForm extends javax.swing.JFrame {
    /**
     * Creates new form UpdateForm
     */
    private int id;
    public UpdateForm() {
        initComponents();
    }
    public UpdateForm(Employe emp) {
        initComponents();
        this.id = emp.getId();
        //System.out.println(emp.toString());
        nomText.setText(emp.getNom());
        prenomText.setText(emp.getPrenom());
        posteText.setText(emp.getPoste());
        genderCombo.setSelectedItem(emp.getGender());
        dateText.setText(emp.getDateRec());
        setTitle("Modifier un employé");
        setSize(320,350);
        setVisible(true);
        setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        nomText = new javax.swing.JTextField();
        prenomText = new javax.swing.JTextField();
        posteText = new javax.swing.JTextField();
        dateText = new javax.swing.JTextField();
        genderCombo = new javax.swing.JComboBox();
        updateBtn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        deleteBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Nom");

        jLabel2.setText("Prénom");

        jLabel3.setText("Poste");

        jLabel4.setText("Date Recru");

        jLabel5.setText("Gender");

        genderCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Homme", "Femme" }));

        updateBtn.setText("Modifier");
        updateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBtnActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 51, 51));
        jLabel6.setText("Modifier un employé");

        deleteBtn.setText("Supprimer");
        deleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 69, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel6)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(43, 43, 43))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(nomText)
                            .addComponent(prenomText)
                            .addComponent(posteText)
                            .addComponent(genderCombo, 0, 172, Short.MAX_VALUE)
                            .addComponent(dateText)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(deleteBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(updateBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                .addGap(284, 284, 284))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(nomText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prenomText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(posteText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(genderCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(dateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteBtn)
                    .addComponent(updateBtn))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 269;
        gridBagConstraints.ipady = 117;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>                        

    private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        Controller cr = new Controller();
        Employe u = new Employe();
        u.setId(this.id);
        //System.out.println(this.id);
        u.setNom(nomText.getText());
        u.setPrenom(prenomText.getText());
        u.setGender(genderCombo.getSelectedItem().toString());
        u.setPoste(posteText.getText());
        u.setDateRec(dateText.getText());
        try {
            cr.Update(u);
            new Employes(new EmpList());
        } catch (SQLException ex) {
            Logger.getLogger(UpdateForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        dispose();
    }                                         

    private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        Controller cr = new Controller();
        try {
            cr.delete(this.id);
            new Employes(new EmpList());
        } catch (SQLException ex) {
            Logger.getLogger(UpdateForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        dispose();
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UpdateForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UpdateForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UpdateForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UpdateForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UpdateForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField dateText;
    private javax.swing.JButton deleteBtn;
    private javax.swing.JComboBox genderCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nomText;
    private javax.swing.JTextField posteText;
    private javax.swing.JTextField prenomText;
    private javax.swing.JButton updateBtn;
    // End of variables declaration                   
}

                            

- الملف EmpList.java

دائما فforms كنزيد فيه ملف جديد كنسميه EmpList عبارة عن JPanel ولي غادي يكون فيه table لي غادي تعرض جميع المستخدمين لي عندي فقاعدة البيانات فأولا فEmpList كنعمر table بإستعمال ل fonction fillTable منبعد كنزيدلها الكود لي كيمكن باش ملي يظغط عليها المستخدم كتاخد السطر لي تظغط عليه وكتعرضو فالform update من بعد مكتبحث عليه بإستعمال لfonction findById وهاد الكود هو لي كاين فل fonction valueChanged الكود ديال الملف هو :

                                    /*
 * 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 Forms;
import employes.model.Controller;
import employes.model.Employe;
import java.sql.SQLException;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;

/**
 *
 * @author Imad
 */
public class EmpList extends javax.swing.JPanel implements ListSelectionListener{

    /**
     * Creates new form EmpList
     */
    private final Controller controller = new Controller();
    public EmpList() throws SQLException {
        initComponents();
        jTable1.setModel(controller.fillTable());
        ListSelectionModel selectedRow = jTable1.getSelectionModel();
        selectedRow.addListSelectionListener(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        jTable1.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 51));
        jLabel1.setText("Liste des employés");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addComponent(jLabel1)
                .addContainerGap(114, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if(e.getSource() == jTable1.getSelectionModel() && e.getValueIsAdjusting()){
           Employe emp = null;
           TableModel model = (TableModel) jTable1.getModel();
           //return id field
           Integer i = (Integer) model.getValueAt(jTable1.getSelectedRow(),0);
           Controller cr = new Controller();
            try {
                emp = cr.findById(i);
                //System.out.println(emp.getNom());
            } catch (SQLException ex) {
               System.out.println(ex);
            }
            new UpdateForm(emp).setVisible(true);
       }
    }
}

                                

- الملف Employes.java

الملف الرئيسي ديالي ولي هو Employes.java غادي يكون فيه الكود لي فل constructeur Employes(JPanel panel كيستقبل الجدول لي كيعرض المستخدمين كاملين وكيزيدو فالصفحة الرئيسية ثم كاين constructeur Employes لي كيستقبل الصفحة ديال الترحيب وكيزيدها فالصفحة الرئيسية ثم كاين ل fonction createMenu لي كتزيدنا القائمة للصفحة الرئيسية ولي فيها ايضا الكود لي كيدي للإضافة مستخدم ملي كتظغط على إضافة ولي كيعرض المستخدمين ملي كتظغط على لائحة المستخدمين وكيسد لapplication ملي كتكليكي على خروج الكود ديال الملف هو :

                                    /*
 * 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 employes;

import Forms.AddForm;
import Forms.EmpList;
import Forms.Home;
import employes.model.Controller;
import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Imad
 */
public final class Employes extends JFrame{

    /**
     * @throws java.sql.SQLException
     */
    public Employes(JPanel panel) {
       add(panel,BorderLayout.CENTER); 
       setSize(500,400);
       setTitle("Liste des employés");
       setVisible(true);
    }
    public Employes() throws SQLException{
        add(new Home(),BorderLayout.CENTER);
        setTitle("Employés App");
        setSize(500,400);
        setVisible(true);
        setJMenuBar(this.createMenu());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public JMenuBar createMenu(){
        JMenuBar jMenuBar1 = new JMenuBar();
        JMenu fileMenu = new JMenu("Fichier");
        JMenuItem addData = new JMenuItem("Ajouter");
        JMenuItem exit = new JMenuItem("Sortir");
        fileMenu.add(addData);
        fileMenu.addSeparator();
        fileMenu.add(exit);
        JMenu windowFile = new JMenu("Fenêtre");
        JMenu showMenu = new JMenu("Voir");
        JMenuItem formEmp = new JMenuItem("Liste Employés");
        showMenu.add(formEmp);
        windowFile.add(showMenu);
        jMenuBar1.add(fileMenu);
        jMenuBar1.add(windowFile);
        exit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                int choice = JOptionPane.showConfirmDialog(Employes.this,"Voulez vous vraiment quitter ?","Confirmation",JOptionPane.OK_CANCEL_OPTION);
                if(choice == JOptionPane.OK_OPTION){
                    System.exit(0);
                }
            } 
        });
        addData.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddForm().setVisible(true);
            }
            
        });
        formEmp.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new Employes(new EmpList());
                } catch (SQLException ex) {
                    Logger.getLogger(Employes.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        return jMenuBar1;
    }
    public static void main(String[] args) throws SQLException{
        // TODO code application logic here
        Employes employes = new Employes();
    }
}

                                


إشترك في قناتنا على اليوتيوب

بحث في الموقع


إشترك للتوصل بالجديد